Note 14.4.2. Hint.
It may be a good idea to utilize fstrings and the
.split()
method.Bookshelf
and Book
. Book
s 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)
main.py
, and instantiate a Bookshelf
object. Then, create a few Book
objects and add them to the Bookshelf
object.Book Title | Author | Year |
---|---|---|
No Longer Human | Osamu Dazai | 1948 |
The Parallax View | Slavoj Žižek | 2006 |
Fear Stalks the Land! | Thom Yorke | 2021 |
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.
.split()
method.Bookshelf
object, let’s create a few methods to search for books.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.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.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
.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
.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
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.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.Book
objects.