Organize a Small Read–Compute–Write Program

Give input, reusable calculations, the runner, and generated output distinct roles, then run and verify the whole workflow twice.

A small program becomes easier to inspect when each file has one clear role. The measurement report now has enough parts to justify a simple layout: saved input, reusable calculations, one runner, and generated output.

Use Only the Structure the Program Needs

This layout is sufficient:

measurement_report/
  data/
    measurements.txt
  reports/
    summary.txt
  measurement_tools.py
  run_report.py

The folders and files answer four practical questions:

PathRole
data/measurements.txtinput supplied to the program
reports/summary.txtoutput produced by a run
measurement_tools.pyreusable parsing and computation
run_report.pyworkflow that connects paths, input, computation, and output

The structure matters because the roles differ. It is not a rule that every Python program needs four files. Add a file or folder when it owns a real responsibility, not merely to make a small program look larger.

Q1. Place the reusable calculation

Where should a function that calculates the mean of an already parsed list of readings belong?

Choose one

Select one choice, then check.

HintSeparate values from locations

The function receives parsed readings. It does not need to know where they came from or where their result will be written.

SolutionKeep computation in the reusable module

Put the function in measurement_tools.py. The runner can import it, and a check can call it directly without creating any files.

Not attempted
Review

Not marked done.

Keep Reusable Work Independent of Paths

measurement_tools.py accepts ordinary strings and values. It does not open a particular file:

The functions state their inputs through parameters and return their results. parse_measurements does not assume a filename. build_report produces text but does not decide where to save it. This separation lets us inspect each stage with a small direct call.

The mean function keeps the Chapter 4 contract: no available readings produce None. The complete runner will have accepted readings, so formatting the numerical mean is valid there. A more general report function would need to state how it formats the legitimate None result.

Let the Runner Own the Workflow

run_report.py knows the project paths and the order of work:

Read the runner from top to bottom:

  1. name the input and output locations;
  2. reject missing supplied input rather than creating it;
  3. read and parse the UTF-8 text;
  4. compute the report through reusable functions;
  5. create the generated-output directory;
  6. write and read back the report;
  7. verify the exact saved text.

This is the chapter's complete path. The runner owns sequence and locations; the imported module owns reusable transformations.

Run Again and Repair from Evidence

Run the unchanged program a second time. Because the report uses write mode, the second run replaces summary.txt with the same derived text instead of adding a duplicate report. The exact read-back assertion should pass again.

Now suppose the runner contains a path typo:

input_path = Path("data") / "measurement.txt"

The supplied file is named measurements.txt, with a final s. The useful evidence is not merely “the file is missing.” Inspect the location the runner actually requested:

The resolved path and False result identify the mismatch. Repair the filename, rerun the complete workflow, and require the read-back assertion to pass. Do not create an empty measurement.txt; that would hide the missing-input defect and produce a different experiment.

Q2. Respond to a missing path honestly

The reports/ folder is absent, and data/measurements.txt is also absent. What should the runner do?

Choose one

Select one choice, then check.

HintAsk who owns each artifact

The program owns the report it generates. It does not own the measurements another source was expected to supply.

SolutionCreate only generated-output locations

Stop with a useful missing-input error. Once valid input exists, create reports/ before writing the generated summary.

Not attempted
Review

Not marked done.

Preserve the Same Roles in Browser Python

Browser Python uses a temporary in-memory file system, but the program roles do not change. A complete browser example may create its own supplied fixture at the beginning because a fresh runtime starts without project files. That setup is separate from the workflow being practised.

The following exercise keeps the program in one editor so it can run here. The commented setup creates the input fixture. The code below it must read that input, write a derived report elsewhere, and verify the saved result.

Q3. Complete and verify the workflow

Complete the runner section. It must keep the supplied input unchanged, create the report directory, save the derived report, and verify the exact read-back.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintFollow the workflow in order

Read and number the lines, then parse them. Build report with the supplied function so the count, rejection, mean, classes, and groups stay together. Then create output_path.parent, write the text, read it back into saved, and assert saved == report.

SolutionKeep input and output roles separate
Not attempted
Review

Not marked done.

The small program now has a visible chain of ownership: supplied bytes become decoded text and parsed values; reusable functions compute a report; one runner owns paths and sequence; generated text is written and checked. Chapter 9 can build on this structure by giving settings and results explicit record types without changing the workflow's boundaries.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.