CP1 / COURSE HOME

Week 09 / Organise observations

Read a device message

Agree on the record format before extracting its value.

So far, our examples supplied ready-to-use numbers. A device often sends text, and the receiving program must first identify what each field means.

Our teaching device sends T1,23.5,C: sensor ID, numeric text, then unit. The supported ID is T1 and the supported unit is C.

Start with the problem

Try this first.

Label the three fields. Compare T1,23.5,C with T1,23.5 and T1,23.5,F. Which messages follow the agreed structure and unit rule?

Why this week's tool?

String operations separate and inspect fields. We will check the message structure now; handling text that cannot become a number comes in Week 12.

By the end: Explain which field is the value and which fields give it context, and reject an incomplete or unsupported message for a stated reason.

The idea behind the program

A protocol is a shared agreement

A comma is only a delimiter because sender and receiver agree it is. Define field count, allowed IDs, unit spelling and whitespace handling. A plausible-looking number with an unexpected unit is not ready to use.

Parse before interpreting

strip() removes outer whitespace; split(',') separates this deliberately simple format. Strip each field, then inspect its role. A real CSV format can contain quoted commas; this simple protocol explicitly does not support them.

Do not repair ambiguity silently

An extra field or wrong unit should produce an explicit outcome. Guessing which field to discard can hide a sender/receiver mismatch. Preserve the raw message alongside the reason for rejection.

A short Python example

Read the example alongside the explanation. Run it in a new notebook cell and change one input to see how it behaves.

raw = ' T1,23.5,C\n'
fields = raw.strip().split(',')
if len(fields) != 3:
    print('REJECT: expected three fields')
else:
    sensor, value_text, unit = [field.strip() for field in fields]
    if sensor != 'T1' or unit != 'C':
        print('REJECT: unsupported sensor or unit')
    else:
        print('STRUCTURE OK:', sensor, value_text, unit)
        print('Numeric validation still required')

Example output

STRUCTURE OK: T1 23.5 C
Numeric validation still required

Parsing translates a representation under a contract

String processing turns a structured message into named pieces. Splitting locates separators; conversion interprets one piece as a number. Successful conversion alone does not establish that the fields or units mean what the program expects.

Draw or trace

Use the simple teaching format "T1,23.5,C". Draw three labelled boxes: sensor, reading text, unit. Preserve the raw message while deriving the pieces.

Predict before running. Would "T1,,C" mean zero? Would "T1,23.5,F" describe the same temperature?

Trace and explanation — after your prediction
  1. Split the nominal line into T1, 23.5 and C.
  2. Convert only the numeric field after checking that it is present.
  3. Keep the unit attached; the same number with F has a different meaning from C.

An empty field is missing data, not zero. The unit mismatch needs an explicit policy. Simple splitting is suitable only for this restricted format; quoted separators require the CSV tools studied later.

Change one thing. Add a fourth field or remove the unit. Write the expected rejection or handling rule before changing the parser.

Türkçe: Parçalama alanları bulur; dönüşüm sayıyı yorumlar. Boş alan sıfır değildir, birim de verinin anlamının parçasıdır.

Examples and variations

Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.

01Nominal message

' T1,23.5,C\n'

Question: What are the three fields after trimming?

  1. Remove outside whitespace
  2. Split on commas → three fields
  3. Trim fields → T1 | 23.5 | C

Structure accepted; numeric validation still pending

Accepting a message shape is a separate step from accepting its measurement.

02Unexpected unit

'T1,74.3,F'

Question: May the receiver treat 74.3 as degrees Celsius?

  1. Three fields are present
  2. Sensor T1 is supported
  3. Unit F violates the C-only protocol

REJECT: unsupported unit

A conversion would need an explicit protocol extension. Relabelling F as C would corrupt the measurement.

03Extra delimiter

'T1,23,5,C'

Question: Should the parser guess that 23,5 means 23.5?

  1. Split produces T1 | 23 | 5 | C
  2. Field count is four, not three
  3. The record is ambiguous under this protocol

REJECT: expected three fields

Fix the agreement with the sender; do not silently join fields to make the record look valid.

See the Colab code run

Interactive walkthroughs of String Processing. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.

Work on it in Colab

Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.

  1. Write the protocol on an index card: field order, count, allowed ID, allowed unit and whitespace rule.
  2. Run the three bench cases through the parser. Keep a table containing raw message, fields and structural decision.
  3. Try T1,abc,C. Explain why structure can pass while numeric validity remains unresolved.
  4. Exchange one valid and one malformed message with a partner. Decide from the written protocol, not from what you think they intended.

Something to take away: A small protocol specification and a parsing table with explicit reasons for every rejected message.

Suggested exercises, downloads & solutions

Read the notebook's teaching cells before these exercises.

  • EX06 CSV Line Parser (Medium)
  • EX08 Acronym Generator (Medium)
Download notebookWorked solutions

Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.

Optional notes & guidance
My notes
Using AI or working with a partner

Ask AI for a parser for your exact protocol. Give it an extra comma, an unsupported unit and abc as the value. Check that it distinguishes structure checks from numeric validation and does not silently correct data.

You can also review the supplied example with a partner. Use the same inputs to compare the reasoning. Follow the syllabus rules for assessed work.