Python Behavior Lab
Prerequisites and supported environment
Section titled “Prerequisites and supported environment”Finish the three preceding Python lessons before you start this one, and work inside the Ubuntu x86-64 course VM with Python 3, tar, and sha256sum all available. Everything here is local, and the only thing that needs internet access is the download itself. Keep that verified archive completely untouched, because it’s your reset point and you’ll probably want it.
Download python-behavior-v1.0.0.tar into ~/underdunn-course/downloads, where its expected SHA-256 is:
531448744b567edabd6acd8a7a6fca9f5fd7d8f7d185295228574ec0fa49fd61cd ~/underdunn-course/downloadssha256sum python-behavior-v1.0.0.tarmkdir -p ~/underdunn-course/labs/python-behaviortar -xf python-behavior-v1.0.0.tar -C ~/underdunn-course/labs/python-behaviorcd ~/underdunn-course/labs/python-behavior/python-behavior-v1.0.0find . -maxdepth 2 -type f -printf '%P\n' | sortYou should see behavior.py and values.txt come back from that. And stop right there if the digest differs, or if either of those files is missing.
Milestone 1: map and predict
Section titled “Milestone 1: map and predict”Begin with where the input path enters the program and where the final result is printed. Then work inward through the operations connecting them. The earlier lessons gave you the pieces: an argument supplies text, opening a file supplies lines, conversion supplies numbers, and a condition determines which numbers affect state. Your map should show the order this particular program uses, rather than assuming it matches an earlier example.
Read behavior.py and values.txt, and work out which lines reach the count update. A quick scratch trace may help, but no completed table is required. Then run:
python3 behavior.py values.txtCompare the selected count with the comparison and update in the source.
Milestone 2: check the path through the loop
Section titled “Milestone 2: check the path through the loop”Pick a selected value and a line that leaves the count unchanged. Follow each
through stripping, conversion and the condition. If your predicted total differs
from the output, use nl -ba values.txt to find the first line where your count diverges.
A line skipped before conversion never becomes a number. A valid number rejected by the comparison reaches that test but leaves the count alone. Keep those paths distinct.
Milestone 3: observe a controlled failure
Section titled “Milestone 3: observe a controlled failure”Add an invalid line to a copy of the input, and then explain the traceback that comes back. Don’t modify the original file, since it’s the thing everything else is measured against.
cp values.txt values-invalid.txtprintf 'not-a-number\n' >> values-invalid.txtpython3 behavior.py values-invalid.txtprintf 'status=%s\n' "$?"Check the final exception, the first frame inside behavior.py itself, the input line responsible for it, and the exit status. Then remove values-invalid.txt, and nothing else in that directory.
Keep the two line numbers distinct. The traceback locates an operation in the Python source; your numbered input identifies the data that reached that operation. Use the two line numbers to find the failing operation and its input. A conversion failure establishes that this text could not be interpreted as the requested number. It does not establish that the later comparison ran or that the program completed its final summary.
Milestone 4: verify the result
Section titled “Milestone 4: verify the result”Enter the comparison threshold from the source and the selected count you checked with the original input.
Layered hints
Section titled “Layered hints”Hint 1: strategy
Find the comparison constant first, then count only the values whose branch actually reaches the state update.Hint 2: evidence
Trace the loop once per input line, and check the conversion before you check the comparison.Hint 3: explicit action
Openvalues.txt and behavior.py side by side in Text Editor, complete one trace row per input line, and then confirm the whole thing with python3 behavior.py values.txt.Common failures
Section titled “Common failures”- The checksum doesn’t match: delete only that download, fetch the pinned link again, and recheck it. Don’t extract a mismatched archive.
FileNotFoundError: runpwdandls -la, and then check the supplied relative path against them.- Your count differs from the output: find the first trace row where your prediction and the branch disagree, and start there.
- The original input got edited: use the reset below rather than trying to recreate it from memory.
Check your result
Section titled “Check your result”The checker should accept the threshold and selected count. If your count differs, trace the first input line where the branch or count surprises you. The invalid-input run should fail at conversion, before the final summary.
Reset and cleanup
Section titled “Reset and cleanup”cd ~/underdunn-course/labs/python-behaviorrm -rf -- python-behavior-v1.0.0tar -xf ~/underdunn-course/downloads/python-behavior-v1.0.0.tar -C .For final cleanup, run only the first two lines, and keep the verified archive and any working files you want.