-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd-special.py
67 lines (53 loc) · 1.16 KB
/
add-special.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
import json, sys
from iterfzf import iterfzf
if len(sys.argv) != 2:
print("./add-special.py ALIAS")
sys.exit(-1)
def round_float(v):
return round(v*100)/100
alias = sys.argv[1]
try:
with open("foods.json") as f:
foods = json.load(f)
except FileNotFoundError:
foods = {}
fat, carb, prot = 0, 0, 0
total = 0
while True:
select = iterfzf(foods.keys())
if select is None:
break
f, c, p = foods[select]
while True:
amount = input(f"{select} amount> ")
try:
amount = int(amount)
except ValueError:
print("Try again")
continue
break
r = amount/100
fat += f*r
carb += c*r
prot += p*r
total += amount
if total == 0:
print("exiting...")
sys.argv(0)
while True:
new_total = input(f"{total} total> ")
if not new_total:
break
try:
total = int(new_total)
except ValueError:
print("Try again")
continue
break
r = 100/total
f = round_float(fat*r)
c = round_float(carb*r)
p = round_float(prot*r)
foods[alias] = (f, c, p)
with open("foods.json", "w") as f:
json.dump(foods, f, indent=2)