-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_students_db.py
198 lines (167 loc) · 8.35 KB
/
populate_students_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
184
185
186
187
188
189
190
191
192
193
from sqlalchemy import create_engine
from sqlalchemy.sql import text, func, select, and_, or_, not_, desc, bindparam
from db_tables import metadata, Questions, Answers, Results, Students, StudentsTest, Graphs
import pandas as pd
engine = create_engine('sqlite:///quizDB.db?check_same_thread=False', echo=True)
conn = engine.connect()
metadata.create_all(engine)
#CLEAR RESULTS TABLE
if engine.dialect.has_table(engine.connect(), "results"):
conn.execute(Results.delete())
df_questions = pd.read_excel('quizApp/data/question_table.xlsx', 'Sheet1')
#check for table and if it is there clear before writing to
if engine.dialect.has_table(engine.connect(), "questions"):
conn.execute(Questions.delete())
conn.execute(Questions.insert(), [{'question_id':data.question_id,
'dataset':data.dataset,
'question':data.question,
'question_type':data.question_type}
for ix, data in df_questions.iterrows()])
df_answers = pd.read_excel('quizApp/data/answer_table.xlsx', 'Sheet1')
#check for table and if it is there clear before writing to
if engine.dialect.has_table(engine.connect(), "answers"):
conn.execute(Answers.delete())
conn.execute(Answers.insert(), [{'answer_id':data.answer_id,
'question_id':data.question_id,
'answer':data.answer,
'correct':data.correct}
for ix, data in df_answers.iterrows()])
df_graphs = pd.read_excel('quizApp/data/graph_table.xlsx', 'Sheet1')
#check for table and if it is there clear before writing to
if engine.dialect.has_table(engine.connect(), "graphs"):
conn.execute(Graphs.delete())
conn.execute(Graphs.insert(), [{'graph_id':data.graph_id,
'dataset':data.dataset,
'graph_location':data.graph_location}
for ix, data in df_graphs.iterrows()])
student_question_list = [[(1, 2), (3, 2), (4, 0), (2, 1), (5, 0), (0, 0)],
[(1, 2), (0, 2), (5, 1), (2, 0), (3, 0), (4, 1)],
[(3, 0), (2, 1), (4, 0), (5, 1), (0, 0), (1, 2)],
[(5, 2), (2, 2), (0, 1), (1, 0), (3, 1), (4, 0)],
[(2, 2), (0, 1), (3, 1), (4, 0), (1, 1), (5, 1)],
[(0, 0), (3, 0), (1, 0), (2, 2), (5, 2), (4, 2)],
[(4, 2), (1, 1), (5, 2), (0, 0), (3, 1), (2, 1)],
[(4, 2), (3, 0), (1, 2), (0, 1), (5, 2), (2, 1)],
[(3, 1), (2, 0), (4, 2), (1, 1), (0, 1), (5, 2)],
[(0, 2), (4, 1), (3, 0), (5, 0), (1, 1), (2, 0)],
[(5, 1), (4, 1), (0, 2), (3, 2), (1, 2), (2, 0)],
[(2, 1), (5, 0), (0, 2), (3, 2), (4, 2), (1, 1)],
[(3, 1), (5, 2), (4, 1), (0, 2), (2, 0), (1, 0)],
[(1, 1), (5, 1), (2, 2), (4, 0), (3, 1), (0, 2)],
[(2, 0), (1, 0), (5, 0), (4, 1), (0, 2), (3, 2)],
[(1, 1), (5, 1), (0, 2), (4, 2), (2, 1), (3, 1)],
[(5, 1), (4, 2), (2, 0), (1, 2), (3, 2), (0, 1)],
[(0, 0), (2, 2), (1, 0), (4, 1), (5, 2), (3, 2)],
[(0, 1), (1, 2), (5, 2), (2, 0), (4, 2), (3, 0)],
[(1, 0), (3, 2), (0, 0), (2, 2), (4, 0), (5, 0)],
[(3, 2), (2, 1), (4, 1), (1, 0), (5, 1), (0, 0)],
[(1, 0), (3, 2), (5, 0), (0, 1), (4, 1), (2, 2)],
[(5, 2), (3, 1), (1, 1), (0, 0), (4, 0), (2, 2)],
[(4, 1), (0, 0), (3, 1), (2, 1), (5, 1), (1, 0)],
[(5, 0), (2, 0), (0, 1), (3, 0), (1, 1), (4, 2)],
[(0, 2), (4, 0), (1, 1), (3, 1), (2, 2), (5, 0)],
[(2, 0), (0, 0), (3, 0), (1, 2), (5, 0), (4, 1)],
[(0, 1), (4, 2), (2, 1), (5, 2), (1, 0), (3, 0)],
[(5, 0), (4, 0), (2, 2), (3, 0), (1, 2), (0, 1)],
[(4, 0), (1, 2), (2, 1), (5, 1), (0, 2), (3, 2)]]
#temp created student id list
# question_student_id_list = [x + 1 for x in range(30)]
# heuristic_student_id_list = [x + 1 for x in range(30,60)]
#read in student lists
df_sid = pd.read_csv('student_id_list.csv')
df_sid.Questions = df_sid.Questions.apply(lambda x: int(x))
df_sid.Heuristics = df_sid.Heuristics.apply(lambda x: int(x))
question_student_id_list = [int(x) for x in list(df_sid.Questions)]
heuristic_student_id_list = [int(x) for x in list(df_sid.Heuristics)]
combined_id_list = question_student_id_list + heuristic_student_id_list
#check for table and if it is there clear before writing to
if engine.dialect.has_table(engine.connect(), "students"):
conn.execute(Students.delete())
conn.execute(Students.insert(), [{'student_id':sid,
'progress':'pre_test',
'opt_in':'na'}
for sid in combined_id_list])
def create_student_data(sid_list, student_question_list, test, group):
if test == 'pre_test' or test == 'post_test':
question_list = [x[:3] for x in student_question_list]
else:
question_list = [x[3:] for x in student_question_list]
for n, student in enumerate(question_list):
student_id = sid_list[n]
#count the order for each student per test
order = 0
for ix, graph in enumerate(student):
student_test_id = int(str(student_id)+str(ix))
dataset = graph[0]
if dataset == 0:
#not using 0 precursor for easier use in excel
graph_id = graph[1] + 1
else:
graph_id = int(str(dataset)+str(graph[1]+1))
if test == 'pre_test' or test == 'post_test':
order += 1
if dataset == 0:
question_id = 5
else:
question_id = int(str(dataset)+str(5))
#write row to db
conn.execute(StudentsTest.insert(), {'student_id':student_id,
'test':test,
'graph_id':graph_id,
'dataset':dataset,
'question_id':question_id,
'order':order,
'complete':'no'})
elif group == 'heuristic':
for x in range(6,9):
order += 1
if dataset == 0:
question_id = x
else:
question_id = int(str(dataset)+str(x))
#write row to db
conn.execute(StudentsTest.insert(), {'student_id':student_id,
'test':test,
'graph_id':graph_id,
'dataset':dataset,
'question_id':question_id,
'order':order,
'complete':'no'})
else:
#multiple choice questions
for x in range(3):
order += 1
if dataset == 0:
question_id = x + 1
else:
question_id = int(str(dataset)+str(x + 1))
#write row to db
conn.execute(StudentsTest.insert(), {'student_id':student_id,
'test':test,
'graph_id':graph_id,
'dataset':dataset,
'question_id':question_id,
'order':order,
'complete':'no'})
if test == 'training':
#only have rating question for training
order += 1
if dataset == 0:
question_id = 4
else:
question_id = int(str(dataset)+str(4))
#write row to db
conn.execute(StudentsTest.insert(), {'student_id':student_id,
'test':test,
'graph_id':graph_id,
'dataset':dataset,
'question_id':question_id,
'order':order,
'complete':'no'})
#check for table and if it is there clear before writing to
if engine.dialect.has_table(engine.connect(), "students_test"):
conn.execute(StudentsTest.delete())
#create all the student_test table data
for test in ['pre_test', 'training', 'post_test']:
create_student_data(question_student_id_list, student_question_list, test, 'question')
create_student_data(heuristic_student_id_list, student_question_list, test, 'heuristic')