-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.py
462 lines (419 loc) · 16.1 KB
/
Common.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#Classes
import pickle
import copy
import datetime
is_debugging = True
class RestrictionProfile:
def __init__(self, name = '', restrictions=[], banned_ingredients=[], banned_recipes=[]):
'''
--------------
Parameters:
--------------
name : str
Name of the restriction profile
restrictions : list[RestrictionProfile]
Sublist of restriction profiles
banned_ingredients : list[Ingredient]
List of banned ingredients
banned_recipes : list[Recipe]
List of banned recipes
'''
self.name = name
self.restrictions = restrictions
self.banned_ingredients = banned_ingredients
self.banned_recipes = banned_recipes
def __str__(self):
my_str='Restriction Profile '+self.name+'\n\tRestrictions: '
if len(self.restrictions) > 0:
my_str += ', '.join([r for r in self.restrictions])
my_str += '\n\tBanned Ingredients: '
if len(self.banned_ingredients) > 0:
my_str += ', '.join([b.name for b in self.banned_ingredients])
my_str += '\n\tBanned Recipes: '
if len(self.banned_recipes) > 0:
my_str += ', '.join([r.name for r in self.banned_recipes])
return my_str
def __eq__(self, other):
return type(self) == type(other) and self.name.lower() == other.name.lower()
class HistEntry:
def __init__(self, recipes = [], date = datetime.date.today()):
'''
HistEntry is a single history entry
recipes : list[Recipe]
Recipes chosen on that date
date : datetime.date
Date the recipes were chosen
'''
self.recipes = recipes
self.date = date
def __str__(self):
return str(self.date)+':\n'+'\n'.join([r.name for r in self.recipes])
def __cmp__(self, other):
return self.date > other.date
class History:
def __init__(self, entries = []):
self.entries = entries
def AddEntry(self, entry):
self.entries.append(entry)
def AddRecipes(self, recipes):
self.entries.append(HistEntry(recipes))
def GetLastRecipes(self):
if len(self.entries) == 0:
return []
return max(self.entries).recipes
class GlobalState:
def __init__(self, all_recipes = [], all_ingredients = [], history = History(),
quick_meals = False, do_dessert = False, do_snacks = False, num_days = 7,
restriction_profiles=[]):
self.all_recipes = all_recipes
self.all_ingredients = all_ingredients
self.history = history
self.quick_meals = quick_meals
self.do_dessert = do_dessert
self.do_snacks = do_snacks
self.num_days = num_days
self.restriction_profiles = restriction_profiles
if restriction_profiles == []:
self.DefaultRestrictionProfiles()
self.chefs = ['Emily','Jason']
def __str__(self):
self_str = "------ Global State -------"
self_str += "\nRecipes : " +", ".join([x.name for x in self.all_recipes])
self_str += "\nIngredients : " +", ".join([x.name for x in self.all_ingredients])
self_str += "\nHistory : " +str(self.history)
self_str += "\nQuick Meals : " +str(self.quick_meals)
self_str += "\nDo Dessert : " +str(self.do_dessert)
self_str += "\nDo Snacks : " +str(self.do_snacks)
self_str += "\nNumber of Days : " +str(self.num_days)
self_str += "\nRestriction Profiles : " + ", ".join([x.name for x in self.restriction_profiles])
self_str += "\nChefs : " +str(self.chefs)
return self_str
def NumServings(self):
return self.num_days*2
def DefaultRestrictionProfiles(self):
print("setting default restriction profiles")
self.restriction_profiles.append(RestrictionProfile('Fatty'))
self.restriction_profiles.append(RestrictionProfile('Meat'))
self.restriction_profiles.append(RestrictionProfile('Beef'))
self.restriction_profiles.append(RestrictionProfile('Dairy'))
self.restriction_profiles.append(RestrictionProfile('Nuts'))
self.restriction_profiles.append(RestrictionProfile('Fish'))
self.restriction_profiles.append(RestrictionProfile('Soy'))
return
global_state = GlobalState()
#Load and Save Functions
def GetGlobalState():
global global_state
return global_state
def SetGlobalState(state):
global global_state
global_state = state
return
def LoadState(global_state, current_chefs, file_name='GroceryState.grc'):
'''
Load the global state, which includes the master ingredient list, master recipe list, total restrictions, and history of recipes used. Use pickle to serialize
'''
try:
state_file = open(file_name,"r")
global_state = pickle.load(state_file)
if global_state.restriction_profiles == []:
global_state.DefaultRestrictionProfiles()
if global_state.history == []:
global_state.history = History()
print "Loading file "
print str(global_state)
state_file.close()
except Exception as e:
print "An exception occured in LoadState : "+str(e)
return
SetDefaultsOnLoad(global_state, current_chefs)
return global_state
def LoadDifferentState(global_state): #TODO
return
def SetDefaultsOnSave(global_state): #TODO
'''
Reset state parameters to defaults for saving
'''
global_state.quick_meals = False
global_state.do_dessert = False
global_state.do_snacks = False
global_state.num_days = 7
return
def SetDefaultsOnLoad(global_state, current_chefs):
'''
Set defaults in load
'''
global_state.chefs = ['Emily','Jason'] #TODO there is a bug in here
current_chefs = global_state.chefs #TODO etc
return
def SaveState(global_state, file_name = 'GroceryState.grc'):
'''
Serialize and save the global state
'''
SetDefaultsOnSave(global_state)
state_file = open(file_name,"w+")
pickle.dump(global_state, state_file)
state_file.close()
return
def ValidFilename(file_name):
return True
def FileNamePrompt():
'''
Explain the criteria for a correct file name
'''
def SaveAsMenu(global_state):
'''
Menu for saving a new file
'''
print "File name: (hit 'c' for cancel)"
while True:
selection = raw_input('> ')
if selection == 'c' or selection == 'C':
break
elif len(selection > 0) and ValidFilename(selection):
SaveState(global_state, selection+".grc")
SaveState(global_state) #The file that loads should be the last one modified
break
else:
print "Invalid file name."
print FileNamePrompt()
DisplayMainMenu()
return
#List Add Functions
def AddItem(the_list, name, name_plural, list_func, current_global_list, search_by_name_func):
'''
Add item to a provided list
Parameters:
list : list
List to add the item to
name : str
Name of the type of object for the prompt. Should be of the form <article> <type name>
name_plural : str
Name of the type of object in plural form for the prompt
list_func : function
Function that lists the available objects of that type
current_global_list : list
Global list of that object type
search_by_name_func : function
Function that searches for that object by name
'''
global global_state
print 'Enter a name of'+name+'.'
print 'If you need to see a list of '+name_plural+', type "list" followed by a filter.'
print "Or, to go back to the previous menu, type 'e'"
while True:
selection = raw_input('> ')
if selection == 'e' or selection == 'E':
break
elif selection.lower()[0:4] == 'list':
list_func(selection, current_global_list)
elif selection in [r.name for r in current_global_list]:
r = search_by_name_func(selection, current_global_list)
if r:
the_list.append(r)
break
else:
print selection + " not found."
return the_list
#List Edit Functions
def EditItemMenu(name, name_plural, parent_menu_name, search_by_name_func, current_global_list, edit_item_func, display_parent_menu):
'''
Obtain an item to edit and edit it
Parameters:
name : str
Name of item type for prompt
name_plural : str
Name of item type in plural for prompts
parent_menu_name : str
Name of parent menu
search_by_name_func : function
Function that searches for the item by name
current_global_list : list
List containing this item
edit_item_func : function
Function editing this item
display_parent_menu : function
Function that displays the parent menu to this one
'''
print 'Name the ' + name + ' you would like to edit.'
print 'For a list of ' + name_plural + ', type "list" plus any applicable filter'
print "Or to go back to the " + parent_menu_name + " menu, type 'e'"
while True:
selection = raw_input('> ')
if selection == 'e' or selection == 'E':
break
elif selection[0:4] == 'list':
ListItem(selection, current_global_list)
else:
item = search_by_name_func(selection, current_global_list)
if item:
new_item = edit_item_func(item)
if new_item != None:
log("Replacing item with " + str(new_item))
current_global_list = ReplaceItem(new_item, current_global_list)
break
else:
print selection + " not found."
display_parent_menu()
return current_global_list
def ReplaceItem(item, current_global_list):
log(" current global list before replacement " + str(current_global_list))
current_global_list = [i if i.name != item.name else item for i in current_global_list]
log(" current global list after replacement " + str(current_global_list))
return current_global_list
#List Remove Functions
def RemoveItem(the_list, name, name_plural, list_func, previous_menu, global_list = True, confirm = True):
'''
Add item to a provided list
Parameters:
list : list
List to add the item to
name : str
Name of the type of object for the prompt. Should be of the form <article> <type name>
name_plural : str
Name of the type of object in plural form for the prompt
list_func : function
Function that lists the available objects of that type
search_by_name_func : function
Function that searches for that object by name
'''
global global_state
print 'Enter a name of'+name+' to remove.'
print 'If you need to see a list of '+name_plural+', type "list" followed by a filter.'
print "Or, to go back to the previous menu, type 'e'"
while True:
selection = raw_input('> ')
if selection == 'e' or selection == 'E':
break
elif selection.lower()[0:4] == 'list':
if global_list:
list_func(selection)
else:
list_func(selection, the_list)
elif selection in [r.name for r in the_list]:
for r in the_list:
if r.name == selection:
if confirm:
print 'You have chosen to remove the following ' + name.split(" ")[1]
print str(r)
print 'Confirm your choice (y/n):'
while True:
selection = raw_input('> ')
if selection.lower() == 'y':
the_list.remove(r)
break
elif selection.lower() == 'n':
break
else:
the_list.remove(r)
break
break
else:
print selection + " not found."
previous_menu()
return the_list
#List List Functions
def ListItem(command, the_list, separator = '\n'):
'''
Print a list according to a provided filter
Parameters:
command : str
command of the format 'list <x>', where x is a filter of the start of the word
'''
filter = command[5:]
alphabetical_members = sorted(the_list, key=lambda x: x.name)
if len(command) < 6:
filtered_members = [x.name for x in alphabetical_members]
else:
filtered_members = [x.name for x in alphabetical_members if x.name[0:len(filter)]==filter]
print separator.join(filtered_members)
return
def SearchItemByName(name, the_list):
'''
Search a list for an item by name
'''
for x in the_list:
if x.name.lower() == name.lower():
return x
return None
def PromptForName(item_type, object_list):
print 'Give this ' + item_type + ' a name.'
name = ''
while True:
name = raw_input('> ')
if name.lower() in [x.name.lower() for x in object_list]:
print name + ' already in use. Please provide another name:'
continue
break
return name
def PromptParameterString(type_name, parameter_name, validity_func, validity_prompt, default = False): #TODO, check for already existing thing somewhere.
print "Give " + type_name + " " + parameter_name + "."
print "Press 'c' to cancel, or 'b' to go back."
if default:
print "Press 'd' to accept the default or current value"
while True:
selection = raw_input('> ')
if selection.lower() == 'c' or selection.lower() == 'b':
return selection
elif default == True and selection.lower() == 'd':
return selection
elif len(selection) > 0 and validity_func(selection):
return selection
else:
print "Invalid input."
print validity_prompt()
def PromptParameterMultiple(type_name, prompt_type, parameter_name, validity_func = None, validity_prompt = None, default = False):
print 'Would you like to add ' + parameter_name + ' to this ' + type_name + " (y/n)?"
print '(c)ancel, (b)ack'
parameters = []
while True:
selection = raw_input('> ')
if selection.lower() == 'f' or selection.lower() == 'n':
break
elif selection.lower() == 'b' or selection.lower() == 'c':
return selection
elif selection.lower() == 'y':
if prompt_type == 'string':
parameters.append(PromptParameterString(type_name, parameter_name, validity_func, validity_prompt, default))
else:
parameters.append(PromptParameterYesNo(type_name, parameter_name, default))
print 'Would you like to add ' + parameter_name + ' to this ' + type_name + " (y/n)?"
print '(c)ancel, (b)ack'
return parameters
def PromptParameterYesNo(type_name, parameter_name, default = False):
print "Is this " + type_name + " " + parameter_name + "? (y/n)"
print "Press 'c' to cancel, or 'b' to go back."
if default:
print "Press 'd' to accept the default or current value"
while True:
selection = raw_input('> ')
if selection.lower() == 'c' or selection.lower() == 'b':
return selection
elif default == True and selection.lower() == 'd':
return selection
elif selection.lower() == 'y':
return True
elif selection.lower() == 'n':
return False
else:
continue
def ValidBool(val):
if val == '1' or val == '0':
return True
print val + " len: "+str(len(val))
return False
def PromptValidBool():
print "Use 1 for True, 0 for False"
def ValidNum(num):
try:
float(num)
return float(num) > 0
except ValueError:
return False
def PromptValidNum():
print "Provide a number greater than 0"
def log(string_to_log):
print str(string_to_log)
def debug(string_to_log):
if is_debugging:
print str(string_to_log)