Lesson map
Lesson 04 published. Map, example, EX013.
flowchart TD
N0["Learning outcomes"]
N1["4.1 Why scripts help"]
N2["4.2 CSV and JSON"]
N3["4.3 Processing logs safely"]
N4["4.4 Making output useful"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5
N5 --> EX["Worked example"]
EX --> Q["Exercise EX013"]
Logical drawing
flowchart LR
N0["Learning outcomes"]
N1["4.1 Why scripts help"]
N2["4.2 CSV and JSON"]
N3["4.3 Processing logs safely"]
N4["4.4 Making output useful"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> Q["EX013"]
[Learning outcomes]
|
v
[4.1 Why scripts help]
|
v
[4.2 CSV and JSON]
|
v
[4.3 Processing logs safely]
|
v
[4.4 Making output useful]
|
v
[Synthesis]
|
v
[Worked example] --> [Exercise EX013]
Learning outcomes
You will (1) write simple Python scripts; (2) read CSV and JSON; (3) process logs safely.
4.1 Why scripts help
Technicians repeat checks: which certificates expire soon, which accounts failed to log in, which configurations changed. Short Python scripts using only the standard library make these checks fast and repeatable. Amina writes scripts that read a file, process each record and print a short summary. She keeps them under fifty lines where possible and comments why each step exists. All course data are synthetic. Scripts are tools for careful work; she reads every line and tests each script on small examples before trusting its output.
4.2 CSV and JSON
Data often arrive as CSV, a table with one record per line and commas between fields, or JSON, a structured format with keys and values. Python's csv and json modules read both. Amina uses CSV for lists such as user accounts and certificate inventories, which Rosa can open in a spreadsheet, and JSON for structured exports from devices. She documents the expected fields and checks each record for missing values, reporting how many were skipped rather than ignoring them silently.
4.3 Processing logs safely
Logs are text written by devices and sometimes include text supplied by users, such as usernames. Amina never runs log text as a command and never inserts it into a shell. She treats every field as data, checks it matches the expected format and counts records that do not. She also avoids copying logs to personal devices, keeping them on the institute's lab storage. These habits protect both the analysis and the organisation's data, and they are easy to build from the first script.
4.4 Making output useful
A good script answers a question in a few lines. Amina's scripts print aligned columns with headings, a summary line and any warnings, such as how many records were malformed. She uses clear labels such as "renew now" rather than codes. Rosa will use some scripts after Amina's course, so the output must make sense to someone who did not write the script. Useful output also makes errors visible, because readers can check that the summary matches the detail. She asks Rosa to try each script once.
Worked example
Amina's certificate script reads a CSV of six certificates, skips one malformed row and prints a sorted table with statuses.
Synthetic case. Data are synthetic.
Errors and limits
Silently skipping malformed records can hide important data.
Exercises
- EX013 — CSV versus JSON. When does Amina use CSV rather than JSON?
Show marking criteria
Rubric: 3 = simple tables Rosa opens in spreadsheet;; 2 = tables;; 1 = always;; 0 = none. - EX014 — Malformed records. How should malformed records be handled?
Show marking criteria
Rubric: 3 = counted and reported;; 2 = reported;; 1 = ignored;; 0 = none. - EX015 — Log text rule. Why never pass log text to a shell?
Show marking criteria
Rubric: 3 = may contain user-supplied text;; 2 = unsafe;; 1 = slow;; 0 = none. - EX016 — Output labels. Why use labels such as "renew now"?
Show marking criteria
Rubric: 3 = clear to non-authors;; 2 = clarity;; 1 = shorter;; 0 = none.
Synthesis
Simple, safe scripts reading standard formats make routine checks reliable.
Continue studying — chapter 04 of 18
← Chapter 03 (draft) · Course chapter index · Chapter 05 (draft) →