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

43. Reliable Automation · Give Reports a Predictable Date

Generate a dated output name from supplied data and validate that date.

Lesson files · lessons/reliable-automation/dated-output

Before you start
- Read an ISO date with date.fromisoformat().
- Build a filename from a value using an f-string.

By the end
- Generate a report filename for a valid supplied date.
- Check the error message for a date outside the calendar.

Where you can use it
Name each weekly digest from its reporting date so rerunning an old week targets the same filename.

A repeated job needs a predictable report name. Pass the run date as data so a test can repeat the same job later. If writing a new file, exclusive creation mode x can protect an existing report. A person can choose a different output path if it exists.

Predict
Why pass a fixed date during a test?
- The filename is repeatable.
- It makes the program run faster.

Worked example (example.py)

from datetime import date
stamp = date.fromisoformat("2026-11-20")
print(f"report-{stamp}.csv")


Example result
report-2026-11-20.csv

Read an ISO date. Print digest-YYYY-MM-DD.txt for a valid date, or Invalid date. for a bad one.

Starter program (starter.py)

from datetime import date
stamp = date.fromisoformat("2026-11-20")
print(f"report-{stamp}.csv")


Hint
Catch ValueError from date.fromisoformat before printing the filename.

Case 1. Try the sample
Input
2026-11-20
Expected output
digest-2026-11-20.txt

Case 2. Impossible date
Input
2026-02-30
Expected output
Invalid date.

Stretch
Write a short runbook for running a weekly report and keeping prior files.

Reflect
What is the expected behaviour if a report is run twice for one date?

Check your prediction
The filename is repeatable. A fixed input lets you compare the expected filename every time.
