Rerun October without counting it twice
A report that appends doubles a month every time it reruns. Fix it to replace that month, and write the instructions someone else needs to rerun it.
13 min · on a computer
Free course. Your Maestro, an AI, .
Turning manual spreadsheet steps into a Glossary word gives you the same calculation every run, yet how that script writes its output decides whether you can safely run a month a second time. Reruns happen routinely in reporting work: finance sends a corrected October export two days later, an automated scheduler retries after a timeout, or a teammate runs the command again to verify a total.
Look at how naive_report.py updates reports/history.csv. Every time run() totals a month's regions, it opens reports/history.csv in append mode and tacks four new rows onto the end of the file. In the workbench below, rerun_check.py runs September (2026-09) once and October (2026-10) twice. After the second October run, reports/history.csv has 8 rows for October instead of 4, and October's total in history reads 5,600.0 instead of 2,800.0. Every retry adds another copy of the month.
Deleting reports/history.csv or overwriting it from scratch before each run breaks history in the other direction. Because reports/history.csv stores every closed month in one table, wiping the file during an October run erases September's four rows.
To make the report safe to rerun, replace only the rows for the month being run, an operation often called an upsert:
Read the existing history
If
reports/history.csvalready exists, read its rows withcsv.DictReader. When the file does not exist yet on the first run, start with an empty list.Keep rows from other months
Filter the existing rows to keep only those where
monthdiffers from the month passed intorun(). That drops any old rows for the target month while keeping earlier months like September.Add the new rows and sort
Combine the kept rows from other months with the freshly computed region rows for the target month, and sort them by
monthandregion.Rewrite the table
Open
reports/history.csvfor writing withcsv.DictWriter, write the header once, and write the combined rows. Running October once or ten times leaves 4 rows for September and 4 for October.
The full project's report.py uses this same replace-by-month rule on both reports/history.csv and the run log reports/runs.csv, so rerunning a month refreshes its log entry too. If you get stuck filtering out one month's rows before rewriting the file, ask your Maestro.
Write the instructions someone else needs
Code that handles reruns cleanly solves half the problem; the other half is making sure a teammate can rerun any month from source data without reading your Python code. In this course, scheduling is simulated in the sandbox rather than set up on a server, so README.md should also name the exact Glossary word command a monthly scheduler would call.
Each of the five sections in README.md answers one question a colleague will have:
- What you need: what must be installed, and which file must be in
data/under which name? - Run one month: what single command runs one month?
- Where the outputs go: which files does a run write or update?
- How to tell it worked: what three things would you look at to know the run succeeded and the total is right?
- If a file is bad: what happens to the outputs, and what should the colleague do next?
Try it
Run the starter check first to see October counted twice. Then update naive_report.py so rerunning a month replaces that month's rows, and complete README.md.
Once your change in naive_report.py is in place, running rerun_check.py prints History rows for 2026-10: 4 and October total in history: 2800.0 after two October runs. When your README.md is ready, paste it to your Maestro to check that a colleague could follow it from start to finish.