-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
240 lines (202 loc) · 7.77 KB
/
app.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from models import (Base, session, Book, engine)
import datetime
import csv
import time
def menu():
while True:
print('''
\nPROGRAMMING BOOKS
\r1) Add book
\r2) View all books
\r3) Search for books
\r4) Book Analysis
\r5) Exit
''')
choice = input('What would you like to do? ')
if choice in ['1', '2', '3', '4', '5']:
return choice
else:
input('''
\rPlease choose one of the options above!
\rPress enter to try again.''')
def submenu():
while True:
print('''
\n1) Edit
\r2) Delete
\r3) Return to main menu. ''')
choice = input('What would you like to do? ')
if choice in ['1', '2', '3']:
return choice
else:
input('''
\rPlease choose one of the options above!
\rPress enter to try again.''')
def clean_date(date_str):
months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December']
split_date = date_str.split(' ')
try:
month = int(months.index(split_date[0]) + 1)
day = int(split_date[1].split(',')[0])
year = int(split_date[2])
return_date = datetime.date(year, month, day)
except ValueError:
input('''
\n***** DATE ERROR *****
\rThe date format should include a valid Month Day, Year from the past.
\rEx: August 2, 2002
\rPress enter to try again.
\r**************''')
return
else:
return return_date
def clean_price(price_str):
try:
price_float = float(price_str)
except ValueError:
input('''
\n***** PRICE ERROR *****
\rThe price should be a number without a currency symbol!
\rEx: 12.99
\rPress enter to try again.
\r**************''')
else:
return int(price_float * 100)
def clean_id(id_str, options):
try:
book_id = int(id_str)
except ValueError:
input('''
\n***** ID ERROR *****
\rThe price should be a number!
\rPress enter to try again.
\r**************''')
return
else:
if book_id in options:
return book_id
else:
input(f'''
\n***** ID ERROR *****
\rOptions: {options}!
\rPress enter to try again.
\r**************''')
return
def edit_check(column_name, current_value):
print(f'\n**** EDIT {column_name} ****')
if column_name == 'Price':
print(f'\rCurrent Value: {current_value/100}')
elif column_name == 'Date':
print(f'\rCurrent Value: {current_value.strftime("%B %d, %Y")}')
else:
print(f'\rCurrent Value: {current_value}')
if column_name == 'Date' or column_name == 'Price':
while True:
changes = input('What would you like to change the value to? ')
if column_name == 'Date':
changes = clean_date(changes)
if type(changes) == datetime.date:
return changes
elif column_name == 'Price':
changes = clean_price(changes)
if type(changes) == int:
return changes
else:
return input('What would you like to change the value to? ')
def add_csv():
with open ('suggested_books.csv') as csvfile:
data = csv.reader(csvfile)
for row in data:
book_in_db = session.query(Book).filter(Book.title==row[0]).one_or_none()
if book_in_db == None:
title = row[0]
author = row[1]
date = clean_date(row[2])
price = clean_price(row[3])
new_book = Book(title=title, author=author, published_date=date, price=price)
session.add(new_book)
session.commit()
def app():
app_running = True
while app_running:
choice = menu()
if choice == '1':
#add book
title = input('Title: ')
author = input('Author: ')
date_error = True
while date_error:
date = input('Published Date (Ex: October 25, 2022): ')
date = clean_date(date)
if type(date) == datetime.date:
date_error = False
price_error = True
while price_error:
price = input('Price (Ex: 25.65): ')
price = clean_price(price)
if type(price) == int:
price_error = False
new_book = Book(title=title, author=author, published_date=date, price=price)
session.add(new_book)
session.commit()
print('Book added!')
time.sleep(1.5)
elif choice == '2':
#view books
for book in session.query(Book):
print(f'{book.id} | {book.title} | { book.author}')
input('\nPress enter to return to the main menu!')
elif choice == '3':
#search book
id_options = []
for book in session.query(Book):
id_options.append(book.id)
id_error = True
while id_error:
id_choice = input(f'''
\nID Options: {id_options}
\rBook id: ''')
id_choice = clean_id(id_choice, id_options)
if type(id_choice) == int:
id_error = False
the_book = session.query(Book).filter(Book.id==id_choice).first()
print(f'''
\n{the_book.title} by {the_book.author}
\rPublished: {the_book.published_date}
\rPrice: ${the_book.price / 100}''')
sub_choice = submenu()
if sub_choice == '1':
#edit
the_book.title = edit_check('Title', the_book.title)
the_book.author = edit_check('Author', the_book.author)
the_book.published_date = edit_check('Date', the_book.published_date)
the_book.tpricetle = edit_check('Price', the_book.price)
session.commit()
print('Book information updated!')
time.sleep(1.5)
elif sub_choice == '2':
#delete
session.delete(the_book)
session.commit()
print('Book deleted')
time.sleep(1.5)
elif choice == '4':
#analysis
oldest_book = session.query(Book).order_by(Book.published_date).first()
newest_book = session.query(Book).order_by(Book.published_date.desc()).first()
total_books = session.query(Book).count()
#python_books = session.query(Book).filter(Book.book_title.like('%Python%')).count()
print(f'''\n**** BOOK ANALYSIS ****
\rOldest Book: {oldest_book.title}
\rNewsest Book: {newest_book.title}
\rTotal Books: {total_books}''')
input('\nPress enter to go back to the main menu')
elif choice == '5':
print('Have a good day!')
app_running = False
if __name__ == '__main__':
Base.metadata.create_all(engine)
add_csv()
app()