Created
September 11, 2016 20:06
-
-
Save carlosp420/4d72886821e50f2794eb6c1b38f6f190 to your computer and use it in GitHub Desktop.
generate grocery shopping list for a bunch of recipes #food
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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