Chapter 3 — Programming foundations
Lesson map
Lesson 03 published. Map, example, EX007.
flowchart TD
N0["Learning outcomes"]
N1["3.1 Writing simple scripts"]
N2["3.2 Reading structured data"]
N3["3.3 Small checks for ledger data"]
N4["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4
N4 --> EX["Worked example"]
EX --> Q["Exercise EX007"]
Logical drawing
flowchart LR
N0["Learning outcomes"]
N1["3.1 Writing simple scripts"]
N2["3.2 Reading structured data"]
N3["3.3 Small checks for ledger data"]
N4["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> Q["EX007"]
[Learning outcomes]
|
v
[3.1 Writing simple scripts]
|
v
[3.2 Reading structured data]
|
v
[3.3 Small checks for ledger data]
|
v
[Synthesis]
|
v
[Worked example] --> [Exercise EX007]
Learning outcomes
You will (1) write simple scripts; (2) read structured data; (3) build small checks for ledger data.
3.1 Writing simple scripts
Lucas writes Python scripts using only the standard library. His first reads a text file of booking records, counts bookings per machine and prints the totals. He learns variables, loops, conditions and functions by solving real tasks. Ms Adebayo asks him to add comments explaining why, not just what, each part does, so a colleague can maintain it. Scripts save time on repetitive checks and make them consistent. A check written as a script runs the same way every time, unlike a manual check that depends on someone's attention on a busy afternoon.
3.2 Reading structured data
Ledger tools often output JSON, a structured text format of keys and values. Lucas reads JSON with Python's json module and extracts block numbers, transaction counts and timestamps. He handles missing fields carefully, using defaults and printing warnings rather than crashing. He also reads CSV files exported from the booking system. Structured data is easier to check than free text because fields have names. He learns to validate data before using it, checking types and ranges, so that one bad record cannot produce misleading totals that someone might act on without noticing the problem.
3.3 Small checks for ledger data
Lucas writes a script that compares the number of booking credits spent in the booking system with the number recorded on the ledger for the same day. If they differ, it lists the bookings involved. On a synthetic day of 64 bookings, both show 64. He then alters one test record and confirms the script reports a mismatch. Testing a check with known bad data proves it works. Ms Adebayo notes that such small scripts, kept in version control and reviewed, form the backbone of practical security operations in small organisations like the makerspace.
Worked example
Lucas's daily comparison matches 64 bookings in both systems and correctly reports a mismatch when one test record is altered.
Synthetic case. The scripts and booking data are synthetic.
Errors and limits
A check never tested with known bad data may silently fail to detect anything.
Exercises
- EX007 — Comment purpose. What should comments explain? Rubric: 3 = why each part does what it does;; 2 = why;; 1 = vague;; 0 = none.
- EX008 — Missing fields. How are missing JSON fields handled? Rubric: 3 = defaults and warnings rather than crashing;; 2 = warnings;; 1 = crash;; 0 = none.
- EX009 — Synthetic day result. How many bookings matched on the synthetic day? Rubric: 3 = 64;; 2 = sixty-four;; 1 = 46;; 0 = none.
Synthesis
Simple, tested scripts that read structured data make checks reliable.
Continue studying — chapter 03 of 30
← Chapter 02 (draft) · Course chapter index · Chapter 04 (draft) →