-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipeTools.py
149 lines (126 loc) · 4.65 KB
/
recipeTools.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
import sys
import os
import csv
import re
import base64
import pickle
class Recipe(object):
def __init__(self, name, instructions, glass, ingredients):
self.name = name
self.glass = glass
self.instructions = instructions
self.ingredients = ingredients
self.image = ""
self.sanitizedIngredients = []
for ingredient in ingredients:
thisIngredient = ""
if "measure" in ingredient:
ingredientStart = ingredient.find(" ",ingredient.find("measure"))+1
thisIngredient = ingredient[ingredientStart:]
#print thisIngredient
elif "tablespoon" in ingredient:
ingredientStart = ingredient.find(" ",ingredient.find("tablespoon"))+1
thisIngredient = ingredient[ingredientStart:]
#print thisIngredient
elif "teaspoon" in ingredient:
ingredientStart = ingredient.find(" ",ingredient.find("teaspoon"))+1
thisIngredient = ingredient[ingredientStart:]
#print thisIngredient
elif "dash" in ingredient:
ingredientStart = ingredient.find(" ",ingredient.find("dash"))+1
thisIngredient = ingredient[ingredientStart:]
#print thisIngredient
elif "drops of" in ingredient:
ingredientStart = ingredient.find(" ",ingredient.find("drops of"))+1
thisIngredient = ingredient[ingredientStart:]
#print thisIngredient
else:
thisIngredient = ingredient
thisIngredient = re.sub(r"[0-9]", "", thisIngredient)
thisIngredient = re.sub(r"/", "", thisIngredient)
thisIngredient = thisIngredient.lstrip()
thisIngredient = thisIngredient.rstrip()
if "" != thisIngredient:
self.sanitizedIngredients.append(thisIngredient.upper())
def canBeMade(self, yourIngredients, pref="bool"):
youNeed = [x for x in self.sanitizedIngredients if x not in yourIngredients]
if "bool" == pref:
if len(youNeed) != 0:
#print "You cannot make a " + self.name + ", you don't have " + str(youNeed)
return False
else:
return True
else:
return youNeed
class RecipeBook(object):
def __init__(self, inFile):
f = open(inFile, 'rt')
f.readline() # skip the first line
self.recipeList = []
try:
reader = csv.reader(f)
for row in reader:
self.recipeList.append(Recipe(row[0], row[1], row[2], row[3:len(row)-1]))
finally:
f.close()
def allIngredients(self):
#make a list of all ingredients
allIngredients = []
for recipe in self.recipeList:
for ingredient in recipe.sanitizedIngredients:
if ingredient not in allIngredients:
allIngredients.append(ingredient)
return allIngredients
def whatCanYouMake(self, stock):
thingsYouCanMake = []
for recipe in self.recipeList:
if recipe.canBeMade(stock.ingredients):
thingsYouCanMake.append(recipe.name)
return thingsYouCanMake
def whatCanYouNotMake(self, stock):
thingsYouCanNotMake = []
for recipe in self.recipeList:
if not recipe.canBeMade(stock.ingredients):
thingsYouCanNotMake.append(recipe.name)
return thingsYouCanNotMake
def whatYouShouldBuy(self, stock):
thingsYouNeed = []
youShouldBuy = []
for recipe in self.recipeList:
thingsYouNeed.extend(recipe.canBeMade(stock.ingredients,"list"))
for i in range(10):
buyNext = max(set(thingsYouNeed), key=thingsYouNeed.count)
thingsYouNeed = [x for x in thingsYouNeed if x != buyNext]
youShouldBuy.append(buyNext)
return youShouldBuy
def export(self, fileName):
fOut = open(fileName+".csv", 'wb')
writer = csv.writer(fOut)
writer.writerow( ('name', 'instructions', 'glass', 'ingredients', 'sanitizedIngredients', 'image') )
for recipe in self.recipeList:
pickledIngredients = pickle.dumps(recipe.ingredients).replace("\n","|")
pickledSanitizedIngredients = pickle.dumps(recipe.sanitizedIngredients).replace("\n","|")
writer.writerow( (recipe.name, recipe.instructions, recipe.glass, pickledIngredients, pickledSanitizedIngredients, recipe.image) )
def setImages(self, path):
for root, dirs, images in os.walk(path):
for image in images:
imageRecipe = image.lower()
imageRecipe = imageRecipe.replace("_"," ")
imageRecipe = imageRecipe.replace(".jpg","")
imageRecipe = imageRecipe.replace(".png","")
for recipe in self.recipeList:
if recipe.name.lower() == imageRecipe:
with open(path+"\\"+image, "rb") as image_file:
encoded_string = "data:image/jpeg;base64," + base64.b64encode(image_file.read())
recipe.image = encoded_string
#for recipe in self.recipeList:
# print recipe.name + " image: " + recipe.image
class Inventory(object):
def __init__(self, ingredients):
self.ingredients = ingredients
def main():
allRecipes = RecipeBook("C:\Users\imigat0\src\MixMatcher\menu.csv")
allRecipes.setImages("C:\Users\imigat0\desktop\images")
allRecipes.export("allstuff")
if __name__ == "__main__":
main()