Paul's Python CoursePPC

Project 12 of 20, for Neil

Library Manager

Loans and reference copies for Neil's racing and space books on the Studio shelves, with Yahya and Caitlin arguing about whether a library is a kind of list.

About 2 hours, in 18 steps.

Sign up free to open itLog in

Act 2 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • NeilMy racing and space books live on the Studio shelves, but nothing says which ones are out or who's got them.
  • NeilI asked Yahya and Caitlin to write me something and they've been arguing by the kettle for an hour.
  • YahyaLook, a reference book is a book and a library is a list of books, it's that simple.
  • CaitlinA library isn't a list though, it has books in it, and rules.
  • PaulYou're each right about one thing. Write it and you'll find out which.

In library.py, with type hints throughout:

  1. Status, an Enum with two members: ON_SHELF ("on the shelf") and ON_LOAN ("on loan").
  2. Book, a dataclass: title, author, year, then status (default Status.ON_SHELF) and borrower (default None).
    • lend(member) puts it on loan to member and returns True. If it's already on loan, it changes nothing and returns False.
    • give_back() puts it back on the shelf, with no borrower.
    • str(book) gives Carrying the Fire, Michael Collins (1974): on loan.
  3. ReferenceBook, a kind of Book that never leaves the library: lend always returns False, and it shows itself as a book does, then , reference only, building on Book's line with super().
  4. Library, which has books, kept in a list called books:
    • add(book) adds it and returns True, unless a book with that title is already there, whatever its capitals: then False.
    • find(title) returns the book with that title, whatever its capitals, or None.
    • on_shelf() returns the books that aren't on loan, in the order they were added.
    • len(library) is how many books it has.
What it prints
The library has 3 books
Carrying the Fire, Michael Collins (1974): on loan
House Style, Paul (2025): on the shelf, reference only
On the shelf: The Mechanic's Tale, House Style

What you learn

What's in it

  1. A class that writes itself
  2. A class that writes itself
  3. Notes on what goes where
  4. Notes, not rules
  5. A fixed set of choices
  6. On the shelf, or not
  7. How Paul would keep a record
  8. A kind of something
  9. A kind of book
  10. Let the parent do its part
  11. A setup that never ran
  12. Has a, or is a
  13. Caitlin's way
  14. How Paul would do it
  15. Fix Aaesha's version
  16. Make the library
  17. Ship it