learnapp.py
13.4 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# python standard library
from contextlib import contextmanager # `with` statement in db sessions
import logging
from os import path, sys
from datetime import datetime
# user installed libraries
import bcrypt
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import networkx as nx
# this project
from models import Student, Answer, Topic, StudentTopic
from knowledge import StudentKnowledge
from factory import QFactory
from tools import load_yaml
# setup logger for this module
logger = logging.getLogger(__name__)
class LearnAppException(Exception):
pass
# ============================================================================
# LearnApp - application logic
# ============================================================================
class LearnApp(object):
def __init__(self, config_file):
# state of online students
self.online = {}
config = load_yaml(config_file)
# connect to database and checks for registered students
self.db_setup(config['database'])
# dependency graph shared by all students
self.deps = build_dependency_graph(config)
# add topics from dependency graph to the database, if missing
self.db_add_missing_topics(self.deps.nodes())
# ------------------------------------------------------------------------
# login
# ------------------------------------------------------------------------
def login(self, uid, pw):
with self.db_session() as s:
student = s.query(Student).filter(Student.id == uid).one_or_none()
if student is None:
logger.info(f'User "{uid}" does not exist!')
return False # student does not exist
if bcrypt.checkpw(pw.encode('utf-8'), student.password):
if uid in self.online:
logger.warning(f'User "{uid}" already logged in, overwriting state')
else:
logger.info(f'User "{uid}" logged in')
tt = s.query(StudentTopic).filter(StudentTopic.student_id == uid)
state = {t.topic_id:
{
'level': t.level,
'date': datetime.strptime(t.date, "%Y-%m-%d %H:%M:%S.%f")
} for t in tt}
self.online[uid] = {
'number': student.id,
'name': student.name,
'state': StudentKnowledge(self.deps, state=state),
# 'learning': None, # current topic learning
}
return True
else:
logger.info(f'User "{uid}" wrong password!')
return False
# ------------------------------------------------------------------------
# logout
# ------------------------------------------------------------------------
def logout(self, uid):
del self.online[uid]
logger.info(f'User "{uid}" logged out')
# ------------------------------------------------------------------------
# change_password
# ------------------------------------------------------------------------
def change_password(self, uid, pw):
if not pw:
return False
with self.db_session() as s:
u = s.query(Student).get(uid)
u.password = bcrypt.hashpw(pw.encode('utf-8'), bcrypt.gensalt())
logger.info(f'User "{uid}" changed password')
return True
# ------------------------------------------------------------------------
# checks answer (updating student state) and returns grade.
# ------------------------------------------------------------------------
def check_answer(self, uid, answer):
knowledge = self.online[uid]['state']
ref, grade = knowledge.check_answer(answer) # also moves to next question
logger.info(f'User "{uid}" got {grade:.2} in question "{ref}"')
if knowledge.get_current_question() is None:
# finished topic, save into database
finished_topic = knowledge.get_current_topic()
level = knowledge.get_topic_level(finished_topic)
date = str(knowledge.get_topic_date(finished_topic))
finished_questions = knowledge.get_finished_questions()
logger.info(f'User "{uid}" finished "{finished_topic}"')
with self.db_session(autoflush=False) as s:
# save topic
a = s.query(StudentTopic).filter_by(student_id=uid, topic_id=finished_topic).one_or_none()
if a is None:
# insert new studenttopic into database
t = s.query(Topic).get(finished_topic)
a = StudentTopic(level=level, date=date, topic=t)
u = s.query(Student).get(uid)
u.topics.append(a)
else:
# update studenttopic in database
a.level = level
a.date = date
s.add(a)
logger.debug(f'Saved topic "{finished_topic}" into database')
# save answered questions from finished_questions list
s.add_all([
Answer(
ref=q['ref'],
grade=q['grade'],
starttime=str(q['start_time']),
finishtime=str(q['finish_time']),
student_id=uid,
topic_id=finished_topic)
for q in finished_questions])
logger.debug(f'Saved {len(finished_questions)} answers into database')
return grade
# ------------------------------------------------------------------------
# Start new topic
# ------------------------------------------------------------------------
def start_topic(self, uid, topic):
try:
ok = self.online[uid]['state'].init_topic(topic)
except KeyError as e:
logger.warning(f'User "{uid}" tried to open nonexistent topic: "{topic}"')
raise e
else:
if ok:
logger.info(f'User "{uid}" started "{topic}"')
else:
logger.warning(f'User "{uid}" restarted "{topic}"')
return ok
# ------------------------------------------------------------------------
# Start new topic
# ------------------------------------------------------------------------
# def start_learning(self, uid, topic):
# try:
# ok = self.online[uid]['state'].init_learning(topic)
# except KeyError as e:
# logger.warning(
# f'User "{uid}" tried to open nonexistent topic: "{topic}"')
# raise e
# else:
# if ok:
# logger.info(f'User "{uid}" started "{topic}"')
# else:
# logger.warning(f'User "{uid}" denied locked "{topic}"')
# return ok
# ------------------------------------------------------------------------
# Fill db table 'Topic' with topics from the graph if not already there.
# ------------------------------------------------------------------------
def db_add_missing_topics(self, nn):
with self.db_session() as s:
tt = [t[0] for t in s.query(Topic.id)] # db list of topics
missing_topics = [Topic(id=n) for n in nn if n not in tt]
if missing_topics:
s.add_all(missing_topics)
logger.info(f'Added {len(missing_topics)} new topics to the database')
# ------------------------------------------------------------------------
# setup and check database
# ------------------------------------------------------------------------
def db_setup(self, db):
logger.info(f'Checking database "{db}":')
engine = create_engine(f'sqlite:///{db}', echo=False)
self.Session = sessionmaker(bind=engine)
try:
with self.db_session() as s:
n = s.query(Student).count()
m = s.query(Topic).count()
q = s.query(Answer).count()
except Exception:
logger.critical(f'Database "{db}" not usable!')
sys.exit(1)
else:
logger.info(f'{n:6} students')
logger.info(f'{m:6} topics')
logger.info(f'{q:6} answers')
# ------------------------------------------------------------------------
# helper to manage db sessions using the `with` statement, for example
# with self.db_session() as s: s.query(...)
# ------------------------------------------------------------------------
@contextmanager
def db_session(self, **kw):
session = self.Session(**kw)
try:
yield session
session.commit()
except Exception as e:
session.rollback()
raise e
finally:
session.close()
# ========================================================================
# methods that do not change state (pure functions)
# ========================================================================
# ------------------------------------------------------------------------
def get_student_name(self, uid):
return self.online[uid].get('name', '')
# ------------------------------------------------------------------------
def get_student_state(self, uid):
return self.online[uid]['state'].get_knowledge_state()
# ------------------------------------------------------------------------
def get_student_progress(self, uid):
return self.online[uid]['state'].get_topic_progress()
# ------------------------------------------------------------------------
def get_student_question(self, uid):
return self.online[uid]['state'].get_current_question() # dict
# ------------------------------------------------------------------------
def get_student_question_type(self, uid):
return self.online[uid]['state'].get_current_question()['type']
# ------------------------------------------------------------------------
def get_student_topic(self, uid):
return self.online[uid]['state'].get_current_topic() # str
# ------------------------------------------------------------------------
def get_title(self):
return self.deps.graph['title']
# ------------------------------------------------------------------------
def get_topic_name(self, ref):
return self.deps.node[ref]['name']
# # ------------------------------------------------------------------------
# def get_topic_type(self, ref):
# return self.deps.node[ref]['type']
# ------------------------------------------------------------------------
def get_current_public_dir(self, uid):
topic = self.online[uid]['state'].get_current_topic()
p = self.deps.graph['path']
return path.join(p, topic, 'public')
# ============================================================================
# Builds a digraph.
#
# First, topics such as `computer/mips/exceptions` are added as nodes
# together with dependencies. Then, questions are loaded to a factory.
#
# g.graph['path'] base path where topic directories are located
# g.graph['title'] title defined in the configuration YAML
# g.graph['database'] sqlite3 database file to use
#
# Nodes are the topic references e.g. 'my/topic'
# g.node['my/topic']['name'] name of the topic
# g.node['my/topic']['questions'] list of question refs defined in YAML
# g.node['my/topic']['factory'] dict with question factories
#
# Edges are obtained from the deps defined in the YAML file for each topic.
# ----------------------------------------------------------------------------
def build_dependency_graph(config={}):
logger.info('Building graph and loading questions:')
# create graph
prefix = config.get('path', '.')
title = config.get('title', '')
database = config.get('database', 'students.db')
g = nx.DiGraph(path=prefix, title=title, database=database)
# iterate over topics and build graph
topics = config.get('topics', {})
tcount = qcount = 0 # topic and question counters
for ref,attr in topics.items():
g.add_node(ref)
tnode = g.node[ref] # current node (topic)
if isinstance(attr, dict):
tnode['type'] = attr.get('type', 'topic')
tnode['name'] = attr.get('name', ref)
tnode['questions'] = attr.get('questions', [])
g.add_edges_from((d,ref) for d in attr.get('deps', []))
fullpath = path.expanduser(path.join(prefix, ref))
filename = path.join(fullpath, 'questions.yaml')
loaded_questions = load_yaml(filename, default=[]) # list
# if questions not in configuration then load all, preserving order
if not tnode['questions']:
tnode['questions'] = [q.setdefault('ref', f'{ref}:{i}') for i,q in enumerate(loaded_questions)]
# make questions factory (without repeating same question)
tnode['factory'] = {}
for q in loaded_questions:
if q['ref'] in tnode['questions']:
q['path'] = fullpath # fullpath added to each question
tnode['factory'][q['ref']] = QFactory(q)
logger.info(f'{len(tnode["questions"]):6} {ref}')
qcount += len(tnode["questions"]) # count total questions
tcount += 1
logger.info(f'Total loaded: {tcount} topics, {qcount} questions')
return g