Skip to content

M3 solution

The expected, fully-commented artifacts for M3's lab. Peek only after you've tried the lab: the win is building it yourself.

File What it is
budget_pro.py The M2 categorizer refactored: functions, reads expenses.txt, uses the rich library for a table, saves JSON, and handles a bad line with try/except.
expenses.txt Sample input, item, amount per line, including one deliberately broken line to show error handling.

Run it

This module runs on your own machine (not Colab). From inside your activated virtual environment, in a folder containing both files:

pip install rich          # once (or: pip install -r ../../requirements.txt)
python budget_pro.py      # macOS/Linux may need python3
It prints a table and writes expenses_categorized.json next to the script.

How this was verified

Verified end-to-end on Python 3 in a fresh virtual environment: created python3 -m venv .venv, activated it, pip install rich (got rich 15.0.0), then python budget_pro.py. Observed output:

Skipping bad line: 'this line is broken on purpose'
            Your budget
┏━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
┃ Item           ┃ Amount ┃ Size   ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━┩
│ Coffee         │  $4.50 │ small  │
│ Groceries      │ $62.00 │ big    │
│ Bus ticket     │  $2.75 │ small  │
│ New headphones │ $89.99 │ big    │
│ Lunch          │ $13.25 │ medium │
│ Cinema         │ $11.00 │ medium │
└────────────────┴────────┴────────┘
Total: $183.49 across 6 items
Saved expenses_categorized.json
The broken line is skipped (not crashed), and a valid expenses_categorized.json is written.