-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathhr_db.py
executable file
·183 lines (152 loc) · 5.88 KB
/
hr_db.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
172
173
174
175
176
177
178
179
180
181
182
183
#! /usr/bin/env python3
import collections
import sqlite3
import glob
import json
import os
import sys
import logging
my_fields = ['id', 'slug', 'name',
'track_id', 'track_slug', 'track_name',
'contest_slug',
'primary_contest_refid',
# 'custom_case', 'is_custom', 'custom',
'difficulty_name',
'custom_checker_language', 'checker_program',
'default_language', 'status'
]
def reorder_fields(f):
if f in my_fields:
return my_fields.index(f)
return len(my_fields) + 1
class hr_db:
def __init__(self):
self.conn = sqlite3.connect('hackerrank.db')
self.tables = {}
self.inserts = {}
self.ids = {}
def load_models(self):
models = []
# load in memory models from offline tree
logging.debug("scanning files...")
for i in glob.iglob(os.path.join(os.path.dirname(__file__),
"offline", "models", "**", "*.json"),
recursive=True):
with open(i, "rb") as f:
data = json.load(f)
if data['status'] is True:
m = data['model']
models.append(m)
logging.debug("%d models found", len(models))
# analyze models
for m in models:
self.analyse(m, "challenge")
# create the database tables
c = self.conn.cursor()
for name, fields in self.tables.items():
sql = "drop table if exists `{}`".format(name)
c.execute(sql)
sql = "create table if not exists `{}` (\n".format(name)
sql += ",\n".join("`{}` {}".format(k, fields[k])
for k in sorted(fields.keys(), key=reorder_fields))
sql += ")"
c.execute(sql)
sql = "insert into `{}` (\n".format(name)
sql += ",".join("`{}`".format(k) for k in fields.keys())
sql += ") values ("
sql += (",?" * len(fields))[1:]
sql += ")"
self.inserts[name] = sql
self.ids[name] = set()
logging.info("table: %s %d", name, len(fields))
c.close()
self.conn.commit()
# store models into the database
logging.debug("load models")
c = self.conn.cursor()
for m in models:
self.charge(c, m, "challenge")
c.close()
self.conn.commit()
def charge(self, c, values, tablename):
if values['id'] in self.ids[tablename]:
return
columns = []
for k in self.tables[tablename].keys():
if k.endswith("_refid"):
k = k[:-6]
v = values.get(k)
if isinstance(v, dict):
if "id" in v:
self.charge(c, v, k)
v = v["id"]
else:
v = json.dumps(v)
elif isinstance(v, list):
if len(v) > 0 and not isinstance(v[0], str):
v = json.dumps(v)
else:
try:
v = "|".join(v)
except TypeError:
print(k)
raise
columns.append(v)
c.execute(self.inserts[tablename], columns)
self.ids[tablename].add(values['id'])
def analyse(self, values, tablename):
if tablename not in self.tables:
logging.debug("discover table %s", tablename)
fields = self.tables[tablename] = collections.OrderedDict()
else:
fields = self.tables[tablename]
for k, v in values.items():
if k.endswith('_template') or k.endswith('_template_head') or k.endswith('_template_tail'): continue
if k.endswith('_skeliton_head') or k.endswith('_skeliton_tail'): continue
if isinstance(v, str):
t = "text"
elif isinstance(v, bool):
t = "boolean"
elif isinstance(v, int):
# enforce type for these fields
if k in ["max_score", "factor", "success_ratio"]:
t = "float"
else:
t = "integer"
if k == "id":
t += " primary key not null"
elif isinstance(v, float):
t = "float"
elif isinstance(v, list):
t = "text"
elif v is None:
continue
elif isinstance(v, dict):
if "id" in v:
t = "integer"
self.analyse(v, k)
k += "_refid"
else:
t = "text"
else:
logging.error("unknown type: %s", type(v))
if k in fields:
if fields[k] != t:
logging.warning("mismatch {:20} type={} found={} value={} of {}".format(k, fields[k], t, repr(v), tablename))
else:
fields[k] = t
def set_logging(verbose):
""" set up a colorized logger """
if sys.stdout.isatty():
logging.addLevelName(logging.DEBUG, "\033[0;32m%s\033[0m" % logging.getLevelName(logging.DEBUG))
logging.addLevelName(logging.INFO, "\033[1;33m%s\033[0m" % logging.getLevelName(logging.INFO))
logging.addLevelName(logging.WARNING, "\033[1;35m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName(logging.ERROR, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
if verbose:
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG, datefmt='%H:%M:%S')
else:
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.ERROR, datefmt='%H:%M:%S')
if __name__ == '__main__':
set_logging(True)
menu = hr_db()
menu.load_models()