generated from Code-Institute-Org/p3-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
221 lines (177 loc) · 6.54 KB
/
run.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
# This app lets users create and check a shopping list for dinner parties,
# using a database of dishes, ingredients and quantities in Google Sheets
# STANDARD PACKAGES
# import sleep to show output for some time period
# package suggested by my mentor
from time import sleep
# 3RD PARTY LIBRARIES
# colored terminal output
# package suggested by my mentor
from colorama import Fore, Back, Style
# text with ASCII art
import pyfiglet
# SELF-WRITTEN MODULES & FUNCTIONS
# general-purpose functions
from utilities import validate_y_n, clear
# code for retrieving data from the database in Google Sheets
import gsheet
# custom classes, methods & library
import planner
# GET DATA FROM GOOGLE SHEETS
# get a whole worksheet as a list of lists from Google Sheets
# This code is based on the Love Sandwiches project
_data = gsheet.recipes.get_all_values()
# end of code based on the Love Sandwiches project
# TESTING: print for testing/development purposes
# print(data)
# row 0 is the type of dish: starter, main, dessert or drink
# row 0 is not used for the MVP, included here for
# scalability purposes
# row 1 of `_data` is the list of dishes
# item 0 of each row is the ingredient
# subsequent items of each row are the quantity of ingredients needed
# for the corresponding dish
# when an ingredient is not needed for a dish, the corresponding cell is empty
# CONSTANTS
# get the row containing the names of dishes
# the [:] at the end copies the list, so that `DISHES_ROW` can be changed
# without `data[1]` also changing
DISHES_ROW = _data[1][:]
# content and styling of initial prompt to start planning
INITIAL_QUESTION = [f'{Back.MAGENTA}'
'Would you like to plan a dinner party? (Y/N):'
f'{Back.RESET} ']
# content and styling of prompt asking to add more dishes
MORE_DISHES = [f'{Back.MAGENTA}'
'Would you like to add another dish? (Y/N):'
f'{Back.RESET} ']
# content and styling of prompt to check pantry
CHECK_PANTRY = [f'{Back.MAGENTA}'
'Would you like to check your pantry for ingredients? (Y/N):'
f'{Back.RESET} ']
# content and styling of goodbye message
GOODBYE_MESSAGE = '\n' + Fore.MAGENTA \
+ pyfiglet.figlet_format('Have fun!', font='doom') \
+ Fore.RESET
# instruction displayed at the end of running
START_INSTRUCTION = '\nPress RUN PROGRAM to start again'
# GLOBAL VARIABLES
# create a DishList object with `DISHES_ROW` as the input
_dishes = planner.DishList(DISHES_ROW)
# create empty ShoppingList object (will be a list of lists eventually)
_shopping_list = planner.ShoppingList([])
# set whether the planning cycle runs or not
_planning = False
# FUNCTIONS
def welcome_text():
"""Print initial screen text"""
# name of the app in ASCII art using `pyfiglet`
print(Style.BRIGHT + Fore.MAGENTA +
pyfiglet.figlet_format('Dinner Party!', font='doom')
)
print('Designed and coded by Sylvia Blaho (github.com/blahosyl)\n'
+ Fore.RESET)
print('\nDo you love hosting dinner parties? 🍝 🥂 🎂 🥳'
'\nThis app helps you plan them!'
'\nJust select the dishes or drinks you want to make '
'for your guests,'
'\nand the app generates a shopping list for you!\n')
def welcome():
"""
Starts planning or ends the program
depending on user input
"""
# get global variable
global _planning
# print welcome text
welcome_text()
# ask if user wants to start planning, validate input
start = validate_y_n(INITIAL_QUESTION)
if start == 'Y':
print("\nLet's get planning! 🍾\n")
# sleep for 1.5 seconds after printing output
sleep(1.5)
# clear the console
clear()
print('\nHere is the list of dishes you can choose from 🤓')
# start the addition cycle
_planning = True
elif start == 'N':
# exit the program with a message
_planning = False
# clear the terminal
print('\nMaybe some other time, then 👋\n')
# sleep for 1.5 seconds after printing output
sleep(1.5)
# clear the console
clear()
print(GOODBYE_MESSAGE)
print(START_INSTRUCTION)
def print_shopping_list_block():
"""
Check if shopping list is empty,
print confirmation of planning ending,
the shopping list (if not empty), the goodbye message
and the start instruction
"""
# if the shopping list is empty (all ingredients in pantry)
if _shopping_list.list_data == []:
print('You have all the ingredients needed, \n'
'no need to buy anything 💰')
else:
print('\nHere comes your shopping list 📋')
# sleep for 1.5 seconds after printing output
sleep(1.5)
# clear the screen
clear()
print(Fore.GREEN + '\nSHOPPING LIST' + Fore.RESET)
planner.print_formatted(_shopping_list.list_data)
# sleep for 1.5 seconds after printing output
sleep(1.5)
print(GOODBYE_MESSAGE)
print(START_INSTRUCTION)
def end_planning():
"""
Stop the planning loop, ask if check_pantry() should be run,
print shopping list
"""
# stop the planning loop
global _planning
_planning = False
# ask if the user wants to check their pantry
pantry = validate_y_n(CHECK_PANTRY)
if pantry == 'Y':
_shopping_list.check_pantry()
# print the shopping list
print_shopping_list_block()
def ask_more():
"""
If there are dishes left on the list,
ask the user if they want to add more dishes
"""
# get global variables
global _dishes
# check if there are dishes left
# (note: dishes[0] = '', so this should not be counted)
if len(_dishes.dish_data) > 1:
# ask user if they want to add a dish to the shopping list,
add_dish = validate_y_n(MORE_DISHES)
if add_dish == 'Y':
# planning remains True, keep the loop running
clear()
print('\nCool, here is the list of dishes again 🤓')
elif add_dish == 'N':
print("\nGot it! That's enough cooking for now 🍲\n")
end_planning()
else:
print('\nYou have selected all the dishes.')
print('🌯 🍛️ 🍷 🍻 🌮 🥃 🥗 🧆 🍰 🫔 🍹 \n')
end_planning()
# RUN APP
# print initial text and ask if user wants to start the program
welcome()
while _planning:
# add dish to shopping list
_shopping_list.unify_ingredients(_dishes.select_dish(), _data)
# ask if user wants to add more dishes
ask_more()