Skip to main content

Foundations of Python Programming: Functions First

Section 14.4 Comprehensive Practice Program: Books and Bookshelves

This exercise is designed to give you some experience working with objects and classes. It is advised that you’ve completed the Classes exercises before attempting.
You’re given two classes, Bookshelf and Book. Books have a few properties: a title, an author, and a year of publication. We set these when instantiating the book. A Bookshelf only has one property: a books list. It’s empty by default.
class Book:
    def __init__(self, title: str, author: str, year: int):
        self.title = title
        self.author = author
        self.year = year


class Bookshelf:
    def __init__(self):
        self.books = []

    def add_book(self, book: Book) -> None:
        self.books.append(book)

Subsection 14.4.1 Adding Some Books

Using the class templates from above, create a file, main.py, and instantiate a Bookshelf object. Then, create a few Book objects and add them to the Bookshelf object.
Here are some books you can use:
Table 14.4.1.
Book Title Author Year
No Longer Human Osamu Dazai 1948
The Parallax View Slavoj Žižek 2006
Fear Stalks the Land! Thom Yorke 2021

Subsection 14.4.2 Confirming Book Data

It’s important to make sure the data you’re parsing ends up in the right places. Create a method in the Book class called get_data() to return the book’s information. I really like APA, so format it like this:
book = Book("No Longer Human", "Osamu Dazai", 1948)
print(book.get_data()) # Dazai, O. (1948). No Longer Human.
Now loop through every book on the shelf and print out its data.

Note 14.4.2. Hint.

It may be a good idea to utilize fstrings and the .split() method.

Subsection 14.4.3 Searching for Books

Now that all the book data is loaded in the Bookshelf object, let’s create a few methods to search for books.
Create a method in the Bookshelf class called get_book_by_name() that takes a string as an argument and returns the book with the matching name. If no book is found, return None. Don’t worry about case sensitivity for now.
Create another method in the Bookshelf class called get_book_by_author() that takes a string as an argument and returns the book with the matching author by last name. If no book is found, return None. Again, don’t worry about case sensitivity for now, and don’t worry about authors with multiple books. For now, each author only has one book.
Now, create a method in the Bookshelf class called get_book_by_year() that takes an integer as an argument and returns the book with the matching year. If no book is found, return None.
Finally, create a method in the Bookshelf class called get_book_by_position() that takes an integer as an argument and returns the book at the matching position. If no book is found, return None.

Subsection 14.4.4 Filling Up Bookshelves

Now that you’re able to search for books, let’s fill up the bookshelf with some more books. Take the following list of books and save it as a text file. Then, write a function called load_books() that reads the file, turns each line into a Book object, and adds it to the Bookshelf object. You shouldn’t need to use any try/except blocks for this.
No Longer Human,Osamu Dazai,1948
The Parallax View,Slavoj Žižek,2006
Fear Stalks the Land!,Thom Yorke,2021
A Certain Hunger,Chelsea Summers,2020
The Stranger,Albert Camus,1942
Annihilation,Jeff Vandermeer,2014
Player Piano,Kurt Vonnegut,1952
1984,George Orwell,1949
Brave New World,Aldous Huxley,1932
Farenheit 451,Ray Bradbury,1953
Metamorphosis,Franz Kafka,1915
Slaughterhouse-Five,Kurt Vonnegut,1969
Thus Spoke Zarathustra,Friedrich Nietzche,1885
Zen and the Art of Motorcycle Maintenance,Robert Pirsig,1974
Sapiens,Yuval Harari,2011
What If?,Randall Munroe,2014
The Tipping Point,Malcolm Gladwell,2000
Flash Boys,Michael Lewis,2014
Hacker Hoaxer Whistleblower Spy,Gabriella Coleman,2014
Kid A Mnesia,Thom Yorke,2021
The Consumer,Michael Gira,1994
Lapvona,Ottessa Moshfegh,2022
Spelunky,Derek Yu,2016
Siddhartha,Hermann Hesse,1922
The Great Gatsby,F. Scott Fitzgerald,1925
Lolita,Vladimir Nabakov,1955
Diary of an Oxygen Thief,Anonymous,2006
The Road,Cormac McCarthy,2006
Metamorphosis,Franz Kafka,2005
Crime and Punishment,Fyodor Dostoevsky,1866
A Litte Life,Hanya Yanagihara,2015
Gone Girl,Gillian Flynn,2012
Neuromancer,William Gibson,1984
Do Androids Dream of Electric Sheep?,Phillip K. Dick,1968
Story of Your Life,Ted Chiang,1998
The Big Short,Michael Lewis,2010
The Brothers Karamazov,Fyodor Dostoevsky,1880
2001: A Space Odyssey,Arthur C. Clarke,1968
The Myth of Sisyphus,Albert Camus,1942
On the Taboo Against Knowing Who You Are,Alan Watts,1966
Animal Farm,George Orwell,1945
You’re gonna need to make some edits to some existing functions. Not all these authors have a last name, so you’ll have to use their first name without an initial. Also, a few searching methods may need to be revised. Some books may have been written in the same year, or by the same author. Change the get_book_by_author() and get_book_by_year() methods to return a list of all matching Book objects. If the search returns only one match, the list will contain only one item. If the search returns nothing the list will be empty.

Subsection 14.4.5 Final Touches

There are a few extra methods to include, just to add some more functionality and fix some issues.
There were a lot of classics on that list, so add a method to the Bookshelf class that returns a list of books written before a specified year. Make sure this method returns a list of Book objects. Also, make sure the year is inclusive.
Some authors appear on that list more than others. Write a method that returns a list of authors that have more than one book on the shelf. Make sure this method returns a list of strings, not Book objects.
Finally, there are some books that have been replaced by newer copies on the shelf. Write a method that finds any duplicate books and removes the older copies. Have this method return the number of books removed.
You have attempted of activities on this page.