-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_patterns.py
83 lines (66 loc) · 2.53 KB
/
insert_patterns.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
#!/usr/bin/python3
"""
Author: Konstantinos Liosis
File: insert_patterns.py
Desc: Patterns entry
"""
import csv
def patterns(cursor, db_conn, files):
"""
:param cursor:
:param files:
:return:
"""
# pattern_class tables (only 2 classes)
try:
query = "INSERT INTO pattern_class(alphabet, num_classes)" \
"VALUES (%s, %s)"
args = [("abcde", "5"), ("abcdefghij", "10")]
cursor.executemany(query, args)
db_conn.commit()
except Exception as e:
print(e)
# First insert the patterns, then positions
for filename in [x for x in files if "positions" not in x]:
print("Inserting {}".format(filename))
file_type, city, class_type = filename.split("_")
class_type = 1 if class_type == '5' else 2
# Find geolocation uid
geolocation = 0
try:
query = "SELECT geol_uid FROM city WHERE LOWER(name) = %s"
cursor.execute(query, [city])
geolocation = cursor.fetchall()[0][0]
except Exception as e:
print(e)
# First count how many are already inserted to
# make sure primary_key gets properly incremented
num_of_pat = 0
try:
query = "SELECT COUNT(*) FROM pattern"
cursor.execute(query)
num_of_pat = int(cursor.fetchone()[0])
except Exception as e:
print(e)
query = "INSERT INTO pattern(ptrn_id, ptrn, length, occurences, geol_uid, ptrn_c_uid)" \
"VALUES (%s, %s, %s, %s, %s, %s)"
final_data = []
with open("pattern_data/"+filename, 'r') as csv_file:
input_data = csv.reader(csv_file)
for idx, row in enumerate(input_data):
if idx > 1:
final_data.append((int(row[0]) + num_of_pat, row[1], row[2], row[3], geolocation, class_type))
cursor.executemany(query, final_data)
db_conn.commit()
# Insert positions
query = "INSERT INTO result_position(ptrn_id, wvl_uid, position)" \
"VALUES (%s, %s, %s)"
for filename in [x for x in files if "positions" in x]:
final_data = []
with open("pattern_data/"+filename, 'r') as csv_file:
input_data = csv.reader(csv_file)
for idx, row in enumerate(input_data):
if idx > 1:
final_data.append((int(row[0]) + num_of_pat, row[1], row[2]))
cursor.executemany(query, final_data)
db_conn.commit()