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

68. Weekly Activity Digest Capstone · Ship the Weekly Activity Digest

Deliver an automation that reads an export, validates records and writes a reproducible report.

Lesson files · lessons/automation-capstone/weekly-activity-digest

Before you start
- Validate CSV rows and track the first valid use of each ID.
- Keep a calculation separate from an argument parser and report display.
- Use exclusive file creation to protect a report.

By the end
- Produce the sorted digest and its invalid and duplicate counts for the three built-in cases.
- Run the tool with a local CSV path and verify that a second write preserves the first report.

Where you can use it
Replace a club’s manual weekly tally with a command that checks the export and creates a report the organiser can keep.

Use the short brief, calculation and file test from this module to build one tool. The starter gives you three fictional fixtures, an argument parser and a loop that reads the selected fixture. Begin by getting the report right in Studio.

The local version should produce the same report from a real CSV file. Give both sources to the same report function so you can test the rules apart from opening files and displaying the result.

Predict
Does a header-only file mean the report is broken?
- Yes. Every valid CSV must have data rows.
- No. It should report zero accepted rows.

Worked example (example.py)

def format_counts(invalid, duplicates):
    return f"Invalid: {invalid}\nDuplicates: {duplicates}"

print(format_counts(1, 2))


Example result
Invalid: 1
Duplicates: 2

In Studio, enter sample, empty or invalid to choose a fixture. Print Activity digest, then group totals as Group: N min in alphabetical order, then the Invalid: and Duplicates: counts. Require a nonempty ID and group and a non-negative whole number of minutes. Keep the first valid row for each ID.

Save your finished script as main.py beside activities.csv from the workbook. In a terminal in that folder, run python3 main.py activities.csv, or use py on Windows. Add --output digest.txt to create a report. Run the same save command twice and check that the first report keeps its contents. Record the command and the result for a new reader.

Starter program (starter.py)

import argparse
import csv
import io

FIXTURES = {
    "sample": "id,group,minutes\nr1,Robots,20\ng1,Garden,10\nr2,Robots,15\nr1,Robots,20\nx,Chess,oops\n",
    "empty": "id,group,minutes\n",
    "invalid": "id,group,minutes\nr1,Robots,-1\nr1,Robots,0\n,Chess,10\n",
}

parser = argparse.ArgumentParser(description="Build a weekly activity report")
parser.add_argument("csv_path", nargs="?")
parser.add_argument("--output")
args = parser.parse_args()
rows = csv.DictReader(io.StringIO(FIXTURES[input().strip()]))
print("Activity digest")
for row in rows:
    print(row["group"])


Starter input
sample

Hint
Use DictReader over StringIO in Studio and an opened text file locally. Check the header, then validate each row before adding its ID to the seen set. Keep formatting in one place.

Case 1. Try the sample
Input
sample
Expected output
Activity digest
Garden: 10 min
Robots: 35 min
Invalid: 1
Duplicates: 1

Case 2. Header-only export
Input
empty
Expected output
Activity digest
Invalid: 0
Duplicates: 0

Case 3. Valid zero after rejected row
Input
invalid
Expected output
Activity digest
Robots: 0 min
Invalid: 2
Duplicates: 0

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Adapt the tool for a reading log, kit loans or your own weekly task. Write one new case and name one rule from the activity digest you can reuse.

The ZIP includes added practice files for this lesson. Open its lesson folder and read the included instructions.

Reflect
Give the exact command you tested, one normal case, one invalid case, and a design decision that makes the tool safe to repeat.

Check your prediction
No. It should report zero accepted rows. An empty period can be a legitimate input.
