◇ OpenCyber Academy
Technologist · Chapter 5 · English

Chapter 5 — Python scripting basics

Open, free learning for anyone who wants to protect people and technology. No account is required to read this text or its worked answers.

Chapter 5 — Python scripting basics

Lesson map

Lesson 05 published. Map, example, EX013.

flowchart TD
  N0["Learning outcomes"]
  N1["5.1 First scripts"]
  N2["5.2 Handling errors"]
  N3["5.3 Dates and numbers"]
  N4["5.4 Structure for reuse"]
  N0 --> N1 --> N2 --> N3 --> N4
  N4 --> EX["Worked example"]
  EX --> Q["Exercise EX013"]

Logical drawing

flowchart LR
  N0["Learning outcomes"]
  N1["5.1 First scripts"]
  N2["5.2 Handling errors"]
  N3["5.3 Dates and numbers"]
  N4["5.4 Structure for reuse"]
  N0 --> N1 --> N2 --> N3 --> N4 --> Q["EX013"]
[Learning outcomes]
    |
    v
[5.1 First scripts]
    |
    v
[5.2 Handling errors]
    |
    v
[5.3 Dates and numbers]
    |
    v
[5.4 Structure for reuse]
    |
    v
[Worked example] --> [Exercise EX013]

Learning outcomes

You will (1) write Python scripts for data tasks; (2) handle files and errors; (3) structure scripts for reuse.

5.1 First scripts

Ravi writes Python scripts that read contractor portal exports in CSV format using the csv module, count rows and summarise jobs by status. He uses functions for each step: reading, checking and summarising. Ms. Moreau reviews his code and asks for clearer names, such as count_jobs_by_status instead of f2. She explains that clear names make scripts readable months later, when the author has forgotten details and a colleague needs to understand them quickly. Ravi renames every function in his scripts that week.

5.2 Handling errors

Contractor exports sometimes arrive with missing columns or extra blank lines. Ravi's first script crashes on such files. He adds checks that confirm expected columns are present and skip blank lines, with clear messages explaining any problem. When a column is missing, the script stops and reports which one, rather than producing wrong results silently. He learns that stopping loudly is better than continuing quietly with bad data. He adds a small set of broken test files to his repository so that these behaviours can be checked whenever the script changes.

5.3 Dates and numbers

Repair data contain dates in several formats, because contractors use different systems. Ravi uses the datetime module to parse each expected format and records any value that matches none. He also converts costs from text to numbers carefully, handling currency symbols and commas. He finds that one contractor uses day-month order while another uses month-day, which had caused completion dates to be misread in earlier reports. Ravi reports this to the steward, who asks both contractors to confirm their formats, and earlier reports are corrected with a note explaining the change.

5.4 Structure for reuse

Ravi organises his scripts into a module with shared functions for reading files, parsing dates and writing results, and separate small scripts for each task. He writes simple tests for the date parser using examples from each contractor. Ms. Moreau notes that reusable functions mean a fix, such as a new date format, needs making in one place, reducing the chance that different scripts treat the same data differently. When a third contractor joins later, Ravi adds its date format to the shared parser and every script handles it correctly at once.

Worked example

Ravi writes function-based scripts with clear names, stops loudly on missing columns, finds day-month and month-day date confusion between contractors and organises shared functions with tests.

Synthetic case. Exports are synthetic.

Errors and limits

Scripts that continue quietly on bad input produce wrong results.

Exercises

Continue studying — lesson 05 (chapter 05 of 30)

← Chapter 04 · Course chapter index · Chapter 06 →

Continue to the next authored lesson.