add definition of recipes in terms of other recipes

This commit is contained in:
olari
2021-06-14 15:07:37 +03:00
parent 689c40b58d
commit d26ed919b7
2 changed files with 54 additions and 15 deletions

View File

@@ -10,7 +10,7 @@ def format_timestamp(timestamp):
def parse_foods_file(): def parse_foods_file():
path = Path.home() / 'projects' / 'open-journal' / 'foods' path = Path.home() / 'projects' / 'open-journal' / 'foods'
text = path.read_text() text = path.read_text()
foods, recipes = text.split('---') foods, recipes_str = text.split('---')
def parse_macro(macro): def parse_macro(macro):
if macro == '...': if macro == '...':
@@ -34,6 +34,8 @@ def parse_foods_file():
result[k] = v result[k] = v
return result return result
recipes = {}
def evaluate_ingredients(ingredients): def evaluate_ingredients(ingredients):
result = {} result = {}
@@ -42,30 +44,31 @@ def parse_foods_file():
k,v = parse_macro(ingredient) k,v = parse_macro(ingredient)
if k == 'TOTAL': if k == 'TOTAL':
result[k] = v result[k] = v
break continue
else: else:
total_weight += v total_weight += v
food = foods.get(k)
total = 100.0
if not food:
food = recipes[k].copy()
total = food['TOTAL']
del food['TOTAL']
food = foods[k]
for kk,vv in food.items(): for kk,vv in food.items():
if kk not in result: if kk not in result:
result[kk] = 0.0 result[kk] = 0.0
result[kk] += vv * (v/100.0) result[kk] += vv * (v/total)
if 'TOTAL' not in result: if 'TOTAL' not in result:
result['TOTAL'] = total_weight result['TOTAL'] = total_weight
return result return result
recipes = { for ingredients in [recipe.split('\n') for recipe in recipes_str.strip().split('\n\n')]:
ingredients[0]: evaluate_ingredients(ingredients[1:]) recipes[ingredients[0]] = evaluate_ingredients(ingredients[1:])
for ingredients in [
recipe.split('\n') for recipe in recipes.strip().split('\n\n')
]
}
def get_calories_from_macros(mm): def get_calories_from_macros(mm):
calories = 0.0 calories = 0.0

46
foods
View File

@@ -640,6 +640,15 @@ Carbs 72g
Sugar 3.9g Sugar 3.9g
Protein 11g Protein 11g
WholeGrainPasta
Energy 350kcal
Fat 2g
SaturatedFat 0.4g
Carbs 62g
Sugar 3.6g
Fiber 8g
Protein 16g
SaskiaPeach SaskiaPeach
Energy 16kcal Energy 16kcal
Sugar 4g Sugar 4g
@@ -968,6 +977,11 @@ Milk 180g
Potato 1000g Potato 1000g
TOTAL 1300g TOTAL 1300g
MashedPotato2
Margarine 20g
Potato 760g
Milk 300g
MashedPotato3 MashedPotato3
Potato 1050g Potato 1050g
Milk 350g Milk 350g
@@ -1009,6 +1023,13 @@ Oil 50g
Sugar 12g Sugar 12g
Flour 300g Flour 300g
Pancake2
AlmondMilk 650g
Egg 130g
Sugar 10g
Oil 30g
Flour 370g
CreamPotatoes CreamPotatoes
Cheese 170g Cheese 170g
FoodCream 450g FoodCream 450g
@@ -1068,11 +1089,6 @@ ProteinShake
SkimMilk 400g SkimMilk 400g
ProteinPowder 60g ProteinPowder 60g
MashedPotato2
Margarine 20g
Potato 760g
Milk 300g
VegeKotlet VegeKotlet
Oil 45g Oil 45g
HarkisRouheseos 180g HarkisRouheseos 180g
@@ -1112,6 +1128,12 @@ PeasCornPepper 220g
Ketchup 100g Ketchup 100g
TOTAL 740g TOTAL 740g
NyhtoKauraVeggies
Oil 40g
NyhtoKaura 240g
PeasCornPepper 220g
Ketchup 40g
Pancake Pancake
SkimMilk 430g SkimMilk 430g
Water 300g Water 300g
@@ -1169,3 +1191,17 @@ Egg 130g
PopcornDone PopcornDone
Popcorn 150g Popcorn 150g
Oil 40g Oil 40g
MacCheeseVeggies
Cheese 150g
Oil 20g
PeasCornPepper 230g
WholeGrainPasta 400g
TOTAL 1270g
EggMacCheeseVeggies
MacCheeseVeggies 215g
Egg 325g
Oil 20g
Flour 20g
Cheese 45g