-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-generator.py
98 lines (76 loc) · 2.81 KB
/
user-generator.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
import mysql.connector
import html
connection = mysql.connector.connect(
user='root', password='', host='localhost', database='delta-test', port=3306
)
cursor = connection.cursor()
with open('Test-parser.txt', errors='ignore', encoding='utf-8') as f:
lines = f.readlines()
themes = {}
theme_names = []
temp = {}
temp_lines = []
temp_variants = []
temp2 = {}
def represents_int(s):
try:
int(s)
except ValueError:
return False
else:
return True
temp = {}
theme_name = ''
question_name = ''
temp_variants = []
right_answers = []
for line in lines:
if (line.strip() != ''):
if (represents_int(line.strip()[0]) == True):
theme_names.append(line.strip())
theme_name = line.strip()
theme_name = theme_name.replace('`', "'")
temp[line.strip()] = []
else:
if (line.strip()[0] == '*'):
question_name = line.strip()[1:]
# temp[theme_name] = []
question_name = question_name.replace('`', "'")
temp[theme_name].append(question_name)
# elif (line.strip()[1] == '.' or line.strip()[2] == '.'):
# # print(line)
# temp[theme_name][question_name].append(line.strip())
# print(temp)
theme_id = 32
try:
# for theme_name in temp:
# print(theme_name, end="\n")
# theme_name = theme_name.replace('"', "'")
# query = f'INSERT INTO question (title, theme_id) VALUES ("'+html.escape(theme_name, quote=True)+f'", {theme_id})'
# cursor.execute(query)
# print(query)
#--------------------------------------------------------------------------------------------------------------------
query = f'SELECT * FROM question where theme_id={theme_id};'
cursor.execute(query)
questions_query = cursor.fetchall()
for question in questions_query:
for theme_name in temp:
if(question[1] == theme_name.replace('"', ''').replace('<', '<').replace('>', '>')):
for variant in temp[theme_name]:
is_right = 0
if(variant[1] == '+'):
is_right = 1
variant = variant.replace("+", "", 1)
variant = variant.replace('"', "'")
# print(' ' + questions)
query = f'INSERT INTO variant (title, question_id, is_right) VALUES ("'+html.escape(variant, quote=True)+f'", {question[0]}, {is_right})'
cursor.execute(query)
print(query)
# Commit your changes in the database
connection.commit()
except Exception() as e:
# Rolling back in case of error
connection.rollback()
print(e)
# pass
print('success')