Level 5 / Project 12 - Fail Safe Exporter¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | Browser |
Estimated time: 85 minutes
Focus¶
- atomic writes and export integrity
Why this project exists¶
This project gives you level-appropriate practice in a realistic operations context. Goal: run the baseline, alter behavior, break one assumption, recover safely, and explain the fix.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-5/12-fail-safe-exporter
python project.py --input data/sample_input.json --output data/exported.json --format json
python project.py --input data/sample_input.json --output data/exported.csv --format csv
pytest -q
Expected terminal output¶
Exported 4 records to data/exported.json (atomic write)
Exported 4 records to data/exported.csv (atomic write)
6 passed
Expected artifacts¶
data/exported.jsonordata/exported.csv- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a
--validateflag that checks each record has required fields before exporting. - Add a backup: before overwriting, copy the existing file to
.bak. - Add a
--dry-runflag that validates and reports but does not write. - Re-run script and tests.
Break it (required) — Core¶
- Pass an output path to a read-only directory (or a path with invalid characters).
- Pass input data where records have inconsistent keys (some missing columns).
- Capture the first failing test or visible bad output.
Fix it (required) — Core¶
- Catch
OSError/PermissionErrorduring atomic write and report clearly. - Pad missing keys with empty strings during CSV export.
- Add tests for write failures and inconsistent records.
- Re-run until output and tests are deterministic.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
atomic_write_jsonwrite to a.tmpfile and then rename? - What happens if the process crashes between writing
.tmpand renaming? - How does
atomic_write_csvdetermine CSV headers from the records? - Where do you see atomic writes in production (databases, log rotation, config updates)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
- Classes and Objects
- Errors and Debugging
- Files and Paths
- Types and Conversions
- Quiz: Classes and Objects
| ← Prev | Home | Next → |
|---|---|---|