This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Suggest.py
165 lines (110 loc) · 3.67 KB
/
Suggest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
from FoodLogger import *
class Suggest:
def sortOpts(self, num=15):
def perSort(x):
yem = x[1]
f = float(yem.fat)
p = float(yem.prot)
pff = (f - self.allowed_fat)/f
ppp = (p - self.allowed_prot)/p
return pff + ppp
for x,v in self.singles.items():
self.portions[x] = v
sorted_ports = sorted(iter(self.portions.items()), key=perSort, reverse=True)
Yemek.printFullHeader()
count = 0
for x,v in sorted_ports:
if count==num:break
count += 1
print(v.printout(pre="%2d: " % count))
def __init__(self, foodlist_obj, kc, carb_obj, prot, fat, tag=""):
self.flist = foodlist_obj.foodmap
self.allowed_kc = kc
self.allowed_carb = carb_obj
self.allowed_prot = prot
self.allowed_fat = fat
self.wanted_tag = tag
#This is more for my own curiousity
def lowCalHighPF(self, num=15):
def makeit(x):
yem = x[1]
f = float(yem.fat)
p = float(yem.prot)
kc = float(yem.kC)
return 1/((kc/p) + (kc/f))
sorted_ports = sorted(iter(self.flist.items()), key=makeit, reverse=True)
Yemek.printFullHeader()
count = 0
for x,v in sorted_ports:
if count==num:break
count += 1
print(v.printout(pre="%2d: " % count))
def suggestSomething(self):
outnow = (' '*(Yemek.buffer-8)) + "Allow : %s" % Yemek.outformat
outnow = '\n'+'%'.join(outnow.split('%')[:-2])+'\n'
print(outnow % (
int(self.allowed_kc),
self.allowed_carb.total, self.allowed_carb.fibre, self.allowed_carb.sugstar, self.allowed_carb.bad,
self.allowed_prot,
self.allowed_fat))
self.suggestSingles()
self.suggestPortions()
self.sortOpts()
def minimumScale(self, yem):
kc_scale = float(self.allowed_kc)/yem.kC
yem = yem.scaled(kc_scale)
if yem.carb.bad > self.allowed_carb.bad:
carb_scale = float(self.allowed_carb.bad)/yem.carb.bad
try:
scale = float(1)/( int( float(1)/carb_scale ) )
except ZeroDivisionError:
scale = 0.1
yem = yem.scaled(scale)
if yem.fat > self.allowed_fat:
fat_scale = float(self.allowed_fat)/yem.fat
scale = float(1)/( int( float(1)/fat_scale ) )
yem = yem.scaled(scale)
if yem.prot > self.allowed_prot:
protein_scale = float(self.allowed_prot)/yem.prot
scale = float(1)/( int( float(1)/protein_scale ) )
yem = yem.scaled(scale)
return yem
def suggestSingles(self):
# Filter singles again for kc limit
singles1 = dict((x,v) for x,v in self.flist.items()\
if len(v.unit)>2 and (
( (v.fat/v.carb.bad > 1) or (v.prot/v.carb.bad > 1) )
if (self.wanted_tag=="")\
else (self.wanted_tag in v.tags.tags)
))
self.singles={}
for x,y in singles1.items():
v = self.minimumScale(y)
# if (v.kC < self.allowed_kc
# and v.carb.bad < self.allowed_carb.bad
# and v.fat < self.allowed_fat
# and v.prot < self.allowed_prot):
self.singles[x]=v
def suggestPortions(self):
portions = dict((x,v) for x,v in self.flist.items() if len(v.unit)<=2) # g, ml, etc
self.portions = {}
#scale portions to a minimum, and then chuck out the unrealistic ones
for name in portions:
yem = portions[name]
if self.wanted_tag!="":
if not self.wanted_tag in yem.tags.tags:continue
new_scale = self.minimumScale(yem)
if new_scale.prot <= 0.1 and self.allowed_prot >=0:continue
if new_scale.unit == 'g':
if new_scale.per < 20.0 or new_scale.per > 350:
continue
if new_scale.unit == 'ml':
if new_scale.per < 100.0 or new_scale.per > 1000:
continue
self.portions[name] = new_scale
#w = FoodLogger()
#w.makeTotals(w.date)
#p = w.pie
#s = Suggest(w.foodlist, p.macro_kc, p.macro_carb, p.macro_prot, p.macro_fat)
#s = Suggest(w.foodlist, 300, p.carb_total, p.protein_total, p.fat_total)