forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
204 lines (166 loc) · 5.63 KB
/
Copy pathmain.py
File metadata and controls
204 lines (166 loc) · 5.63 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
This program gathers information from the temp_data.csv file about temperature
"""
from pkg_resources import resource_filename
from typing import List
from uuid import uuid4
from sqlalchemy import create_engine
from sqlalchemy import and_
from sqlalchemy.sql import func, asc, desc
from sqlalchemy.orm import sessionmaker
from treelib import Tree
from project.modules.models import Author
from project.modules.models import Book
from project.modules.models import Publisher
def get_total_number_of_books_by_publishers(session, direction: str) -> List:
"""
:param session: database session to work with
:param direction:
:return:
"""
if direction not in ["asc", "desc"]:
raise Exception(f"Unknown direction: {direction}")
dir = desc if direction == "desc" else asc
results = (
session.query(
Publisher.name, func.count(Book.title).label("total_books")
)
.join(Publisher.books)
.group_by(Publisher.name)
.order_by(dir("total_books"))
)
return results
def get_total_number_of_authors_by_publishers(session, direction: str) -> List:
"""
:param session: database session to work with
:param direction:
:return:
"""
if direction not in ["asc", "desc"]:
raise Exception(f"Unknown direction: {direction}")
dir = desc if direction == "desc" else asc
results = (
session.query(
Publisher.name, func.count(Author.fname).label("total_authors")
)
.join(Publisher.authors)
.group_by(Publisher.name)
.order_by(dir("total_authors"))
)
return results
def get_authors(session) -> List:
"""
This function returns a list of author objects
:param session: database session to work with
:return: list of Author objects
"""
results = session.query(Author).order_by(Author.lname).all()
return results
def add_new_book(session, author_name, book_title, publisher_name):
"""
This function adds a new book to the database
:param session: database session to work with
:param author_name: authors full name
:param book_title: book title
:param publisher_name: publisher of book
:return: None
"""
fname, lname = author_name.split(" ")
# Does the book exist?
book = session.query(Book).filter(Book.title == book_title).one_or_none()
if book is not None:
raise Exception("Book exists", book_title)
# Get author
author = (
session.query(Author)
.filter(and_(Author.fname == fname, Author.lname == lname))
.one_or_none()
)
# Did we get an author?
if author is None:
raise Exception("No author found", author_name)
# Get publisher
publisher = (
session.query(Publisher)
.filter(Publisher.name == publisher_name)
.one_or_none()
)
# Did we get a publisher?
if publisher is None:
raise Exception("No publisher found", publisher)
# Create the book
new_book = Book(title=book_title)
# Insert the book and create the relationships
author.books.append(new_book)
publisher.books.append(new_book)
session.commit()
def output_hierarchical_author_data(authors):
"""
This function outputs the author/book/publisher information in
a hierarchical manner
:param authors: the collection of root author objects
:return: None
"""
authors_tree = Tree()
authors_tree.create_node("Authors", "authors")
for author in authors:
authors_tree.create_node(
f"{author.fname} {author.lname}",
f"{author.fname} {author.lname}",
parent="authors",
)
for book in author.books:
authors_tree.create_node(
f"{book.title}",
f"{book.title}",
parent=f"{author.fname} {author.lname}",
)
for publisher in book.publishers:
authors_tree.create_node(
f"{publisher.name}", uuid4(), parent=f"{book.title}"
)
# Output the hierarchical authors data
print(authors_tree.show())
def main():
"""
Main entry point of program
"""
print("starting")
# Connect to the database using SqlAlchemy
sqlite_filepath = resource_filename(
"project.data", "author_book_publisher.db"
)
engine = create_engine(f"sqlite:///{sqlite_filepath}")
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
# Get the total number of books printed by each publisher
total_books_by_publisher = get_total_number_of_books_by_publishers(
session, "desc"
)
for row in total_books_by_publisher:
print(f"Publisher: {row.name}, total books: {row.total_books}")
print()
# Get the total number of authors each publisher publishes
total_authors_by_publisher = get_total_number_of_authors_by_publishers(
session, "desc"
)
for row in total_authors_by_publisher:
print(f"Publisher: {row.name}, total authors: {row.total_authors}")
print()
# Output hierarchical authors data
authors = get_authors(session)
output_hierarchical_author_data(authors)
# Add a new book
add_new_book(
session,
author_name="Stephen King",
book_title="The Stand",
publisher_name="Random House",
)
# Output the updated hierarchical authors data
authors = get_authors(session)
output_hierarchical_author_data(authors)
print("finished")
if __name__ == "__main__":
main()