02 Intermediate Python: OOP, Types, Errors & Testing/2.9 Lab walkthrough: Library Management Package
§ 2.9section 9 of 20·~3 min
Lab walkthrough: Library Management Package
Try the spec in the README first. If you're stuck, build it in this order, then compare with lab/.
models.py:@dataclass Book(isbn, title, author,issued_to: str | None = None, anavailableproperty) and@dataclass Member(member_id, name,borrowed: list[str] = field(default_factory=list)).library.py:class Libraryholds two dicts,bookskeyed by ISBN andmemberskeyed by ID. Writeadd_book(reject duplicates),add_member,issue_book(check availability and aMAX_BOOKS_PER_MEMBERlimit),return_book, andsearch(case-insensitive over title and author). DefineBookNotAvailableErrorhere.__init__.py: re-export the public names.- Tests first for each rule. After writing each method, add its tests and run
uv run pytest -q. storage.py:save(library, path)usesasdict+json.dumps, andload(path)usesBook(**d). Add the round-trip test.ruff check,ruff format, commit, push.
Notice the design choice: Library stores books in a dict keyed by ISBN, so lookups are instant, and it raises errors instead of printing them, so the caller (a CLI today, an API next week) decides how to show them.