Skip to content

Commit 913a131

Browse files
author
Thomas Lehmann
committed
recipe calculator
1 parent c2ffb72 commit 913a131

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2017 Thomas Lehmann
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Welcome to the simple recipe calculator
2+
3+
## Quickstart
4+
5+
The example yml files you will find in the repository (no guarantee for food values; fetch your own):
6+
- [food.yml](food.yml)
7+
- [fruit_cocktail.yml](fruit_cocktail.yml)
8+
9+
```
10+
python pyfood.py --yml-food=food.yml --yml-recipe=fruit_cocktail.yml
11+
12+
Rezept: Frucht Cocktail für 3 Person(en)
13+
____________________ ____ _____ _____ _____ _____
14+
Name # kcal KH E F
15+
____________________ ____ _____ _____ _____ _____
16+
Kiwi 200 124.0 18.2 2.0 1.2
17+
Orange 300 141.0 24.9 3.0 0.6
18+
Apfel 400 260.0 57.6 1.2 0.4
19+
Himbeeren 350 150.5 16.8 4.5 1.1
20+
Heidelbeeren 250 97.5 15.0 2.5 0.0
21+
Zitrone 200 66.0 6.0 2.8 1.2
22+
____________________ ____ _____ _____ _____ _____
23+
Summe 839.0 138.5 16.0 4.5
24+
Pro Person 279.7 46.2 5.3 1.5
25+
Anteil % 87.1 10.1 2.8
26+
27+
```
28+
29+
You can see the name and the quantity of each food and
30+
the calculated ingredients. As an example for the Kiwi
31+
the double of the ingredient values is shown as specified
32+
in the food yml file.
33+
34+
At the end you can see the sum per ingredient and the
35+
sum per ingredient value calculated for one person.
36+
37+
Finally you see the percentages. Obviously the example is
38+
not really low carb :)
39+
40+
Hint: there are a lot of online locations to find those
41+
food ingredients but usually buying food you often have
42+
such information available on the article somewhere with
43+
some exceptions of course.
44+
45+
My personal rules I try to follow:
46+
- at least 50% protein for a recipe (in most cases)
47+
- avoid to eat more large calories as you should (can be roughly calculated -> internet)
48+
- avoid fast food; buying fresh things
49+
- do some sport (no athletic; just a litte bit does help; you can do it at home)
50+
- we (my wife and me) are making our own low carb bread (as an example)
51+
52+
## The structure of the food file
53+
54+
Quite simple it's a list of entries like following:
55+
56+
```
57+
- name: Some Food
58+
large_calories: 1.1
59+
fat: 2.2
60+
carb:3.3
61+
protein: 3.3
62+
```
63+
64+
It's always the ingredients related to 100 gram or
65+
100 millilitre of a concrete food. The tool does
66+
not check that the sum of fat, carb and protein
67+
is less or equal that limit.
68+
69+
You can find an example file in the repository.
70+
Please note: no guarantee on the value there;
71+
I fetched them from somewhere for testing purpose only.
72+
73+
## The structure of one recipe file
74+
75+
You would have to organise one recipe per file and
76+
one file looks like following:
77+
78+
```
79+
---
80+
name: Frucht Cocktail
81+
persons: 3
82+
food:
83+
- { name: Kiwi, quantity: 200 }
84+
- { name: .*nge, quantity: 300 }
85+
- { name: Ap.*, quantity: 400 }
86+
- { name: Him.*, quantity: 350 }
87+
- { name: Heid.*, quantity: 250 }
88+
- { name: Zi.*, quantity: 200 }
89+
```
90+
91+
Of course you have name (title) and you specify
92+
for how many person the recipe is. Finally
93+
you have a food list where you specify the food
94+
and how many gram/millilitre you take.
95+
96+
The name is a Python compatible regular expression
97+
so that you do not have to specify the full name.
98+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- name: Kiwi
2+
large_calories: 62
3+
fat: 0.6
4+
carb: 9.1
5+
protein: 1.0
6+
7+
- name: Apfel
8+
large_calories: 65
9+
fat: 0.1
10+
carb: 14.4
11+
protein: 0.3
12+
13+
- name: Orange
14+
large_calories: 47
15+
fat: 0.2
16+
carb: 8.3
17+
protein: 1.0
18+
19+
- name: Heidelbeeren
20+
large_calories: 39.0
21+
fat: 0.0
22+
carb: 6.0
23+
protein: 1.0
24+
25+
- name: Himbeeren
26+
large_calories: 43.0
27+
fat: 0.3
28+
carb: 4.8
29+
protein: 1.3
30+
31+
- name: Zitrone
32+
large_calories: 33.0
33+
fat: 0.6
34+
carb: 3.0
35+
protein: 1.4
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: Frucht Cocktail
3+
persons: 3
4+
food:
5+
- { name: Kiwi, quantity: 200 }
6+
- { name: .*nge, quantity: 300 }
7+
- { name: Ap.*, quantity: 400 }
8+
- { name: Him.*, quantity: 350 }
9+
- { name: Heid.*, quantity: 250 }
10+
- { name: Zi.*, quantity: 200 }
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python
2+
# -*- coding: iso-8859-15 -*-
3+
"""
4+
Recipe calculator.
5+
"""
6+
import re
7+
import click
8+
import yaml
9+
10+
FORMAT = "%20s %4s %5s %5s %5s %5s"
11+
12+
13+
@click.command()
14+
@click.option('--yml-food', default="", help="Yaml file with food details")
15+
@click.option('--yml-recipe', default="", help="Yaml file with recipe details")
16+
def 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))
74+
print("")
75+
76+
77+
if __name__ == "__main__":
78+
main()

0 commit comments

Comments
 (0)