Persisting and Sharing Data

Recap and Feedback


In this part, you connected Python data with files, used libraries for CSV and JSON, and separated parsing from application validation. You also tested the boundaries between reading, calculation, and writing.

The central question is:

What must this function check before passing its result to the next function?

Import an Equipment Catalogue

The concluding exercise turns a CSV catalogue into a checked JSON document. It uses the equipment-record shape from Part 4 and supplies its own starting code and sample file, catalogue.csv; you do not need to copy an earlier submission.

Download the exercise and complete main.py in your IDE. Implement and check the CSV reader first. Then check the JSON writer with known valid records. Run py check.py or python3 check.py from the exercise folder for the supplied assertions. Finally, run main.py separately to try the interactive importer; the handout gives the commands and sample input.

Test the supplied sample, a header-only file, duplicate identifiers, a blank required field, invalid availability text, and a name containing a comma. Verify that a rejected source leaves an existing destination untouched because writing has not started.

Import an equipment catalogue

0 / 45 points

Complete the catalogue importer in main.py. The starter includes a CSV sample and a separate check.py for calculation and file checks.

  1. read_catalogue_csv(path) uses the CSV library with strict=True and requires the exact header id,name,category,available. Open the file with encoding="utf-8" and newline="". Every row must have exactly four fields. Trim surrounding whitespace from all fields. Require nonempty id, name, and category; availability must be exactly yes or no after trimming. Identifiers are case-sensitive and unique after trimming; names may repeat. Return a list of dictionaries in row order, such as {"id": "E10", "name": "Camera", "category": "media", "available": True}. Convert yes to True and no to False. A header-only file returns []. Reject header, field, and application errors with ValueError; CSV parsing and file failures propagate. Do not print or modify a file.
  2. save_catalogue_json(path, items) receives valid records and writes exactly {"version":1,"items":items} as one UTF-8 JSON document. Replace previous contents, return None, and do not print or mutate items. JSON formatting is your choice.
  3. main() asks exactly CSV file: and JSON file: , including their trailing spaces. The user supplies different source and destination paths. Use the reader’s returned records in the save function, loading and validating completely before saving. On OSError, ValueError, or csv.Error, print Import failed. and end. After success, print Imported N items. with the item count. Running main.py must call main() once; importing it must not print, ask for input, or access data files.

A failed source load must preserve an existing destination. Blank lines skipped by the CSV reader are not records.

Work locally

Download this exercise with the Aalto OpenCS IDE plugin, or use Download starter code below and extract the ZIP into its own folder inside programming-practice. Open that exercise folder in your IDE. Keep each exercise in a separate folder.

FileRole
main.pyComplete read_catalogue_csv, save_catalogue_json, and main; keep the guarded application call.
check.pySupplied assertions for reading and saving; add further cases here.
catalogue.csvSample catalogue with three items; only E11 is unavailable.

Save your changes, then open the IDE terminal in this exercise folder: dir on Windows or ls on macOS/Linux should show the files above. Relative paths in the checks start from this folder. Run:

SystemRun the checks
Windowspy check.py
macOS/Linuxpython3 check.py

Use python instead if that is your working Python command. Successful supplied checks print Checks passed.. Read any failed assertion or traceback, fix your code, and rerun the checks. You may extend check.py; use separate data files for extra cases so the supplied samples retain their contents.

The checks read catalogue.csv, verify its records, save practice_catalogue.json, and read that JSON back. Also run py main.py on Windows or python3 main.py on macOS/Linux. Enter catalogue.csv at CSV file: and practice_catalogue.json at JSON file: . The program should print Imported 3 items.. Open the output JSON in the IDE, then try a separate invalid input and verify that an existing output is preserved.

Submit your solution

Save your files. Submit using Submit Current Exercise in the IDE plugin, review the file list, and confirm Submit. Alternatively, create a ZIP from the exercise folder:

SystemCommand
Windowspy -m zipfile -c submission.zip main.py check.py catalogue.csv
macOS/Linuxpython3 -m zipfile -c submission.zip main.py check.py catalogue.csv

Upload submission.zip below and press Submit. The listed files must be at the archive’s top level, without an enclosing folder. Include the supplied support and sample files; leave out generated outputs, .venv, and __pycache__. Read the grading feedback after submitting. The grader also uses its own inputs; passing the sample checks alone does not establish the whole contract.

Check Your Understanding

  1. What changes between a CSV field and the value stored in an application record?
  2. Why does a parser accepting a document not establish that its records are valid?
  3. How can a failed load leave both live state and an existing destination unchanged?
  4. Which checks need files, and which can use ordinary values?

Feedback

Complete the feedback form below, including an estimate of the time you spent on this part. When suggesting improvements in its final free-text field, consider whether files behaved as expected in your environment, which boundary was hardest to debug, and whether documentation checks helped you evaluate AI assistance.

Reflection and feedback

0 / 5 points

Consider the following statements and answer them based on your experiences from studying the materials and working on the assignments in this part. When answering the numeric statements, answer them so that value 1 means that you strongly disagree with the statement, and value 7 means that you strongly agree with the statement. Please be as honest as possible when answering. The feedback will be used to improve the course.

Looking Ahead

In Part 6, you will define classes that keep related data and operations together. Equipment items will support borrowing and returning, and Asteroids will bring together a spaceship, bullets, and asteroid objects. You will build on the file handling, modules, and testing practised here. Each exercise provides its own starting files.