-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrarystructures.py
60 lines (51 loc) · 1.79 KB
/
librarystructures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Book:
def __init__(self,
title: str,
author: str,
genre: str,
pages: int,
publication_year: int):
self.title: str = title
self.author: str = author
self.genre: str = genre
self.pages: int = pages
self.publication_year: int = publication_year
def to_dict(self) -> dict[str, str | int]:
book_dictionary = {
'title' : self.title,
'author' : self.author,
'genre' : self.genre,
'pages' : self.pages,
'publication_year' : self.publication_year
}
return book_dictionary
def to_list(self) -> list[str | int | None]:
book_list = [
self.title,
self.author,
self.genre,
self.pages,
self.publication_year
]
return book_list
def dict_to_book(self, dictionary: dict[str, str | int]):
try:
self.title = dictionary['title']
self.author = dictionary['author']
self.genre = dictionary['genre']
self.pages = dictionary['pages']
self.publication_year = dictionary['publication_year']
except KeyError as e:
print(f'{e} --- Dictionary should have keys: "title", "author", "genre", "pages", "publication_year"')
class Library:
def __init__(self):
self.books: list[Book] = []
def title_list(self) -> str:
titles = ''
if not self.books: # self.books is empty
return 'There are no books currently stored in the library'
for i, book in enumerate(self.books):
titles += f"{i}: {book.title}\n"
return titles
def add_book(self, book: Book):
self.books.append(book)