Boss

Boss: Repair the Archive

The boss
Boss Fight!
Every passing test breaks a shield.

A storm flooded the Guild archive! The lantern delivery ledger survived, but some rows are smudged. Some have words instead of numbers. Some have no island, a negative count, or a missing column. The Archmage needs a clean summary by sunrise.

A tip for short rows

When a row is too short, DictReader fills the missing values with None. The trick (row.get("lanterns") or "") turns both None and a missing key into an empty string "".

Assignment

  1. Use the recipe to define BadRecord, a new kind of Exception.
  2. Write parse_row(row). Here row is a dict from csv.DictReader with the keys date, island and lanterns.
    • Return a tuple (island, count). Strip spaces from the island. Turn the count into an int.
    • Raise BadRecord if the island is empty or missing.
    • Raise BadRecord if lanterns is missing or not a whole number. Chain it from the ValueError.
    • Raise BadRecord if the count is negative.
  3. Write repair_archive(csv_path, json_path):
    • Read the CSV with csv.DictReader. Call parse_row on every row.
    • For good rows, add up the lanterns for each island.
    • For bad rows, collect their line numbers in the file. The header is line 1, so the first data row is line 2.
    • Save this summary to json_path as JSON. Also return it.
{"totals": {"Amber Isle": 7, ...},
 "good": 3,
 "bad_rows": [3, 5, ...]}