-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
56 lines (39 loc) · 1.29 KB
/
menu.py
File metadata and controls
56 lines (39 loc) · 1.29 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
import logging
from app import books
logger = logging.getLogger('scraping.menu')
USER_CHOICE = '''Enter one of the following
- 'b' to look at 5-star books
- 'c' to look at the cheapest books
- 'n' to just get the next available book on the page
- 'q' to exit
Enter your choice: '''
def print_best_books():
logger.debug('Finding best books by rating...')
best_books = sorted(books, key=lambda x: (x.rating * -1, x.price))[:5]
for book in best_books:
print(book)
def print_cheapest_books():
logger.debug('Finding best books by price...')
cheapest_books = sorted(books, key=lambda x: x.price)[:5]
for book in cheapest_books:
print(book)
books_generator = (x for x in books)
def get_next_book():
logger.debug('Getting next book from generator of all books...')
print(next(books_generator))
user_choices = {
'b': print_best_books,
'c': print_cheapest_books,
'n': get_next_book
}
def menu():
user_input = input(USER_CHOICE)
while user_input != 'q':
logger.debug('User did not choose to exit program.')
if user_input in ('b', 'c', 'n'):
user_choices[user_input]()
else:
print('Please choose a valid command.')
user_input = input(USER_CHOICE)
logger.debug('Terminating program...')
menu()