-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
174 lines (124 loc) · 4.22 KB
/
utils.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import pickle
import datetime
from submits import Verdicts
from IPython.display import Image, display, HTML
import shutil
import config
import json
import pprint
pp = pprint.PrettyPrinter(indent=4)
def save_obj(filepath, obj):
dir = os.path.dirname(filepath)
os.makedirs(dir, exist_ok=True)
with open( filepath, 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(filepath ):
with open(filepath, 'rb') as f:
return pickle.load(f)
def make_html(src):
return '<img src="{}" style="max-width:100px;display:inline;margin:1px"/>'.format(src)
def print_images_row(images):
s = ""
for x in images:
s += make_html(x)
display(HTML(s))
def UNIX_from_datetime(dtstr):
"""
INPUT: 2021-06-21T14:10:58.765
"""
y = int(dtstr[0:4])
m = int(dtstr[5:7])
d = int(dtstr[8:10])
h = int(dtstr[11:13])
mins = int(dtstr[14:16])
secs = int(dtstr[17:19])
us = int(dtstr[20:23]) * 1000
#print(y,m,d,h,mins,secs, us)
return int(datetime.datetime(y, m, d, h, mins, secs, us).timestamp() * 1000)
def from_UNIX(ts, fmt='%d-%m-%Y %H:%M:%S'):
return datetime.datetime.fromtimestamp(ts / 1000).strftime(fmt)
def find_task_def(json, name):
for t in json:
if (t["name"] == name):
return t
return None
def generate_catype(cats, types):
return ".".join(sorted(cats)) + "__" + ".".join(sorted(types))
def catype_to_features(catype):
if not (catype in config.catypes_to_features()):
raise Exception("Catype does not exist!")
return config.catypes_to_features(catype)
def extract_text_query(text):
if not (text.startswith("|results|")):
return None
t = text[9:]
i = t.find(";")
t = t[0:i]
xx = [x.strip() for x in t.split(">>")]
return xx
def determine_submit_result(r, verdicts : Verdicts):
node = r["request"]["session"]
c = None
if (r["response"] == None):
c = "TIMEOUT"
elif (r["response_code"] == 404):
c = "SERVER_LAG"
elif (r["response_code"] == 401):
c = "LOGGED_OUT"
elif (r["response_code"] == 412):
c = "F"
elif ("submission" in r["response"]):
if (r["response"]["submission"] == "CORRECT"):
c = "T"
elif (r["response"]["submission"] == "INDETERMINATE"):
c = verdicts.submit_to_verdict(r["timestamp"], node)
else:
c = "F"
return c, node
def cache_index_get(team_name):
cache_indexf_fpth = config.cache_index_file()
cache_index = None
with open(cache_indexf_fpth) as ifs:
cache_index = json.load(ifs)
return team_name in cache_index and cache_index[team_name]
def cache_index_set(team_name):
cache_indexf_fpth = config.cache_index_file()
cache_index = None
with open(cache_indexf_fpth) as ifs:
cache_index = json.load(ifs)
cache_index[team_name] = True
with open(cache_indexf_fpth, "w") as ofs:
str = json.dumps(cache_index)
ofs.write(str)
def find_submit_positions(submit_times, times, vid_positions, reported):
submit_positions = []
submit_reported = []
for j, st in enumerate(submit_times):
prev_t = 0.0
#print(f"search: {st}")
for i, t in enumerate(times):
if (st > times[-1]):
submit_times[j] = None
continue
#print(f"{prev_t} < {st} <= {t}")
if (prev_t < st and st <= t):
#print("->")
submit_positions.append(vid_positions[i])
submit_reported.append(reported[i])
break
prev_t = t
for i in range(len(submit_times)):
if None in submit_times:
submit_times.remove(None)
if len(submit_times) != len(submit_positions):
print("vid_positions:")
pp.pprint(vid_positions)
print("submit_times:")
pp.pprint(submit_times)
print("times:")
pp.pprint(times)
print("submit_positions:")
pp.pprint(submit_positions)
raise Exception("Positon not found!")
return submit_positions, submit_reported