Skip to content
Start free

From a list of clicks to one script

Turn the five manual steps someone did in a spreadsheet each month into one function you can run for any month.

2 parts · 4 and 9 min · part 2 on a computer

Free course. Your Maestro, an AI, .

Before any code existed for this project, someone built the monthly sales report by hand in a spreadsheet. To remember the routine from one month to the next, they wrote a five-step checklist in steps.md: open the month's , delete rows dated outside that month, build a summing amount by region, copy the region totals into the History sheet, and add them up for the email to finance.

Having steps.md open beside the spreadsheet helps a person remember the order, but a written checklist still depends on human attention every run. If an October row slips into September's export and you skip step 2 on a busy afternoon, the pivot table quietly includes it. Even when a monthly file has only 8 rows, like data/sales_2026-09.csv and data/sales_2026-10.csv, turning those clicks into a pays off: the code runs the exact same filter and addition every month, lives in version control, and can be checked against finance's closed numbers.

Each manual step in steps.md maps directly to a part of month_totals(month) in month_totals.py:

  1. Open the month's CSV

    In the spreadsheet, someone opened the file from a menu. In month_totals.py, csv.DictReader opens f"data/sales_{month}.csv" and reads the file into rows, a list where each order is a dictionary. This first step is already in place.

  2. Delete rows dated outside the month

    By hand, someone scanned the date column and deleted stray rows. In code, a list comprehension keeps only dictionaries where r["order_date"] starts with month + "-" using .startswith(...), so out-of-month rows are dropped every run.

  3. Sum amount by region

    A spreadsheet pivot table groups by region and sums the amount column. In Python, loop over the filtered rows, convert r["amount"] with float(...), and add it to that region's running total in a dictionary using .get(r["region"], 0).

  4. Hand off the region totals

    Instead of highlighting four cells and pasting them into a History sheet by hand, include the dictionary of regional sums in the value month_totals(month) returns so another script or test can read it directly.

  5. Compute the grand total

    Instead of re-entering the regional numbers on a calculator for the finance email, call sum(...) on your regional dictionary's .values() and return that grand total in the same dictionary.

Notice where month_totals(month) stops: it computes and returns the totals without sending the email to finance. Keeping calculation separate from delivery lets you call month_totals("2026-09") and month_totals("2026-10") in a loop whenever you test, without emailing finance on every run. If you are unsure what belongs inside or outside the function, ask your Maestro.

Check yourself: Each monthly file in this project has only 8 rows, and steps.md already lists all five steps clearly. Why replace the manual spreadsheet routine with month_totals(month) returning a dictionary of totals?

A good place to stop. Up next: part 2 of 2 · 9 min · on a computer

Try it

Read steps.md in the workbench below, then open month_totals.py. Complete month_totals(month) so it filters rows to that month (step 2), sums amount by region (step 3), and returns a dictionary with the regional totals and the grand total (steps 4 and 5). Name each step from steps.md in a comment, then run uv run month_totals.py. If your totals differ or a dictionary step trips you up, ask your Maestro.

Your turn

Read steps.md. Complete month_totals(month) so it keeps only that month's rows, totals amount by region, and returns the totals and the grand total. You're done when the script prints correct totals for September and October and each step in steps.md is named in a comment.

Check what uv run month_totals.py prints for both months against these totals:

  • 2026-09: East 650.0, North 1200.0, South 800.0, West 350.0, and a grand total of 3000.0.
  • 2026-10: East 710.0, North 800.0, South 810.0, West 480.0, and a grand total of 2800.0.

Confirm that your comments in month_totals.py account for all five steps in steps.md (with step 4 handled by returning the regional totals). Next, you will write those totals to reports/history.csv so rerunning a month replaces its rows instead of doubling them.