
Boss
Boss: Repair the Archive

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
- Use the recipe to define
BadRecord, a new kind ofException. - Write
parse_row(row). Hererowis a dict fromcsv.DictReaderwith the keysdate,islandandlanterns.- Return a tuple
(island, count). Strip spaces from the island. Turn the count into anint. - Raise
BadRecordif the island is empty or missing. - Raise
BadRecordiflanternsis missing or not a whole number. Chain itfromtheValueError. - Raise
BadRecordif the count is negative.
- Return a tuple
- Write
repair_archive(csv_path, json_path):- Read the CSV with
csv.DictReader. Callparse_rowon 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_pathas JSON. Also return it.
- Read the CSV with
{"totals": {"Amber Isle": 7, ...},
"good": 3,
"bad_rows": [3, 5, ...]}- Use the recipe to define
BadRecord, a new kind ofException. - Write
parse_row(row). Hererowis a dict fromcsv.DictReaderwith the keysdate,islandandlanterns.- Return a tuple
(island, count). Strip spaces from the island. Turn the count into anint. - Raise
BadRecordif the island is empty or missing. - Raise
BadRecordiflanternsis missing or not a whole number. Chain itfromtheValueError. - Raise
BadRecordif the count is negative.
- Return a tuple
- Write
repair_archive(csv_path, json_path):- Read the CSV with
csv.DictReader. Callparse_rowon 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_pathas JSON. Also return it.
- Read the CSV with
{"totals": {"Amber Isle": 7, ...},
"good": 3,
"bad_rows": [3, 5, ...]}main.py Python sleeping
Press Run to try your code, then Check to test it.