Day 2 – Modules and CLI Utilities
Today's Focus
Structure Python code into functions and modules, and build a small CLI utility that reads and parses real data.
Tasks
- Refactor yesterday's library catalogue code into a proper module structure:
catalogue/models.py(data structures),catalogue/filters.py(filter/search logic),catalogue/cli.py(entry point). Use relative imports between them. - Write a CLI utility using
argparsethat accepts a CSV file path and one of the subcommandssummary,filter, orsort. Each subcommand should have its own arguments (e.g.filter --column genre --value fiction). - Parse the CSV using the
csvmodule (not pandas). Validate that required columns exist; if not, print a helpful error message and exit with code1. - Add a
--verboseflag that enables debug-level logging using theloggingmodule. Uselogging.DEBUGstatements throughout your parsing logic so they appear only when the flag is set. - Write a
__main__.pyso your package can be run withpython -m catalogue. Test it works from a clean directory. - Add docstrings (Google or NumPy style) to every function. Run
pydoc catalogue.filtersto confirm they render correctly.
Reading / Reference
- Python docs: argparse tutorial.
- Python docs: logging HOWTO — the basic and intermediate sections.
- Real Python: Python Modules and Packages.