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 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 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 __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.