-
Notifications
You must be signed in to change notification settings - Fork 0
/
dazeutils.py
96 lines (77 loc) · 2.94 KB
/
dazeutils.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
from datetime import date
from datetime import timedelta
import json
import sys
import os
locationLog = '/Users/mikayla/Code/sandbox/python/daze/newLocationLog.json'
class Daze:
def __init__(self, json):
self.json = json
self.deserialize(json)
def findPlace(self, placename):
for (p, alts) in self.places.items():
if placename == p:
return p
if placename in alts:
return p
return None
def serialize(self):
return {'places': self.places,
'log': {k.isoformat():v for (k, v) in self.dateDict.items()}
}
def deserialize(self, dazejson):
self.places = dazejson['places']
self.dateDict = {toDate(k):self.findPlace(v) for (k, v) in dazejson['log'].items()}
self.placeDict = {k:[] for k in self.places.keys()}
for (d, p) in self.dateDict.items():
try:
self.placeDict[self.findPlace(p)].append(d)
except KeyError:
print(p)
print(self.findPlace(p))
def add(self, indate, place):
if type(indate) is str:
indate = toDate(indate)
if place not in self.places.keys():
place = self.findPlace(place)
self.dateDict[indate] = place
self.placeDict[place].append(indate)
def remove(self, outdate):
if type(outdate) is str:
outdate = toDate(outdate)
place = self.dateDict.pop(outdate)
self.placeDict[place].remove(outdate)
def summarize(self, firstdate=date.min, lastdate=date.max):
summary = {}
def isInDateRange(xdate):
return firstdate <= xdate <= lastdate
for p in self.places.keys():
summary[p] = len(list(filter(isInDateRange, self.placeDict[p])))
actualfirst = max(firstdate, min(self.dateDict.keys()))
actuallast = min(lastdate, max(self.dateDict.keys()))
return summary, sum(summary.values()), actualfirst, actuallast
def toDate(datestring):
return date(*[int(x) for x in datestring.split('-')])
def fileToDaze(filename):
if filename is None:
try:
with open(os.path.expandvars("$DAZE/settings.json"), 'r') as f:
settings = json.load(f)
filename = os.path.expandvars(settings['log'])
except:
print("Had an error getting log file from settings.")
filename = locationLog
with open(filename, 'r') as f:
data = json.load(f)
return Daze(data)
def dazeToFile(daze, filename):
if filename is None:
try:
with open(os.path.expandvars("$DAZE/settings.json"), 'r') as f:
settings = json.load(f)
filename = os.path.expandvars(settings['log'])
except:
print("Had an error getting log file from settings.")
filename = locationLog
with open(filename, 'w') as f:
json.dump(daze.serialize(), f)