-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.py
81 lines (67 loc) · 2.69 KB
/
Utilities.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
import time
import traceback
import json
import re
def delayed_every(first_delay, delay, task):
time.sleep(first_delay)
next_time = time.time() + delay
while True:
time.sleep(max(0, next_time - time.time()))
try:
task()
except Exception:
traceback.print_exc()
# skip tasks if we are behind schedule:
next_time += (time.time() - next_time) // delay * delay + delay
def every(delay, task):
next_time = time.time() + delay
while True:
time.sleep(max(0, next_time - time.time()))
try:
task()
except Exception:
traceback.print_exc()
# skip tasks if we are behind schedule:
next_time += (time.time() - next_time) // delay * delay + delay
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def load_dirty_json(dirty_json):
regex_replace = [(r"([ \{,:\[])(u)?'([^']+)'", r'\1"\3"'), (r" False([, \}\]])", r' false\1'), (r" True([, \}\]])",
r' true\1')]
for r, s in regex_replace:
dirty_json = re.sub(r, s, dirty_json)
clean_json = json.loads(dirty_json)
return clean_json
def get_inner_json_value(json_element, path):
if path == '':
return json_element
first_dot_index = path.find('.')
first_brace_index = path.find('[')
if first_dot_index == -1 and first_brace_index == -1:
return json_element[path]
if first_dot_index < first_brace_index: # if object
object_name = path[0:first_dot_index]
return get_inner_json_value(json_element[object_name], path[first_dot_index:])
if first_brace_index < first_dot_index: # array
array_name = path[0:first_brace_index]
closing_brace_index = path.find(']')
array_target_index = int(path[first_brace_index+1:closing_brace_index])
remaining_path = path[closing_brace_index+1:]
if len(remaining_path) > 1 and remaining_path[0] == '.':
remaining_path = remaining_path[1:]
return get_inner_json_value(json_element[array_name][array_target_index], remaining_path)
else:
return None
def get_json_value(data, path):
last_snapshot = data
if last_snapshot == '':
return 0
fixed_last_snapshot = re.sub('([{,])(\w+)([},:])', '\\1\"\\2\"\\3', str(last_snapshot, 'utf-8'))
fixed_last_snapshot = fixed_last_snapshot.replace("\'", '"')
fixed_last_snapshot = fixed_last_snapshot.replace(":nan", ':null')
last_snapshot_json_document = json.loads(fixed_last_snapshot)
return get_inner_json_value(last_snapshot_json_document, path)