-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
57 lines (38 loc) · 1.18 KB
/
menu.py
File metadata and controls
57 lines (38 loc) · 1.18 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
import logging
from app import catalog
logger = logging.getLogger('scraping.menu')
def print_best_books():
best_books = sorted(catalog, key=lambda x: (x.rating * -1, x.price))
for book in best_books:
if book.rating == 5:
print(book)
logger.info('5-star books listed.')
def print_cheapest_books():
cheapest_books = sorted(catalog, key=lambda x: x.price)[:10]
for book in cheapest_books:
print(book)
logger.info('10 cheapest books listed.')
book_generator = (b for b in catalog)
def print_next_book():
print(next(book_generator))
def menu():
USER_CHOICE = """Menu:
- 'b' list 5 star books
- 'c' list the 10 cheapest books
- 'n' see next available book
- 'q' exit
"""
functions = {
'b': print_best_books,
'c': print_cheapest_books,
'n': print_next_book
}
user_input = input(USER_CHOICE)
while user_input != 'q':
if user_input in functions:
functions[user_input]()
else:
print('Invalid input')
user_input = input(USER_CHOICE)
logger.info('Quit selected, program terminated')
menu()