Lite / Building Machines That Respond / Week 9
Build it, test it, break it
Your checker says OK for the real log. Will it still say the right thing when the logger glitches, the file is empty, or a reading is exactly 60.0?
Why should I care?
Building in small, tested steps is how engineers keep a growing program under control. Each function you finish and test becomes something you can trust, so when the whole program misbehaves you know where not to look.
- Implement the planned functions one at a time, testing each with assert before moving on.
- Run the whole program on designed test logs and compare with hand-worked expected reports.
- Record one real bug found during testing: symptom, hypothesis, test and fix.
Predict
Lines "41.2", "ERR", "54.0", "--". What mean should the checker report?
- 23.8 °C (dividing by all 4 lines)
- 47.6 °C (dividing by the 2 good readings)
- It should crash
Guess first, then reveal
47.6 °C: (41.2 + 54.0) / 2. The malformed lines are skipped and counted, and the mean uses only good readings. Dividing by 4 would be a bug that tests should catch.
Discover
In the Code Lab, start with decide() from this lesson and its four asserts: run them. Then add parse_reading, summarise and count_outside one at a time, each with its own asserts, and only then the main program. Paste each of your test logs into the lines list and compare with your expected reports.
What the interactive shows (text version)
The Code Lab exercise starts with decide(values, malformed): it returns INSPECT for an empty list, for any value below 10 or above 60, or when more than 2 lines are malformed; otherwise OK. Its tests: [20.0, 30.0] with 0 malformed → OK; [20.0, 61.0] → INSPECT; [10, 60] with 2 malformed → OK; [25.0] with 3 malformed → INSPECT; [] → INSPECT.
Understand
Build one function at a time
SAFE_LOW_C = 10
SAFE_HIGH_C = 60
def decide(values, malformed):
"""Return "INSPECT" or "OK" following the brief."""
if len(values) == 0:
return "INSPECT"
outside = 0
for v in values:
if v < SAFE_LOW_C or v > SAFE_HIGH_C:
outside += 1
if outside > 0 or malformed > 2:
return "INSPECT"
return "OK"
assert decide([20.0, 30.0], 0) == "OK"
assert decide([20.0, 61.0], 0) == "INSPECT"
assert decide([10, 60], 2) == "OK" # band edges are safe
assert decide([25.0], 3) == "INSPECT"
assert decide([], 0) == "INSPECT"Reading the log
In the Code Lab, paste the log into a list: lines = ["41.2", "ERR", ...]. On your own computer, read a file instead:
with open("motor_log.txt") as f:
lines = f.read().splitlines()Either way the rest of the program receives the same list of strings, which is why parsing lives in its own function.
Keep a bug diary
When a test fails, don't just fix it: write the symptom (decide([10, 60], 2) said INSPECT), the hypothesis (< versus <= on the band), the test that confirmed it (a probe showed outside = 2) and the fix. This record is a deliverable of the project, and the habit is worth more than the program.
Explain it another way
Intuitive
Like building a wall one course of bricks at a time and checking it with a level each time, instead of building the whole wall and hoping it's straight.
Technical
Incremental development with unit tests: each function is verified against its contract before integration. Integration tests (whole test logs with expected reports) then check that the pieces fit together.
Applied
This is how professional software is built: small changes, each covered by tests, run automatically. Your sensor-log checker is a miniature of real condition-monitoring software.
Worked example 1
A tester's log is 60.0, 60.0, 60.1. What should the report be?
All three convert; mean = 180.1 / 3 ≈ 60.03 °C, min 60.0, max 60.1. 60.1 is above 60, so outside = 1 and the decision is INSPECT. If your checker says OK, look at the comparison against SAFE_HIGH_C.
Common mix-ups
If the program works on the real log, it's finished.
✓ It's finished when it gives the right report for every designed test log, including the ones built to break it.Tests slow you down.
✓ Tests make you faster: every later change is checked in seconds instead of by hand.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Complete the project: the full checker with at least four tested functions, your test logs with expected and actual reports, a bug-diary entry for a real bug, and a one-page note explaining the algorithm so another learner can run it from the note alone.
Reflect
Which test log found the most interesting bug, and what did it teach you about your own code?
Write your answer on paper or in your own notes. Lite never stores what you write.
Go further
- The Python Tutorial (official)
The official, free introduction to Python, written by its developers.