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.
Week 09 / Organise observations
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
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?
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.
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.
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.
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.
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')STRUCTURE OK: T1 23.5 C Numeric validation still required
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.
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?
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.
Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.
' T1,23.5,C\n'
Question: What are the three fields after trimming?
Accepting a message shape is a separate step from accepting its measurement.
'T1,74.3,F'
Question: May the receiver treat 74.3 as degrees Celsius?
A conversion would need an explicit protocol extension. Relabelling F as C would corrupt the measurement.
'T1,23,5,C'
Question: Should the parser guess that 23,5 means 23.5?
Fix the agreement with the sender; do not silently join fields to make the record look valid.
Interactive walkthroughs of String Processing. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.
Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.
Something to take away: A small protocol specification and a parsing table with explicit reasons for every rejected message.
Read the notebook's teaching cells before these exercises.
Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.
Notes stay in this browser. Download a copy to keep them.
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.