forked from sunziping2016/THUCourseSpider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunning_log.py
42 lines (34 loc) · 1.13 KB
/
running_log.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
# -*- coding: utf-8 -*-
import atexit
import datetime
import json
import os
import sys
from typing import Any, Union
def save_json(obj: Any, path: str, indent: Union[int, None] = 2) -> None:
with open(path, 'w') as f:
json.dump(obj, f, indent=indent)
def load_json(path: str) -> Any:
with open(path) as f:
return json.load(f)
class RunningLog:
def __init__(self, log_path: str):
self.running = {
'start': str(datetime.datetime.now()),
'end': None,
'argv': sys.argv,
'parameters': {},
'state': 'failed'
}
def save_running_log():
print('saving running log to running-log.json')
self.running['end'] = str(datetime.datetime.now())
filename = os.path.join(log_path, 'running-log.json')
all_running = []
if os.path.isfile(filename):
all_running = load_json(filename)
all_running.append(self.running)
save_json(all_running, filename)
atexit.register(save_running_log)
def set(self, key, value):
self.running[key] = value