Chapter 4 — Programming I
Lesson map
Lesson 04 published. Map, example, EX010.
flowchart TD
N0["Learning outcomes"]
N1["4.1 Why program"]
N2["Lab — Leaver accounts"]
N3["4.2 Reading the output"]
N4["4.3 Structuring a script"]
N5["4.4 Testing"]
N6["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6
N6 --> EX["Worked example"]
EX --> Q["Exercise EX010"]
Logical drawing
flowchart LR
N0["Learning outcomes"]
N1["4.1 Why program"]
N2["Lab — Leaver accounts"]
N3["4.2 Reading the output"]
N4["4.3 Structuring a script"]
N5["4.4 Testing"]
N6["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6 --> Q["EX010"]
[Learning outcomes]
|
v
[4.1 Why program]
|
v
[Lab — Leaver accounts]
|
v
[4.2 Reading the output]
|
v
[4.3 Structuring a script]
|
v
[4.4 Testing]
|
v
[Synthesis]
|
v
[Worked example] --> [Exercise EX010]
Learning outcomes
You will (1) write small scripts that read account lists; (2) compare sets of accounts; (3) produce clear, repeatable reports.
4.1 Why program
Comparing account lists by eye works for ten accounts but not for thousands across 140 applications. A short program reads each list, compares it with the HR record of current staff and reports accounts with no matching employee. It does the same check identically every time, and its output can be saved as evidence. Sana writes her first scripts in Python using only the standard library, so they run on any council machine without installing extra packages, which the council's change rules would otherwise require approval for.
Lab — Leaver accounts
"""Compare a synthetic HR staff list with an application's active accounts (stdlib only)."""
hr = {"aali", "bchen", "cdiaz", "dfox", "egray", "fhill", "gking", "mlee"}
app = ["aali", "BChen", "cdiaz ", "dfox", "egray", "fhill", "gking", "jdoe", "kpatel"]
norm = {a.strip().lower() for a in app}
print("active accounts:", len(norm), " current staff:", len(hr))
print("no current employee:", sorted(norm - hr))
print("employee without account:", sorted(hr - norm))
Run with python3 lab_leavers.py. Verified output:
active accounts: 9 current staff: 8
no current employee: ['jdoe', 'kpatel']
employee without account: ['mlee']
4.2 Reading the output
The synthetic HR list contains eight current staff, and the application export contains nine active accounts. The script finds two accounts, jdoe and kpatel, with no matching current employee: likely leavers whose access was never removed. It also finds one employee, mlee, without an account, who may be new and still waiting. Sana does not disable anything herself; she reports the findings to the application owner, who checks each case, because a script can only show a mismatch, not explain it, and some mismatches have innocent causes such as name changes.
4.3 Structuring a script
Sana structures each script in three parts: reading inputs, computing results and printing a report. Each part is a small function with a clear name. Inputs are read from files named at the top, so the same script works on any export with the same format. Keeping computation separate from reading and printing makes it easy to test with small examples. This simple structure means a colleague can understand and rerun the script months later, when Sana has moved on to other parts of the project.
4.4 Testing
A test gives a function a small, known input and checks the output against an answer worked out by hand. For the leaver check, Sana tests an exact match, a case difference in usernames, an account with trailing spaces and an empty list. The case and spacing tests reveal that her first version treated JDoe and jdoe as different people. Fixing that before running the script on real exports prevents false alarms that would waste application owners' time and reduce their trust in the results.
Worked example
The leaver script reports two active accounts with no current employee (jdoe, kpatel) and one employee without an account (mlee); owners confirm the first two are leavers.
Synthetic case. Accounts and staff are synthetic.
Errors and limits
Disabling accounts directly from a script's output, without owner checks, risks locking out staff whose records differ for innocent reasons.
Exercises
- EX010 — Orphan count. Which accounts does the lab flag as having no current employee? Rubric: 3 = jdoe and kpatel;; 2 = two;; 1 = vague;; 0 = none.
- EX011 — Missing account. Which employee has no account in the lab? Rubric: 3 = mlee;; 2 = one;; 1 = vague;; 0 = none.
- EX012 — Case bug. What did the case test reveal? Rubric: 3 = JDoe and jdoe treated as different;; 2 = case;; 1 = vague;; 0 = none.
Synthesis
Small, tested scripts make account comparisons repeatable and trustworthy across many applications.
Continue studying — chapter 04 of 50
← Chapter 03 (draft) · Course chapter index · Chapter 05 (draft) →