Spaces:
Runtime error
Runtime error
huggingface112
commited on
Commit
•
ec82168
1
Parent(s):
c0e0ce8
created Log
Browse files
log.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import utils
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import datetime as dt
|
5 |
+
|
6 |
+
class Log():
|
7 |
+
|
8 |
+
def __init__(self, path):
|
9 |
+
self.path = path
|
10 |
+
self.log = self._load_json_file(self.path)
|
11 |
+
|
12 |
+
def _load_json_file(self, path):
|
13 |
+
# if not exist return empty dict
|
14 |
+
if not os.path.exists(path):
|
15 |
+
return {}
|
16 |
+
# problem loading file return empty dict
|
17 |
+
try:
|
18 |
+
with open(path, 'r') as json_file:
|
19 |
+
data = json.load(json_file)
|
20 |
+
return data
|
21 |
+
except:
|
22 |
+
return {}
|
23 |
+
|
24 |
+
def _save_json_file(self, path, data):
|
25 |
+
with open(path, 'w') as json_file:
|
26 |
+
json.dump(data, json_file, indent=4)
|
27 |
+
|
28 |
+
def update_log(self, key):
|
29 |
+
self.log[key] = utils.time_in_beijing().strftime('%Y-%m-%d %H:%M:%S')
|
30 |
+
self._save_json_file(self.path, self.log)
|
31 |
+
|
32 |
+
def get_time(self, key):
|
33 |
+
time_str = self.log.get(key, None)
|
34 |
+
if time_str is None:
|
35 |
+
return None
|
36 |
+
return dt.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
|
37 |
+
|
38 |
+
|
39 |
+
if __name__ == '__main__':
|
40 |
+
log = Log('instance/log.json')
|
41 |
+
log.update_log('stock_update')
|
42 |
+
# print(log.log)
|