def saveRecipe(tokens, recipe): """ Saves the recipe if it is fully filled in, otherwise gives an error message :param tokens: processed line of input :param recipe: recipe being saved :return: """ ready = True if len(recipe.ingredients.keys()) == 0: print("Cannot save recipe with zero ingredients") ready = False if len(recipe.steps) == 0: print("Cannot save recipe with no steps") ready = False if( recipe.servings is None): print("Must set servings size of recipe before saving") ready = False if(recipe.time is None): print("Must set prep time of recipe before saving") ready = False if not ready: return #displayRecipe(recipe) if recipe.rid is not None: recipe.delete() Recipe.register_recipe(MANAGER, name=recipe.name, prep_time=recipe.time, servings=recipe.servings, equipment=recipe.equipment, owner=USER, steps=recipe.steps, ingredients=recipe.ingredients) print(recipe.name + " saved successfully!") back(tokens)
def post(self): login.logging_user(self) title_last = self.request.get("rtitle") ingr_last = self.request.get("ringredientes") prep_last = self.request.get("rpreparacion") if title_last != "" and ingr_last != "" and prep_last != "": currentUser = users.get_current_user().nickname() aux = Recipe(parent=ndb.Key("Recipe", title_last or "*notitle*"), title=title_last, ingredients=ingr_last, directions=prep_last, author=currentUser) aux.put() time.sleep(0.5) recetas = Recipe.query() template_values = {'recetas': recetas} jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("index.html", **template_values))
def importBSMXRecipe(data): logger.debug("Start parsing BSMX recipe") # BSMX format use not valid entities def correct_bsmx_entities(text): text = re.sub("(ld|rd|rs)quo;", "quot;", text) text = text.replace("°", "°") return text try: tree = ElementTree.fromstring(correct_bsmx_entities(open(data, "r").read())) except TypeError: tree = data except FileNotFoundError: tree = ElementTree.fromstring(correct_bsmx_entities(data)) except: raise recipe = Recipe() recipe_root = tree.find(".//Recipe") recipe.name = recipe_root.find("F_R_NAME").text recipe.brewer = recipe_root.find("F_R_BREWER").text equipment = recipe_root.find("F_R_EQUIPMENT") recipe.volume = bsmx_volume_conversion(float(equipment.find("F_E_BATCH_VOL").text)) recipe.efficiency = float(equipment.find("F_E_EFFICIENCY").text) recipe.boil = float(equipment.find("F_E_BOIL_TIME").text) bsmx_type = { "0": model.constants.RECIPE_TYPE_EXTRACT, "1": model.constants.RECIPE_TYPE_PARTIAL_MASH, "2": model.constants.RECIPE_TYPE_ALL_GRAIN, } recipe.type = bsmx_type[recipe_root.find("F_R_TYPE").text] style = recipe_root.find("F_R_STYLE") recipe.style = "%s%s. %s" % ( style.find("F_S_NUMBER").text, chr(64 + int(style.find("F_S_LETTER").text) % 26), style.find("F_S_NAME").text, ) recipe.recipeNotes = recipe_root.find("F_R_NOTES").text recipe.mash = importBSMXMash(recipe_root.find("F_R_MASH")) elements = recipe_root.find("Ingredients").find("Data") for element in elements.findall("Grain"): recipe.listeFermentables.append(importBSMXFermentable(element)) for element in elements.findall("Hops"): recipe.listeHops.append(importBSMXHop(element)) for element in elements.findall("Yeast"): recipe.listeYeasts.append(importBSMXYeast(element)) for element in elements.findall("Misc"): recipe.listeMiscs.append(importBSMXMisc(element)) logger.debug("End parsing BSMX recipe") return recipe
def optimise(recipe_data, meal_spec): ''' description: Function responsible for optimising a recipe to meet a meal specification. args: 1. recipe_data -- A recipe dictionary object from the list in the recipe.py database. 2. meal_spec -- A meal specification object from the daily_meals list in the user_configs.py file. kwargs: kwargname -- ... raises: exceptname -- ... returns: An optimised recipe object. ''' # Create a new population instance. p = Population() # Fill it with random vaiable variations of the recipe. # While the population is not full. while p.size < configs['max_population_size']: # Create a new recipe. r = Recipe(recipe_data['name'], recipe_data['ingredients']) # Mutate it. r = mutate_recipe(r) # Score the child. score = score_recipe(r, meal_spec) r.score = score # Add it to the population. p.add_recipe(r) # Run the GA to create an version of the recipe with # optimal macronutrient ratios. print("Optimising Recipe: " + recipe_data['name'] + "\n") while p.best_recipe.score < configs['goal_threshold'] \ and p.generation_count < configs['generation_limit']: draw_progress_bar(p.generation_count, configs['generation_limit'] - 1) # Cull the population to the minium level. cull_population(p, meal_spec) # Grow the population to the maximum level. grow_population(p, meal_spec) # Log the best recipe data log_best_recipe(p, meal_spec['name']) # Increment the generation count. p.generation_count = p.generation_count + 1 # Scale the optimised recipe to meet the caloric requirement # given in the meal spec. optimised_recipe = scale_recipe_to_calories(p.best_recipe, \ (meal_spec['perc_daily_calories']/100) \ *user_configs['target_daily_calories']) # Return the optimised recipe. return optimised_recipe
def importBSMXRecipe(data): logger.debug("Start parsing BSMX recipe") #BSMX format use not valid entities def correct_bsmx_entities(text): text = re.sub('(ld|rd|rs)quo;', 'quot;', text) text = text.replace('°', '°') return text try: tree = ElementTree.fromstring(correct_bsmx_entities(open(data, 'r').read())) except TypeError: tree = data except FileNotFoundError: tree = ElementTree.fromstring(correct_bsmx_entities(data)) except: raise recipe = Recipe() recipe_root = tree.find('.//Recipe') recipe.name = recipe_root.find('F_R_NAME').text recipe.brewer = recipe_root.find('F_R_BREWER').text equipment = recipe_root.find('F_R_EQUIPMENT') recipe.volume = bsmx_volume_conversion(float(equipment.find('F_E_BATCH_VOL').text)) recipe.efficiency = float(equipment.find('F_E_EFFICIENCY').text) recipe.boil = float(equipment.find('F_E_BOIL_TIME').text) bsmx_type = { '0': model.constants.RECIPE_TYPE_EXTRACT, '1': model.constants.RECIPE_TYPE_PARTIAL_MASH, '2': model.constants.RECIPE_TYPE_ALL_GRAIN } recipe.type = bsmx_type[recipe_root.find('F_R_TYPE').text] style = recipe_root.find('F_R_STYLE') recipe.style = '%s%s. %s' % (style.find('F_S_NUMBER').text, chr(64+int(style.find('F_S_LETTER').text) % 26), style.find('F_S_NAME').text) recipe.recipeNotes = recipe_root.find('F_R_NOTES').text recipe.mash = importBSMXMash(recipe_root.find('F_R_MASH')) elements = recipe_root.find('Ingredients').find('Data') for element in elements.findall('Grain'): recipe.listeFermentables.append(importBSMXFermentable(element)) for element in elements.findall('Hops'): recipe.listeHops.append(importBSMXHop(element)) for element in elements.findall('Yeast'): recipe.listeYeasts.append(importBSMXYeast(element)) for element in elements.findall('Misc'): recipe.listeMiscs.append(importBSMXMisc(element)) logger.debug("End parsing BSMX recipe") return recipe
def recent_recipes(self, user=None, limit=5): cur = self.get_cursor() cur.execute( """ SELECT recipes.*, owner.*, MAX(datemade) as last_date FROM recipes JOIN dates_made ON recipes.rid = dates_made.rid JOIN users AS owner ON owner.uid = recipes.owner_id %%% GROUP BY recipes.rid, owner.uid ORDER BY last_date DESC LIMIT %s; """.replace('%%%', ('WHERE dates_made.uid = %s' if user != None else '')), ((limit, ) if user == None else (user.uid, limit))) results = (Recipe.new_from_combined_record(self, record) for record in cur.fetchall()) cur.close() return results
def compatible_recipes(self, limit=10): cur = self.manager.get_cursor() cur.execute( """ SELECT recipes.*, owner.*, SUM(LEAST(1.0, qtyowned / ownd_comp.amount)) / ( SELECT COUNT(iname) FROM requires_ingredient WHERE requires_ingredient.rid = recipes.rid ) AS percent_owned FROM ingredient_ownership JOIN requires_ingredient AS ownd_comp ON ownd_comp.iname = ingredient_ownership.iname JOIN recipes ON ownd_comp.rid = recipes.rid JOIN users AS owner ON owner.uid = recipes.owner_id WHERE ingredient_ownership.uid = %s GROUP BY recipes.rid, owner.uid ORDER BY percent_owned DESC LIMIT %s; """, (self.uid, limit)) results = ((Recipe.new_from_combined_record(self.manager, record), record[8]) for record in cur.fetchall()) cur.close() return results
def popular(tokens, optional=None): """ Displays the 10 most made recipes for the week to the user :param tokens: processed line of input :param optional: N/A :return: """ return RecipeListLoop(list(item[0] for item in Recipe.get_popular_recipes(MANAGER, 10)))
def importBeerXML(self): fichierBeerXML = self.s try: self.recipe = Recipe.parse(fichierBeerXML) self.currentRecipeMash = self.recipe.mash except: errors = Errors() errors.warningXml()
def get(self): login.logging_user(self) recetas = Recipe.query() template_values = {'recetas': recetas} jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("index.html", **template_values))
def createRecipe(tokens, optional=None): """ Enter recipe create loop with a new empty recipe :param tokens: processed line of input :param optional: N/A :return: """ return RecipeCreateLoop( Recipe(manager=MANAGER, rid=None, servings=None, time=None, name=tokens[1], owner_id=USER.uid, equip=[], owner=USER, ingr={}, steps=[]) )
def breed_recipes(recipe_1, recipe_2): ''' description: Takes two recipes and randomly combines their IngredientAmount(s) to form a new child recipe. Performs rescaling to ensure the child recipe is valid (i.e its IngredientAmount(s) sum to 100%). args: 1. recipe_1 -- The first parent Recipe instance. 2. recipe_2 -- The second parent Recipe instance. argv: [...] kwargs: kwargname -- ... raises: exceptname -- ... returns: A child Recipe instance. ''' # Start the child's ingredients_data list. child_ingredients_data = [] # Grab a list of the ingredient names. ingredient_names = recipe_1.ingredient_names # For each ingredient name on the list; for ingredient_name in ingredient_names: # Choose this IngredientAmount from parent at random. parent_1_ia = recipe_1.get_ingredient_amount(ingredient_name) parent_2_ia = recipe_2.get_ingredient_amount(ingredient_name) child_ia = random.choice([parent_1_ia, parent_2_ia]) # Create the ingredient dict. # It is valid to use the ingredient perc as the # gms quantity in the ingredient dict, becuase this # is equivalent to the number of gms in a meal totalling # 100g. Since it will be normalised to a percentage # this is OK. ingredient_dict = { "name": ingredient_name, "gms": child_ia.perc_of_meal } # Add the ingredient dict to the child's ingredient list. child_ingredients_data.append(ingredient_dict) # Once the ingredient dict has been created by randomly # choosing from the two parents, instantiate the child # recipe. child_recipe = Recipe(recipe_1.name, child_ingredients_data) # If the child recipe should mutate; if should_mutate(): child_recipe = mutate_recipe(child_recipe) # Rescale the recipe. child_recipe = _rescale_recipe(child_recipe) # Return the child recipe. return child_recipe
def generate(): adjectives = file_to_string_list(ADJECTIVES) food_genres = file_to_string_list(FOOD_GENRES) persons = file_to_string_list(PERSONS) pt_verbs = file_to_string_list(PT_VERBS) ingredients = list(map(lambda x: ingredient_from_st(x), file_to_string_list(INGREDIENTS))) addition_methods = file_to_string_list(ADDITION_METHODS) final_steps = file_to_string_list(FINAL_STEPS) for i in range(VOLUME): recipe_ingredients = random.sample(ingredients, k=random.randint(1, min(len(ingredients)-1, MAX_INGREDIENTS_PER_RECIPE))) title_ingredient = random.choice(recipe_ingredients) name = assemble_name(persons, adjectives, pt_verbs, title_ingredient, food_genres) equip_dictionary = readEquipment(EQUIPMENT) selected_equips = random.sample(list(equip_dictionary.keys()), k=random.randint(0, len(list(equip_dictionary.keys())))) selected_equips_dict = {} for eq in selected_equips: selected_equips_dict[eq] = equip_dictionary[eq] recipe_steps = gen_steps_lst(addition_methods, recipe_ingredients, selected_equips_dict, final_steps) ingredients_dict = gen_ingredient_dictionary(recipe_ingredients) rec = Recipe(MANAGER, 0, random.randint(1, 4), random.randint(5, 360), name, list(selected_equips_dict.keys()), None, ingredients_dict, recipe_steps) print("====================") displayRecipe(rec) Recipe.register_recipe(manager=MANAGER, name=name, servings=random.randrange(1, 4), time=random.randint(5, 360), equip=list(selected_equips_dict.keys()), owner=USER, ingr=ingredients_dict, steps=recipe_steps, owner_id=USER.uid)
def top_10_recipes_graph(manager): pairs = Recipe.get_popular_recipes(manager, 5) X = [] Y = [] for rec, count in pairs: X.append("\n".join(wrap(rec.name, 15))) Y.append(count) fig1, ax1 = plt.subplots() ax1.set_title("Most Made Recipes this Week") ax1.bar(height=Y, x=X, width=0.4) plt.show()
def searchRecipes(self, name): cur = self.get_cursor() partialName = ['%' + x.strip() + '%' for x in name.split(' ')] sqlQuery = """ SELECT recipes.*, owner.* FROM recipes JOIN users AS owner ON owner.uid = recipes.owner_id WHERE """ + " OR ".join(["rName LIKE %s"] * len(partialName)) + ";" cur.execute(sqlQuery, partialName) records = cur.fetchall() cur.close() return (Recipe.new_from_combined_record(self, record) for record in records)
def search_by_ingredient(self, ingr, limit=10): cur = self.get_cursor() cur.execute( """ SELECT recipes.*, owner.* FROM requires_ingredient JOIN recipes ON requires_ingredient.rid = recipes.rid JOIN users AS owner ON owner.uid = recipes.owner_id WHERE iname = %s LIMIT %s; """, (ingr.iname, limit)) results = (Recipe.new_from_combined_record(self, record) for record in cur.fetchall()) cur.close() return results
def get(self): login.logging_user(self) recetas = Recipe.query() recipe_id = self.request.get("id") receta_actual = None for receta in recetas: if str(receta.key.id()) == recipe_id: receta_actual = receta comentarios = Comment.query(Comment.recipe_id == str(recipe_id)) template_values = { 'receta_actual': receta_actual, 'comentarios': comentarios } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("recipeView.html", **template_values))
def recipe_get(self, idx, conn): selrecipes = select([self.recipes]).where(self.recipes.c.id == idx) recipesresult = conn.execute(selrecipes) recipex = "" for recipesrow in recipesresult: selusers = select([self.users]).where( self.users.c.id == recipesrow.user_id) usersresult = conn.execute(selusers) author = usersresult.fetchone().name selingrslst = select([self.ingredients_list]).where( self.ingredients_list.c.recipe_id == idx) ingredslstres = conn.execute(selingrslst) ings = [] for ingredslstrow in ingredslstres: selingrds = select([self.ingredients]).where( self.ingredients.c.id == ingredslstrow.ingredient_id) ingrdsresult = conn.execute(selingrds) for ingredsrow in ingrdsresult: ing = Ingredient( ingredsrow.name, ingredsrow.allergen, ingredslstrow.quantity) ings.append(ing) seldirs = select([self.directions_list]).where( self.directions_list.c.recipe_id == idx) dirsresult = conn.execute(seldirs) dirs = [] for dirsrow in dirsresult: dir = Direction(dirsrow.number, dirsrow.text) dirs.append(dir) recipex = Recipe(idx, recipesrow.name, recipesrow.country, recipesrow.course, recipesrow.views, author, ings, recipesrow.image, dirs) return recipex
def post(self): login.logging_user(self) recetas = Recipe.query() recipe_id = self.request.get("id") comments_author = self.request.get("author") comments_content = self.request.get("content") comments_title = self.request.get("title-commment") if comments_author != "" and comments_content != "" and recipe_id != "" and comments_title != "": comentario = Comment(author=comments_author, comment=comments_content, recipe_id=recipe_id, title=comments_title) comentario.put() time.sleep(0.5) comentarios = Comment.query(Comment.recipe_id == str(recipe_id)) receta_actual = None for receta in recetas: if str(receta.key.id()) == recipe_id: receta_actual = receta template_values = { 'receta_actual': receta_actual, 'comentarios': comentarios } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("recipeView.html", **template_values))
def testIngredientsIsNone(self): with self.assertRaises(Exception): Recipe("Lasagna", ['pasta', 'italian'], 1.0, 2.0, None, ['combine ingredients', 'cook ingredients'], 2)
def testCategoriesIsNone(self): with self.assertRaises(Exception): Recipe("Lasagna", None, 1.0, 2.0, ['meat', 'noodles', 'sauce', 'cheese'], ['combine ingredients', 'cook ingredients'], 2)
def testNameIsNone(self): with self.assertRaises(Exception): Recipe(None, ['pasta', 'italian'], 1.0, 2.0, ['meat', 'noodles', 'sauce', 'cheese'], ['combine ingredients', 'cook ingredients'], 2)
def importBSMXRecipe(data): logger.debug("Start parsing BSMX recipe") #BSMX format use not valid entities def correct_bsmx_entities(text): text = re.sub('(ld|rd|rs)quo;', 'quot;', text) text = text.replace('°', '°') return text try: tree = ElementTree.fromstring( correct_bsmx_entities(open(data, 'r').read())) except TypeError: tree = data except FileNotFoundError: tree = ElementTree.fromstring(correct_bsmx_entities(data)) except: raise recipe = Recipe() recipe_root = tree.find('.//Recipe') recipe.name = recipe_root.find('F_R_NAME').text recipe.brewer = recipe_root.find('F_R_BREWER').text equipment = recipe_root.find('F_R_EQUIPMENT') recipe.volume = bsmx_volume_conversion( float(equipment.find('F_E_BATCH_VOL').text)) recipe.efficiency = float(equipment.find('F_E_EFFICIENCY').text) recipe.boil = float(equipment.find('F_E_BOIL_TIME').text) bsmx_type = { '0': model.constants.RECIPE_TYPE_EXTRACT, '1': model.constants.RECIPE_TYPE_PARTIAL_MASH, '2': model.constants.RECIPE_TYPE_ALL_GRAIN } recipe.type = bsmx_type[recipe_root.find('F_R_TYPE').text] style = recipe_root.find('F_R_STYLE') recipe.style = '%s%s. %s' % (style.find('F_S_NUMBER').text, chr(64 + int(style.find('F_S_LETTER').text) % 26), style.find('F_S_NAME').text) recipe.recipeNotes = recipe_root.find('F_R_NOTES').text recipe.mash = importBSMXMash(recipe_root.find('F_R_MASH')) elements = recipe_root.find('Ingredients').find('Data') for element in elements.findall('Grain'): recipe.listeFermentables.append(importBSMXFermentable(element)) for element in elements.findall('Hops'): recipe.listeHops.append(importBSMXHop(element)) for element in elements.findall('Yeast'): recipe.listeYeasts.append(importBSMXYeast(element)) for element in elements.findall('Misc'): recipe.listeMiscs.append(importBSMXMisc(element)) logger.debug("End parsing BSMX recipe") return recipe
def importDictRecipe(dic): logger.debug("Start parsing dict") recipe = Recipe() recipe.path = dic['path'] recipe.name = dic['name'] recipe.brewer = dic['brewer'] recipe.style = dic['style'] if dic['type'] == "All Grain": recipe.type = RECIPE_TYPE_ALL_GRAIN elif dic['type'] == "Extract": recipe.type = RECIPE_TYPE_EXTRACT elif recipe.type == "Partial Mash": recipe.type = RECIPE_TYPE_PARTIAL_MASH recipe.volume = dic['volume'] recipe.boil = dic['boilTime'] recipe.efficiency = dic['efficiency'] for hop_dic in dic['hops']: hop = Hop() hop.name = hop_dic['name'] if hop_dic['form'] == "Pellet": hop.form = HOP_FORM_PELLET elif hop_dic['form'] == "Leaf": hop.form = HOP_FORM_LEAF elif hop_dic['form'] == "Plug": hop.form = HOP_FORM_PLUG hop.alpha = hop_dic['alpha'] hop.use = hop_dic['use'] hop.time = hop_dic['time'] hop.amount = hop_dic['amount'] recipe.listeHops.append(hop) for f_dic in dic['fermentables']: f = Fermentable() f.name = f_dic['name'] f.type = f_dic['type'] f.fyield = f_dic['fyield'] f.color = f_dic['color'] f.amount = f_dic['amount'] if f_dic['afterBoil'] == 'TRUE': f.useAfterBoil = True else: f.useAfterBoil = False f.recommendMash = f_dic['recoMash'] recipe.listeFermentables.append(f) for y_dic in dic['yeasts']: y = Yeast() y.name = y_dic['name'] y.productId = y_dic['product_id'] y.labo = y_dic['labo'] y.form = y_dic['form'] y.attenuation = y_dic['attenuation'] recipe.listeYeasts.append(y) for m_dic in dic['miscs']: m = Misc() m.name = m_dic['name'] m.amount = m_dic['amount'] m.type = m_dic['type'] m.use = m_dic['use'] m.time = m_dic['time'] recipe.listeMiscs.append(m) mash_dic = dic['mashProfile'] recipe.mash.name = mash_dic['name'] recipe.mash.ph = mash_dic['ph'] recipe.mash.spargeTemp = mash_dic['sparge'] recipe.mash.tunTemp = mash_dic['tunTemp'] for s_dic in mash_dic['steps']: s = MashStep() s.name = s_dic['name'] s.time = s_dic['time'] s.temp = s_dic['temp'] s.type = s_dic['type'] recipe.listeMashSteps.append(s) recipe.recipeNotes = dic['notes'] return recipe
def importBeerXMLRecipe(data): logger.debug("Start parsing recipe") try: tree = ElementTree.parse(data) except TypeError: tree = data except IOError: tree = ElementTree.fromstring(data) except: raise recipe = Recipe() recipe.path = data presentation = tree.find('.//RECIPE') fermentables = tree.findall('.//FERMENTABLE') hops = tree.findall('.//HOP') levures = tree.findall('.//YEAST') misc = tree.findall('.//MISC') style = tree.find('.//STYLE') mash = tree.find('.//MASH') for element in presentation: if 'NAME' == element.tag: recipe.name = element.text logger.debug(" Recipe name: %s", recipe.name) if 'BREWER' == element.tag: recipe.brewer = element.text logger.debug(" Recipe brewer: %s", recipe.brewer) if 'TYPE' == element.tag: if "All Grain" == element.text: recipe.type = model.constants.RECIPE_TYPE_ALL_GRAIN elif "Partial Mash" == element.text: recipe.type = model.constants.RECIPE_TYPE_PARTIAL_MASH elif "Extract" == element.text: recipe.type = model.constants.RECIPE_TYPE_EXTRACT logger.debug(" Recipe type: %s", recipe.type) if "BATCH_SIZE" == element.tag: recipe.volume = float(element.text) logger.debug(" Recipe volume: %s", recipe.volume) if "EFFICIENCY" == element.tag: recipe.efficiency = float(element.text) logger.debug(" Recipe efficiency: %s", recipe.efficiency) if "BOIL_TIME" == element.tag: recipe.boil = float(element.text) logger.debug(" Recipe boil time: %s", recipe.boil) if "NOTES" == element.tag: recipe.recipeNotes = element.text logger.debug(" Recipe notes: %s", recipe.recipeNotes) try: for element in style: if "NAME" == element.tag: recipe.style = element.text except TypeError: recipe.style = "" try: recipe.mash = importBeerXMLMash(mash) except: pass for element in fermentables: recipe.listeFermentables.append(importBeerXMLFermentable(element)) for element in hops: recipe.listeHops.append(importBeerXMLHop(element)) for element in levures: recipe.listeYeasts.append(importBeerXMLYeast(element)) for element in misc: recipe.listeMiscs.append(importBeerXMLMisc(element)) logger.debug("End parsing recipe") return recipe
def testServingsIsNegative(self): with self.assertRaises(Exception): Recipe("Lasagna", ['pasta', 'italian'], 1.0, 2.0, ['meat', 'noodles', 'sauce', 'cheese'], ['combine ingredients', 'cook ingredients'], -1)
def testInstructionsIsNone(self): with self.assertRaises(Exception): Recipe("Lasagna", ['pasta', 'italian'], 1.0, 2.0, ['meat', 'noodles', 'sauce', 'cheese'], None, 2)
def testInstructionsContainsNone(self): with self.assertRaises(Exception): Recipe("Lasagna", ['pasta', 'italian'], 1.0, 2.0, ['meat', 'noodles', 'sauce', 'cheese'], [None, 'combine ingredients', 'cook ingredients'], 2)
def recommended_recipes(self, limit=10): cur = self.manager.get_cursor() cur.execute( """ WITH user_commonality AS ( SELECT others_made.uid as uid, COUNT(others_made.rid)::float / ( SELECT COUNT(rid) FROM dates_made WHERE uid = others_made.uid OR uid = %s ) AS percent_shared FROM dates_made AS personally_made JOIN dates_made AS others_made ON others_made.rid = personally_made.rid WHERE personally_made.uid = %s AND others_made.uid != %s GROUP BY others_made.uid ), users_by_recipe AS ( SELECT uid, rid, LOG(COUNT(id)) + 1 as degree FROM dates_made GROUP BY uid, rid ), recipe_ownership AS ( SELECT recipes.rid, SUM(LEAST(1.0, qtyowned / ownd_comp.amount)) / ( SELECT COUNT(iname) FROM requires_ingredient WHERE requires_ingredient.rid = recipes.rid ) AS percent_owned FROM ingredient_ownership JOIN requires_ingredient AS ownd_comp ON ownd_comp.iname = ingredient_ownership.iname JOIN recipes ON ownd_comp.rid = recipes.rid WHERE ingredient_ownership.uid = %s GROUP BY recipes.rid ), scores AS ( SELECT users_by_recipe.rid, SUM(user_commonality.percent_shared * users_by_recipe.degree) as score, SUM(percent_owned) AS percent_owned FROM users_by_recipe JOIN user_commonality ON users_by_recipe.uid = user_commonality.uid JOIN recipe_ownership ON users_by_recipe.rid = recipe_ownership.rid GROUP BY users_by_recipe.rid ) SELECT recipes.*, users.*, percent_owned, (percent_owned/2 + 0.5) * score AS recommendation_degree FROM scores JOIN recipes ON recipes.rid = scores.rid JOIN users ON recipes.owner_id = users.uid ORDER BY recommendation_degree DESC; """, [self.uid] * 4) results = ((Recipe.new_from_combined_record(self.manager, record), record[8], record[9]) for record in cur.fetchall()) cur.close() return results
def testCreateValidRecipe(self): result = Recipe("Lasagna", ['pasta', 'italian'], 1.0, 2.0, ['meat', 'noodles', 'sauce', 'cheese'], ['combine ingredients', 'cook ingredients'], 3) self.assertEqual("Lasagna", result.getName(), "checking name") self.assertEqual(2, len(result.getCategories()), "checking number of categories") self.assertEqual("pasta", result.getCategories()[0], "checking first category") self.assertEqual("italian", result.getCategories()[1], "checking second category") self.assertEqual(4, len(result.getIngredients()), "checking number of ingredients") self.assertEqual("meat", result.getIngredients()[0], "checking first ingredient") self.assertEqual("noodles", result.getIngredients()[1], "checking second ingredient") self.assertEqual("sauce", result.getIngredients()[2], "checking third ingredient") self.assertEqual("cheese", result.getIngredients()[3], "checking fourth ingredient") self.assertEqual(2, len(result.getInstructions()), "checking number of instructions") self.assertEqual("combine ingredients", result.getInstructions()[0], "checking first instruction") self.assertEqual("cook ingredients", result.getInstructions()[1], "checking second instruction") self.assertEqual(1.0, result.getTimeToCook(), "checking time to cook") self.assertEqual(2.0, result.getTimeToPrep(), "checking time to prep") self.assertEqual(3, result.getServings(), "checking number of servings")
def importBeerXMLRecipe(data): logger.debug("Start parsing recipe") try: tree = ElementTree.parse(data) except TypeError: tree = data except FileNotFoundError: tree = ElementTree.fromstring(data) except: raise recipe = Recipe() recipe.path = data presentation = tree.find('.//RECIPE') fermentables = tree.findall('.//FERMENTABLE') hops = tree.findall('.//HOP') levures = tree.findall('.//YEAST') misc = tree.findall('.//MISC') style = tree.find('.//STYLE') mash = tree.find('.//MASH') for element in presentation: if 'NAME' == element.tag: recipe.name = element.text logger.debug(" Recipe name: %s", recipe.name) if 'BREWER' == element.tag: recipe.brewer = element.text logger.debug(" Recipe brewer: %s", recipe.brewer) if 'TYPE' == element.tag: if "All Grain" == element.text: recipe.type = model.constants.RECIPE_TYPE_ALL_GRAIN elif "Partial Mash" == element.text: recipe.type = model.constants.RECIPE_TYPE_PARTIAL_MASH elif "Extract" == element.text: recipe.type = model.constants.RECIPE_TYPE_EXTRACT logger.debug(" Recipe type: %s", recipe.type) if "BATCH_SIZE" == element.tag: recipe.volume = float(element.text) logger.debug(" Recipe volume: %s", recipe.volume) if "EFFICIENCY" == element.tag: recipe.efficiency = float(element.text) logger.debug(" Recipe efficiency: %s", recipe.efficiency) if "BOIL_TIME" == element.tag: recipe.boil = float(element.text) logger.debug(" Recipe boil time: %s", recipe.boil) if "NOTES" == element.tag: recipe.recipeNotes = element.text logger.debug(" Recipe notes: %s", recipe.recipeNotes) try: for element in style: if "NAME" == element.tag: recipe.style = element.text except TypeError: recipe.style = "" try: recipe.mash = importBeerXMLMash(mash) except: pass for element in fermentables: recipe.listeFermentables.append(importBeerXMLFermentable(element)) for element in hops: recipe.listeHops.append(importBeerXMLHop(element)) for element in levures: recipe.listeYeasts.append(importBeerXMLYeast(element)) for element in misc: recipe.listeMiscs.append(importBeerXMLMisc(element)) logger.debug("End parsing recipe") return recipe
def jsonRecipeLib(self, recipe): self.s = recipe self.recipe = Recipe.parse(recipe) data = self.recipe.export("json") data = data[1:-1] return data
recipe1 = Recipe( recipeID='135', authorID='sarah', name='Sweet Potato Soup', duration='45 minutes', picture='picture', difficulty='easy', style='boiling', country='chinese', course='dessert', ingredient={ "ingredient": [{ "name": "sweet potato", "amount": "400g" }, { "name": "slab sugar", "amount": "1 block" }, { "name": "ginger", "amount": "2 slices" }] }, procedure={ "procedure": [{ "procedure": 1, "description": "Washed and peel sweet the potato" }, { "procedure": 2, "description": "When you slice the sweet potatoes, cut it halfway then split the piece off (It brings out a better flavor)" }, { "procedure": 3, "description": "Add ginger and sweet potatoes into boiled water. Turn the water to med heat. " }, { "procedure": 4, "description": "Cook for 25 minutes until the sweet potatoes are softened. " }, { "procedure": 5, "description": "Add slab sugar. " }] })