MCPcopy Create free account
hub / github.com/ActiveState/code / main

Function main

recipes/Python/93379_Recipe_Calculator/pyfood.py:16–74  ·  view source on GitHub ↗
(yml_food, yml_recipe)

Source from the content-addressed store, hash-verified

14@click.option('--yml-food', default="", help="Yaml file with food details")
15@click.option('--yml-recipe', default="", help="Yaml file with recipe details")
16def main(yml_food, yml_recipe):
17 food = yaml.load(open(yml_food).read())
18 recipe = yaml.load(open(yml_recipe).read())
19
20 persons = int(recipe['persons'])
21 print("\nRezept: %s für %d Person(en)" % (recipe['name'], persons))
22
23 separator = FORMAT % ("_"*20, "_"*4, "_"*5, "_"*5, "_"*5, "_"*5)
24
25 print(separator)
26 print(FORMAT % ("Name", "#", "kcal", "KH", "E", "F"))
27 print(separator)
28
29 sum_large_calories = 0.0
30 sum_carb = 0.0
31 sum_protein = 0.0
32 sum_fat = 0.0
33
34 for rentry in recipe['food']:
35 for fentry in food:
36 if re.match(rentry['name'], fentry['name']):
37 name = fentry['name']
38 quantity = int(rentry['quantity'])
39 large_calories = round(quantity * fentry['large_calories'] / 100.0, 1)
40 carb = round(quantity * fentry['carb'] / 100.0, 1)
41 protein = round(quantity * fentry['protein'] / 100.0, 1)
42 fat = round(quantity * fentry['fat'] / 100.0, 1)
43
44 print(FORMAT % (name, quantity, large_calories, carb, protein, fat))
45
46 sum_large_calories += large_calories
47 sum_carb += carb
48 sum_protein += protein
49 sum_fat += fat
50 break
51
52 sum_ingredients = sum_carb + sum_protein + sum_fat
53 percentage_carb = round(sum_carb * 100.0 / sum_ingredients, 1)
54 percentage_protein = round(sum_protein * 100.0 / sum_ingredients, 1)
55 percentage_fat = round(sum_fat * 100.0 / sum_ingredients, 1)
56
57 print(separator)
58 print(FORMAT % ("Summe", "",
59 sum_large_calories,
60 sum_carb,
61 sum_protein,
62 sum_fat))
63
64 print(FORMAT % ("Pro Person", "",
65 round(sum_large_calories / persons, 1),
66 round(sum_carb / persons, 1),
67 round(sum_protein / persons, 1),
68 round(sum_fat / persons, 1)))
69
70 print(FORMAT % ("Anteil %", "", "",
71 percentage_carb,
72 percentage_protein,
73 percentage_fat))

Callers 1

pyfood.pyFile · 0.70

Calls 5

printFunction · 0.85
openFunction · 0.50
loadMethod · 0.45
readMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected