def ingredient_from_st(st): tokens = tokenize(st) ing = Ingredient.get_ingredient(MANAGER, tokens[0]) if ing is None: ing = Ingredient.register_ingredient(MANAGER, tokens[0], tokens[1], tokens[2]) return ing
def setUp(self): # Define ingredient data for testing. ingredient_data = { "name" : "banana", "amounts_per_x_gms" : 100, "gms_protein" : 1.1, "gms_fat" : 0.3, "gms_carb" : 23, } # Create Ingredient object. self.ingredient = Ingredient(ingredient_data['name'], ingredient_data['amounts_per_x_gms'], ingredient_data['gms_protein'], ingredient_data['gms_fat'], ingredient_data['gms_carb']) # Define the amount of ingredient used for testing. self.ingredient_test_amount = 300 # grams # Create the IngredientAmount object. self.ingredient_amount = IngredientAmount(self.ingredient, self.ingredient_test_amount) # Define the correct number of calories in the specified # amount of ingredient. self.correct_cals_in_amount_of_ingredient = 297.3 # Define the correct quantities of macronutrients in the # specified quantities of ingredient. self.correct_gms_protein_in_amount_of_ingredient = 3.3 self.correct_gms_carb_in_amount_of_ingredient = 69 self.correct_gms_fat_in_amount_of_ingredient = 0.9
def increaseIngredient(tokens, optional=None): """ If ingredient exists and any amount specified in tokens is within acceptable range ingredient is added to user's storage. If ingredient is not present in DB, user is prompted to define its properties and it is added :param tokens: processed input line :param optional: N/A :return: None """ iname = tokens[1].lower() amt = 0 try: amt = int(tokens[2]) except(ValueError): try: amt = float(tokens[2]) except: print("Failure to increase ingredient: amount not a number") return if(amt < 0): print("Amount of ingredient to be added must be positive. Please use the 'remove' command to decrease ingredients.") return ing = Ingredient.get_ingredient(MANAGER, iname) if ing is None: userin = input("ingredient '" + iname + "' does not currently exist in the database\n\nWould you like to add it? (Y/N)\n>") if(userin.strip() == "Y"): ing = Ingredient.register_ingredient(MANAGER, iname, input( "What unit is " + iname + " measured in?\n>").lower(), input( "Where is " + iname + " stored?\n>").lower()) else: return if(ing in USER.owned_ingredients): USER.owned_ingredients[ing] += amt else: USER.owned_ingredients[ing] = amt USER.save_owned_ingredients()
def register_ing_prompt(iname): """ Prompts to see if user wants to register a new ingredient :param iname: name of ingredient :return: new ingredient or None """ userin = input("ingredient '" + iname + "' does not currently exist in the database\n\nWould you like to add it? (Y/N)\n>") if(userin.strip() == "Y"): ing = Ingredient.register_ingredient(MANAGER, iname, input( "What unit is " + iname + " measured in?\n>").lower(), input( "Where is " + iname + " stored?\n>").lower()) return ing else: return None
def decreaseIngredient(tokens, optional=None): """ If ingredient exists and any amount specified in tokens is within acceptable range ingredient is removed from user's storage. :param tokens: processed input line :param optional: N/A :return: None """ iname = tokens[1].lower() delete = False if len(tokens) == 2: delete = True else: amt = 0 try: amt = int(tokens[2]) except(ValueError): try: amt = float(tokens[2]) except: print("Failure to decrease ingredient: amount not a number") return ing = Ingredient.get_ingredient(MANAGER, iname) if ing is None: print("ingredient '" + iname + "' does not exist in the DB.") return try: if(delete): USER.owned_ingredients.pop(ing) else: if(USER.owned_ingredients[ing] < amt): print("You do not have enough '" + iname + "' to remove this amount.") return elif(amt < 0): print("The amount you chose to remove was negative. If you wish to add ingredients use the 'add' command") USER.owned_ingredients[ing] -= amt if(USER.owned_ingredients[ing] == 0): USER.owned_ingredients.pop(ing) except(KeyError): print("You do not currently own any " + iname + ".") USER.save_owned_ingredients()
def make_ingredients_graph(manager): pairs = Ingredient.get_common_ingredients(manager, 10) X = [] Y = [] for ing, count in pairs: X.append(ing.iname) Y.append(count) fig1, ax1 = plt.subplots() ax1.set_title("Most Common Ingredients") ax1.pie(Y, labels=X, autopct='%1.1f%%', startangle=90) ax1.axis( 'equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.show()
def deleteIngredient(tokens, recipe): """ Delete an ingredient from the given recipe :param tokens: Processed command tokens :param recipe: The recipe an ingredient is being deleted from :return: """ iname = tokens[1] ing = Ingredient.get_ingredient(MANAGER, iname) if ing not in recipe.ingredients: print(iname, "was not in the recipe.") return recipe.ingredients.pop(ing) print(iname, "successfully removed from recipe.")
def setUp(self): # Define ingredient data for testing. ingredient_data = { "name" : "banana", "amounts_per_x_gms" : 100, "gms_protein" : 1.1, "gms_fat" : 0.3, "gms_carb" : 23, } # Define the correct stats for ingredient. self.correct_cals_per_gm = 0.991 self.correct_perc_fat = 0.3 self.correct_perc_protein = 1.1 self.correct_perc_carb = 23 # Create Ingredient object. self.ingredient = Ingredient(ingredient_data['name'], ingredient_data['amounts_per_x_gms'], ingredient_data['gms_protein'], ingredient_data['gms_fat'], ingredient_data['gms_carb'])
def search(tokens, optional = None): """ Searched recipe DB by name or ingredient, taking user to RecipeListView :param tokens: processed command tokens :param optional: N/A :return: N/A """ termIndex = 1 if tokens[1] == INGREDIENT_FLAG: termIndex += 1 ing = Ingredient.get_ingredient(MANAGER, tokens[2]) if ing is None: print("Ingredient", tokens[2], "does not exist in the database") print("Searching for recipes containing '" + tokens[termIndex] + "'") return RecipeListLoop(list(MANAGER.search_by_ingredient(ing))) elif tokens[1] == NAME_FLAG: termIndex += 1 return search_name(tokens[termIndex]) else: return search_name(tokens[termIndex])
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 addIngredient(tokens, recipe): """ Add an ingredient to the given recipe :param tokens: Processed command tokens :param recipe: The recipe an ingredient is being added to :return: """ iname = tokens[1] amt = 0 try: amt = int(tokens[2]) except(ValueError): try: amt = float(tokens[2]) except: print("Failure to add ingredient, amount not a number") return if(amt <= 0): print("Ingredient amount must be a positive value") return ing = Ingredient.get_ingredient(MANAGER, iname) if not ing: ing = register_ing_prompt(iname) if not ing: print("Cannot add nonexistent ingredient") recipe.ingredients[ing] = amt print(amt, ing.unit, iname, "added successfully to recipe") pass
def __init__(self, name, ingredients_data): ''' description: The constructor function for the recipe class. args: 1. name -- The name of the meal. 2. ingredients_data -- A list of all of the ingredients which go in the meal. Each ingredient is represented by a dictionary with a "name" and "gms" key, representing its name and quantity in grams. The format is the same as is stored in the recipes database. kwargs: kwargname -- ... raises: exceptname -- ... returns: None ''' self._name = name # Create var to hold score. self.score = None # Create an empty list for the IngredientAmounts self._ingredient_amounts = [] # Calculate the total mass of the meal by working down # the list of ingredients, summing their masses; total_meal_gms = 0 for recipe_ingredient in ingredients_data: total_meal_gms = total_meal_gms + recipe_ingredient["gms"] # Work through and instantiate each of the ingredient amounts # in the recipe. for recipe_ingredient in ingredients_data: # Pull out the name for the ingredient; ingredient_name = recipe_ingredient["name"] # Grab the data for the ingredient; ingredient_data = ingredients_db[ingredient_name] # Instantiate the ingredient object; ingredient = Ingredient(ingredient_name, ingredient_data["amounts_per_x_gms"], ingredient_data["gms_protein"], ingredient_data["gms_fat"], ingredient_data["gms_carb"] ) # Calculate the perc_of_meal; perc_of_meal = (recipe_ingredient["gms"]/total_meal_gms)*100 # Calculate the inital percentage of meal from the recipe db # Find the original ingrdients data from the recipes db init_ingredients_data = None for recipe_data in recipes_db: if recipe_data['name'] == name: init_ingredients_data = recipe_data['ingredients'] break init_total_meal_gms = 0 init_ingredient_gms = None for recipe_ingredient in init_ingredients_data: init_total_meal_gms = init_total_meal_gms + recipe_ingredient["gms"] if recipe_ingredient['name'] == ingredient_name: init_ingredient_gms = recipe_ingredient['gms'] # Calculate the initial_perc_of_meal; initial_perc_of_meal = (init_ingredient_gms/init_total_meal_gms)*100 # Populate allowable variation bounds (from defaults if # none specified). # Find the initial ingredient data from the db original_recipe_ingredient = None for ing_data in init_ingredients_data: if ingredient_name == ing_data['name']: original_recipe_ingredient = ing_data if 'allow_perc_of_meal_inc' in original_recipe_ingredient: allow_perc_of_meal_inc = original_recipe_ingredient[ 'allow_perc_of_meal_inc'] else: allow_perc_of_meal_inc = data_configs[ 'default_allow_perc_of_meal_inc'] if 'allow_perc_of_meal_dec' in original_recipe_ingredient: allow_perc_of_meal_dec = original_recipe_ingredient[ 'allow_perc_of_meal_dec'] else: allow_perc_of_meal_dec = data_configs[ 'default_allow_perc_of_meal_dec'] # Instantiate the ingredient amount object; ingredient_amount = IngredientAmount(ingredient, initial_perc_of_meal, perc_of_meal, allow_perc_of_meal_inc, allow_perc_of_meal_dec) # Add the ingredient amount to the list. self._ingredient_amounts.append(ingredient_amount) # Set the mass of the recipe to 100g to get started self.total_mass_gms = 100 # grams.