──────────────────────────────────────

65. Weekly Activity Digest Capstone · Turn a Weekly Chore into a Brief

Use a small CSV fixture to specify a repeatable input and output contract.

Lesson files · lessons/automation-capstone/plan-the-digest

Before you start
- Read the named fields of a CSV with DictReader.
- Work out a small report’s expected totals before running it.

By the end
- Write who will read the weekly digest and what the report should tell them.
- Create a two-row CSV and use DictReader to check its column names.

Where you can use it
Agree with a club volunteer on the columns in their weekly export and the numbers they expect to see in the digest.

A club organiser adds up session times every week. The digest gives them the total minutes for each group and counts the rows that were invalid or repeated. Each input row gives a session ID, a group and its minutes.

Start with a small fixture whose totals you can check by hand. When a later report surprises you, use this same fixture to check whether the program read the right columns and added the right values.

Predict
Why use a tiny hand-written fixture?
- You can verify the expected result yourself.
- A small file makes all edge cases disappear.

Worked example (example.py)

import csv
from io import StringIO
fixture = "id,group,minutes\na1,Robots,20\n"
reader = csv.DictReader(StringIO(fixture))
print(next(reader)["group"])


Example result
Robots

Write a sentence in your notes naming the reader and what they need to know. Then make a CSV fixture with the header id,group,minutes and two group records. Write down the expected minutes for each group. Use DictReader to print just the header names, joined with a comma and a space.

Starter program (starter.py)

import csv
from io import StringIO
fixture = "id,group,minutes\na1,Robots,20\n"
reader = csv.DictReader(StringIO(fixture))
print(next(reader)["group"])


Hint
Read reader.fieldnames for the header keys before you loop through records.

Case 1. Try the sample
Input
(No input.)
Expected output
id, group, minutes

Stretch
Write acceptance examples for a repeated ID and a header-only CSV.

Reflect
What information should a reader see if some input rows could not be used?

Check your prediction
You can verify the expected result yourself. A predictable file is easier to reason about and repeat.
