-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
59 lines (46 loc) · 1.67 KB
/
update.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
"""
update, generate a new entry
"""
import json
import os
import sys
import copy
import geolocation
import getDate
class MyStr(str):
def __lt__(self, other):
"""Override the default Equals behavior"""
if isinstance(other, self.__class__):
return int(self) < int(other)
return NotImplemented
def __gt__(self, other):
"""Override the default Equals behavior"""
if isinstance(other, self.__class__):
return int(self) > int(other)
return NotImplemented
if __name__ == "__main__":
fileDir = os.path.abspath(os.path.join(
os.path.realpath(sys.argv[0]), os.pardir))
fileName = os.path.join(fileDir, "demo.json")
with open(fileName, 'r') as fhandler:
data = json.load(fhandler)
# update index, prev+1
index = max([int(x) for x in data['problems'].keys()]) + 1
newProblem = copy.deepcopy(data['problems']['1'])
newProblem['date'] = getDate.get_date()
# newProblem['Address'] = ''
newProblem['Address'] = data['problems'][str(index - 1)]['Address']
newProblem['Geolocation'] = geolocation.getGeolocation()
newProblem['problem'] = ''
newProblem['description'] = ''
newProblem['notes'] = ''
newProblem['link']['lintcode'] = ''
newProblem['link']['jiuzhangsuanfa'] = ''
data['problems'][str(index)] = newProblem
newData = dict()
newData['problems'] = dict()
for key, val in data['problems'].items():
newData['problems'][MyStr(key)] = val
with open(fileName, 'w') as fhandler:
json.dump(newData, fhandler, indent=4, sort_keys=True)
print("Job Finished.")