F05 · PREDICT · DEBUG · VERIFY LOCALLY
F05 Save and Restore: Why Did Restart Lose the Data?
Data disappears after exit, and starting from another directory seems to open another library. Loading a damaged file as an empty list even overwrites it on the next save.
Back to the practice route →The bug and the local contract
def load_documents(path):
try:
with open(path, encoding="utf-8") as stream:
return json.load(stream)["documents"]
except Exception:
return []
- Implement load_documents and save_documents. Resolve defaults from a fixed project location and accept explicit paths. Initialize only missing files. Validate UTF-8, fields and schemaVersion=1.
- Report empty files, invalid JSON, bad fields, incompatible versions and read errors instead of pretending they are empty collections. Write a same-directory temporary file and replace; failures preserve the old file, memory and dirty state.
- The page is a controlled model with no real disk I/O. Actual tests must restart a second process, change directories and inject reproducible failures in temporary directories. Full power-loss durability is not promised.
Use fictional document metadata. The web model does not execute arbitrary code.
01 · CONTROLLED MODEL
Predict, then inspect each step
This models only the selected preset. It does not execute your Python draft or read/write computer files. One matching case does not establish correctness for all inputs.
This checks modeled state only. The real project must also test fields, UTF-8, explicit paths, temporary cleanup, subprocess restarts and failure injection; full power-loss recovery is not promised.
Unchecked or invalidated
Before
{
"files": {
"/project/data.json": "{\"schemaVersion\":1,\"documents\":[{\"id\":\"doc_00000000000000000000000000000001\",\"title\":\"Notes\",\"category\":\"course\",\"tags\":[\"study\"]}]}"
},
"memory": [
{
"id": "doc_00000000000000000000000000000001",
"title": "Notes",
"category": "course",
"tags": [
"study"
]
},
{
"id": "doc_00000000000000000000000000000002",
"title": "New Notes",
"category": "course",
"tags": [
"study"
]
}
],
"dirty": true,
"cwd": "/project",
"projectPath": "/project/data.json"
}Model result
Step through the trace to reveal the result
Progressive learning hints
Hints are fixed teaching content. Their use is recorded.
Hint 1
Check the running file, working directory and resolved path. Data in the current process does not prove recovery in the next one.
Hint 2
Handle missing files separately. Report other errors and block automatic saving; avoid except Exception: return [].
Hint 3
Write a same-directory temporary file before replacing. On failure, clean up your temporary file and retain dirty state. Inject replacement failure and assert unchanged original bytes.
My Python draft
Save and export your draft here. Code is not executed or synced to the starter. Draft, input or strategy edits invalidate earlier model results and self-reports.
Local learning records
Saved only in this browser; export is available. No cross-device sync or proof of mastery. Earlier F01 and six-task records are preserved through migration.
- F05 opened: —
- Model check: Unchecked or invalidated
- Local tests (self-reported): —
- Highest hint level: 0/3
- Reference viewed: —
- Unaided variant (self-reported): —
02 · Repair your local project
Start any task independently: its package contains that task’s bug and a runnable baseline for the other business functions. Python 3.11+, standard library only. See the package’s verification notes for tested environments.
- Extract into a new directory. Open a terminal and identify the file being run. Run public tests to reproduce the bug; consult the package’s intentional-failure list.
- Write a hypothesis and choose a value to inspect. Repair the specified function, retain public tests and record the diff and a minimal counterexample.
- Run this task’s tests, then the full regression suite. Web-model results do not replace local execution; self-reports are not remotely verified by this site.
python check_environment.py python -m unittest tests.test_f05 -v python -m unittest discover -s tests -v
Corrupt, empty or unsupported-version files must stop loading; never treat them as an empty project and overwrite them. The default data path is relative to the project; --data selects an explicit file. Save through a same-directory temporary file and replacement. Failures preserve the old file; this is not a complete power-loss durability guarantee.
03 · Independent variant
Independently implement load_todos(path) in challenges.py for todo recovery. Distinguish missing, unreadable, damaged and valid-empty files. Preserve bad files, report failures truthfully and verify a second process from another working directory.
python -m unittest tests.test_variants -v
Implement from the requirement, replace the skipped placeholder in tests/test_student_counterexamples.py and add an assertion that exposes the original bug. Explain the failure. Hint or reference use excludes an unaided completion record for this attempt.
Reference explanation and answer
Try your own repair first. Opening or downloading the reference records answer use. Passing reference tests establishes only that the materials run.
View reference explanation (records answer use)
Persistence needs explicit data, a path and a next-process loading contract. Working-directory-relative paths change the target; fixed project paths remove ambiguity. Loading errors must not silently become empty data. Temporary replacement protects ordinary failed saves, not every power-loss scenario.
Download this task’s reference (answer) · Version and source digest
Course references and review
- Python: files, JSON and exceptions
Experiment only with fictional records and temporary directories.
- VSCode: day 6 terminal
Check the working directory and the actual file executed.