add definition of recipes in terms of other recipes
This commit is contained in:
23
common.py
23
common.py
@@ -10,7 +10,7 @@ def format_timestamp(timestamp):
|
||||
def parse_foods_file():
|
||||
path = Path.home() / 'projects' / 'open-journal' / 'foods'
|
||||
text = path.read_text()
|
||||
foods, recipes = text.split('---')
|
||||
foods, recipes_str = text.split('---')
|
||||
|
||||
def parse_macro(macro):
|
||||
if macro == '...':
|
||||
@@ -34,6 +34,8 @@ def parse_foods_file():
|
||||
result[k] = v
|
||||
return result
|
||||
|
||||
recipes = {}
|
||||
|
||||
def evaluate_ingredients(ingredients):
|
||||
result = {}
|
||||
|
||||
@@ -42,30 +44,31 @@ def parse_foods_file():
|
||||
k,v = parse_macro(ingredient)
|
||||
if k == 'TOTAL':
|
||||
result[k] = v
|
||||
break
|
||||
continue
|
||||
else:
|
||||
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():
|
||||
if kk not in result:
|
||||
result[kk] = 0.0
|
||||
|
||||
result[kk] += vv * (v/100.0)
|
||||
result[kk] += vv * (v/total)
|
||||
|
||||
if 'TOTAL' not in result:
|
||||
result['TOTAL'] = total_weight
|
||||
|
||||
return result
|
||||
|
||||
recipes = {
|
||||
ingredients[0]: evaluate_ingredients(ingredients[1:])
|
||||
for ingredients in [
|
||||
recipe.split('\n') for recipe in recipes.strip().split('\n\n')
|
||||
]
|
||||
}
|
||||
for ingredients in [recipe.split('\n') for recipe in recipes_str.strip().split('\n\n')]:
|
||||
recipes[ingredients[0]] = evaluate_ingredients(ingredients[1:])
|
||||
|
||||
def get_calories_from_macros(mm):
|
||||
calories = 0.0
|
||||
|
||||
Reference in New Issue
Block a user