Skip to content

Instantly share code, notes, and snippets.

@carlosp420
Created September 11, 2016 20:06
Show Gist options
  • Select an option

  • Save carlosp420/4d72886821e50f2794eb6c1b38f6f190 to your computer and use it in GitHub Desktop.

Select an option

Save carlosp420/4d72886821e50f2794eb6c1b38f6f190 to your computer and use it in GitHub Desktop.
generate grocery shopping list for a bunch of recipes #food
import json
import yaml
def parse_recipe(recipe, all_ingredients):
for recipe_name, ingredients in recipe.items():
for ingredient in ingredients:
ingredient_name, amount, unit = ingredient
try:
all_ingredients[ingredient_name]['amount'] += amount
except KeyError:
all_ingredients[ingredient_name] = dict()
all_ingredients[ingredient_name]['amount'] = amount
all_ingredients[ingredient_name]['unit'] = unit
return all_ingredients
with open("recipes.yml", "r") as handle:
data = yaml.load(handle.read())
all_ingredients = dict()
for recipe in data:
all_ingredients = parse_recipe(recipe, all_ingredients)
print(json.dumps(all_ingredients, indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment