The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← CS 101 — Programming Foundations with Python

Weeks 15–16

Modules, reproducibility and an integrated estimator

Learning objectives

  • Explain and trace the mechanisms in modules, reproducibility and an integrated estimator.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

A useful application separates its domain rules, input adapters and user interface. Place estimate and aggregation functions in an importable module; put command-line interaction behind a main entry point. Importing a calculation module should not prompt for input or overwrite files. This separation lets tests load the rules without starting the application.

Reproducibility means another person can recreate the execution conditions and obtain a justified result. Record the Python version, dependency versions, input format and commands. A virtual environment isolates project packages from other projects, but is not a security boundary. If the program uses only the standard library, say so; do not add dependencies simply to appear sophisticated. When a library is needed, verify its maintained documentation and record compatible versions rather than installing arbitrary packages with similar names.

Integration tests exercise a workflow across boundaries: parse a fictional CSV, validate it, calculate totals, produce output and read that output back. Include an invalid batch and confirm that it leaves no misleading success artifact. An operational README should state what the tool handles and what remains out of scope. Evaluate change requests through those boundaries. Supporting fractional hours requires a representation and rounding decision; supporting multiple currencies requires currency-aware aggregation; supporting several users requires identity, authorization and concurrency design. A small coherent program is a better result than a larger program whose output cannot be explained.

Worked reasoning Running the file prints a two-field summary with transactions 2 and net_cents 16250. Importing it defines the functions without printing. The guard separates execution as a script from reuse as a module. The function receives already validated records; a real coordinator must call the parser first. The output is a Python dictionary representation, so a portable file export should instead choose a documented format such as JSON or CSV.

Extended teaching and laboratory

An integrated program should make data flow and failure behavior visible. The entry point reads configuration and arguments, calls parsing and validation, computes the result, and exports a documented format. Pure functions should not silently depend on the current working directory or print intermediate output. A command-line program can return an exit status while a reusable function returns a structured result.

Reproducibility requires more than setting a random seed. Record the input version, program version, policy choices and dependencies. Avoid including unnecessary personal data in diagnostics. A run identifier can connect a summary to its source without embedding the entire source file in logs.

Worked problem Design a work-order estimator that can be imported in a test, run from a terminal, and retried after export failure without charging a customer twice.

Step-by-step reasoning 1. Keep estimate(records, policy) pure: it returns totals and validation findings without performing payments or printing.

  1. Put argument parsing and process exit behavior behind the script entry-point guard. Importing the module must not launch a run.
  1. Write a result manifest with input checksum, policy identifier, output format and program version. Export retry is a separate operation from any payment workflow.
  1. If a payment action is later added, use a stable operation key and durable action ledger. Rerunning a calculation does not itself establish idempotent payment behavior.

Apply the idea

Design a work-order estimator that can be imported in a test, run from a terminal, and retried after export failure without charging a customer twice.

A run succeeds on one machine but reads a different rates.csv on another. Explain the likely dependency and redesign the invocation so the discrepancy is diagnosable.

Read the worked reasoning

A relative filename may have resolved against different working directories. Require an explicit input path or a documented configuration root, resolve and report the chosen source, and include its checksum in the manifest. Keep the report free of unnecessary confidential paths if shared publicly. Tests should run from a temporary working directory to reveal hidden filesystem assumptions.

Check understanding

Why put interactive behavior behind a main guard?

  1. So importing reusable functions does not unexpectedly run the interface
    Feedback

    Import-time side effects make reuse and tests harder.

  2. So type hints become enforced
    Feedback

    The guard does not perform runtime type validation.

  3. So dependency versions are automatically recorded
    Feedback

    Reproducibility needs explicit environment documentation.

Further reading

Read modules and object basics before integrating the estimator. Compare a function-based design with a small class that preserves a stated invariant. Work the recursion examples with explicit base cases and trace the call stack. Advanced GUI and later data-structure chapters remain optional extensions. Then study the original assigned documentation as a language reference and complete the worked and transfer problems.

My learning →