CoursePython from Scratch to Intermediate · Module 10: Intermediate Python · part 71 of 75
Part 71 · Module 10: Intermediate Python

Lab: Test and protect the shelf's code

27 min read·14 Oct 2026
Module 10 · Lab · About 60 minutes · Needs: lessons 1 to 4 of this module, and the Module 9 lab (the booknook folder in your Drive, with models.py)

What you are building

BOOKNOOK/TOOLS.PY TESTS/ RESULTS overdue_loans a generator log_calls a decorator safe_save a context manager test_fees.py test_dates.py test_models.py test_storage.py 30 passed pytest no issues mypy, 7 files 4 overdue loans, 580p owed, 9 of 27 copies out
The lab adds tools.py (blue) and four test files (green); pytest passes all 30 tests and mypy finds no issues.

Amira's request for this module is "Make sure it never breaks." The four lessons gave you the tools: generators (Lesson 1), decorators (Lesson 2), context managers (Lesson 3) and pytest (Lesson 4).

This lab puts all four into the project. A new module, booknook/tools.py, holds a generator of overdue loans, a log_calls decorator applied to Library.lend and Library.return_book, and a safe_save context manager. A new folder, tests/, holds four test files that check every rule, edge cases included. Then pytest runs them and mypy checks the package's hints.

Open a new Colab notebook, call it module-10-lab.ipynb, and run each cell in order.

The rest of this part opens with an account

Signing up is free and takes one email. Nothing to pay, and nothing renews.