-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.py
62 lines (50 loc) · 1.56 KB
/
logging.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
#!/usr/bin/env python3
import time
from tinydb import TinyDB, where
db = TinyDB('my_diet.json')
class Meal(object):
def __init__(self, name, cals, fat, carbs, protein, date):
self.name = name
self.cals = cals
self.fat = fat
self.carbs = carbs
self.protein = protein
if date:
self.date = date
else:
dt = time.time()
dt = round(time.mktime(dt)/84000)
self.date = dt
def log_diet(name, cals, fat, carbs, protein, date):
dt = time.strptime(date, "%m/%d/%Y")
dt = round(time.mktime(dt)/86400) # number of days since the epoch
meal = {'name': name,
'cals':cals,
'fat': fat,
'carbs': carbs,
'protein': protein
}
db.insert({'date': dt, 'meal': meal})
def get_data(start, end):
start = int(time.mktime(time.strptime(start, "%m/%d/%Y")))
start = start/86400
end = int(time.mktime(time.strptime(end, "%m/%d/%Y")))
end = end/86400
data = db.search((where('date') >= start) & (where('date') <= end))
return data
def dict_merge(target, *args):
# Merge multiple dicts
if len(args) > 1:
for obj in args:
dict_merge(target, obj)
return target
# Recursively merge dicts and set non-dict values
obj = args[0]
if not isinstance(obj, dict):
return obj
for k, v in obj.iteritems():
if k in target and isinstance(target[k], dict):
dict_merge(target[k], v)
else:
target[k] = deepcopy(v)
return target