Commit b141e132f9a54301ddde76daa748f202318a8898
1 parent
e02d725b
Exists in
master
and in
1 other branch
adds some type annotations
Showing
3 changed files
with
27 additions
and
19 deletions
Show diff stats
aprendizations/learnapp.py
| @@ -31,11 +31,11 @@ async def _bcrypt_hash(a, b): | @@ -31,11 +31,11 @@ async def _bcrypt_hash(a, b): | ||
| 31 | a.encode('utf-8'), b) | 31 | a.encode('utf-8'), b) |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | -async def check_password(try_pw, pw): | 34 | +async def check_password(try_pw: str, pw: str) -> bool: |
| 35 | return pw == await _bcrypt_hash(try_pw, pw) | 35 | return pw == await _bcrypt_hash(try_pw, pw) |
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | -async def bcrypt_hash_gen(new_pw): | 38 | +async def bcrypt_hash_gen(new_pw: str): |
| 39 | return await _bcrypt_hash(new_pw, bcrypt.gensalt()) | 39 | return await _bcrypt_hash(new_pw, bcrypt.gensalt()) |
| 40 | 40 | ||
| 41 | 41 | ||
| @@ -75,7 +75,10 @@ class LearnApp(object): | @@ -75,7 +75,10 @@ class LearnApp(object): | ||
| 75 | for c in config_files: | 75 | for c in config_files: |
| 76 | self.populate_graph(c) | 76 | self.populate_graph(c) |
| 77 | 77 | ||
| 78 | - self.factory = self.make_factory() # for all questions all topics | 78 | + # factory is a dict with question generators for all topics |
| 79 | + self.factory = self.make_factory() # {'qref': QFactory()} | ||
| 80 | + | ||
| 81 | + # if graph has topics that are not in the database, add them | ||
| 79 | self.db_add_missing_topics(self.deps.nodes()) | 82 | self.db_add_missing_topics(self.deps.nodes()) |
| 80 | 83 | ||
| 81 | if check: | 84 | if check: |
| @@ -83,14 +86,19 @@ class LearnApp(object): | @@ -83,14 +86,19 @@ class LearnApp(object): | ||
| 83 | 86 | ||
| 84 | # ------------------------------------------------------------------------ | 87 | # ------------------------------------------------------------------------ |
| 85 | def sanity_check_questions(self): | 88 | def sanity_check_questions(self): |
| 86 | - for qref, q in self.factory.items(): | ||
| 87 | - logger.info(f'Generating {qref}...') | 89 | + def check_question(qref, q): |
| 90 | + logger.debug(f'Generating {qref}...') | ||
| 88 | try: | 91 | try: |
| 89 | q.generate() | 92 | q.generate() |
| 90 | except Exception as e: | 93 | except Exception as e: |
| 91 | logger.error(f'Sanity check failed in "{qref}"') | 94 | logger.error(f'Sanity check failed in "{qref}"') |
| 92 | raise e | 95 | raise e |
| 93 | 96 | ||
| 97 | + logger.info('Starting sanity checks...') | ||
| 98 | + for qref, q in self.factory.items(): | ||
| 99 | + check_question(qref, q) | ||
| 100 | + logger.info('Sanity checks passed.') | ||
| 101 | + | ||
| 94 | # ------------------------------------------------------------------------ | 102 | # ------------------------------------------------------------------------ |
| 95 | # login | 103 | # login |
| 96 | # ------------------------------------------------------------------------ | 104 | # ------------------------------------------------------------------------ |
| @@ -226,11 +234,13 @@ class LearnApp(object): | @@ -226,11 +234,13 @@ class LearnApp(object): | ||
| 226 | # ------------------------------------------------------------------------ | 234 | # ------------------------------------------------------------------------ |
| 227 | def db_add_missing_topics(self, topics): | 235 | def db_add_missing_topics(self, topics): |
| 228 | with self.db_session() as s: | 236 | with self.db_session() as s: |
| 229 | - dbtopics = [t[0] for t in s.query(Topic.id)] # get topics from DB | ||
| 230 | - missing_topics = [Topic(id=t) for t in topics if t not in dbtopics] | ||
| 231 | - if missing_topics: | ||
| 232 | - s.add_all(missing_topics) | ||
| 233 | - logger.info(f'Added {len(missing_topics)} new topics') | 237 | + new_topics = [Topic(id=t) for t in topics |
| 238 | + if (t,) not in s.query(Topic.id)] | ||
| 239 | + | ||
| 240 | + if new_topics: | ||
| 241 | + s.add_all(new_topics) | ||
| 242 | + logger.info(f'Added {len(new_topics)} new topics to the ' | ||
| 243 | + f'database') | ||
| 234 | 244 | ||
| 235 | # ------------------------------------------------------------------------ | 245 | # ------------------------------------------------------------------------ |
| 236 | # setup and check database | 246 | # setup and check database |
aprendizations/questions.py
| @@ -121,7 +121,7 @@ class QuestionRadio(Question): | @@ -121,7 +121,7 @@ class QuestionRadio(Question): | ||
| 121 | 121 | ||
| 122 | # ------------------------------------------------------------------------ | 122 | # ------------------------------------------------------------------------ |
| 123 | # can return negative values for wrong answers | 123 | # can return negative values for wrong answers |
| 124 | - def correct(self): | 124 | + def correct(self) -> None: |
| 125 | super().correct() | 125 | super().correct() |
| 126 | 126 | ||
| 127 | if self['answer'] is not None: | 127 | if self['answer'] is not None: |
| @@ -187,7 +187,7 @@ class QuestionCheckbox(Question): | @@ -187,7 +187,7 @@ class QuestionCheckbox(Question): | ||
| 187 | 187 | ||
| 188 | # ------------------------------------------------------------------------ | 188 | # ------------------------------------------------------------------------ |
| 189 | # can return negative values for wrong answers | 189 | # can return negative values for wrong answers |
| 190 | - def correct(self): | 190 | + def correct(self) -> None: |
| 191 | super().correct() | 191 | super().correct() |
| 192 | 192 | ||
| 193 | if self['answer'] is not None: | 193 | if self['answer'] is not None: |
| @@ -234,7 +234,7 @@ class QuestionText(Question): | @@ -234,7 +234,7 @@ class QuestionText(Question): | ||
| 234 | self['correct'] = [str(a) for a in self['correct']] | 234 | self['correct'] = [str(a) for a in self['correct']] |
| 235 | 235 | ||
| 236 | # ------------------------------------------------------------------------ | 236 | # ------------------------------------------------------------------------ |
| 237 | - def correct(self): | 237 | + def correct(self) -> None: |
| 238 | super().correct() | 238 | super().correct() |
| 239 | 239 | ||
| 240 | if self['answer'] is not None: | 240 | if self['answer'] is not None: |
| @@ -260,7 +260,7 @@ class QuestionTextRegex(Question): | @@ -260,7 +260,7 @@ class QuestionTextRegex(Question): | ||
| 260 | }) | 260 | }) |
| 261 | 261 | ||
| 262 | # ------------------------------------------------------------------------ | 262 | # ------------------------------------------------------------------------ |
| 263 | - def correct(self): | 263 | + def correct(self) -> None: |
| 264 | super().correct() | 264 | super().correct() |
| 265 | if self['answer'] is not None: | 265 | if self['answer'] is not None: |
| 266 | try: | 266 | try: |
| @@ -291,7 +291,7 @@ class QuestionNumericInterval(Question): | @@ -291,7 +291,7 @@ class QuestionNumericInterval(Question): | ||
| 291 | }) | 291 | }) |
| 292 | 292 | ||
| 293 | # ------------------------------------------------------------------------ | 293 | # ------------------------------------------------------------------------ |
| 294 | - def correct(self): | 294 | + def correct(self) -> None: |
| 295 | super().correct() | 295 | super().correct() |
| 296 | if self['answer'] is not None: | 296 | if self['answer'] is not None: |
| 297 | lower, upper = self['correct'] | 297 | lower, upper = self['correct'] |
| @@ -331,7 +331,7 @@ class QuestionTextArea(Question): | @@ -331,7 +331,7 @@ class QuestionTextArea(Question): | ||
| 331 | self['correct'] = path.join(self['path'], self['correct']) # FIXME | 331 | self['correct'] = path.join(self['path'], self['correct']) # FIXME |
| 332 | 332 | ||
| 333 | # ------------------------------------------------------------------------ | 333 | # ------------------------------------------------------------------------ |
| 334 | - def correct(self): | 334 | + def correct(self) -> None: |
| 335 | super().correct() | 335 | super().correct() |
| 336 | 336 | ||
| 337 | if self['answer'] is not None: # correct answer and parse yaml ouput | 337 | if self['answer'] is not None: # correct answer and parse yaml ouput |
| @@ -368,7 +368,7 @@ class QuestionInformation(Question): | @@ -368,7 +368,7 @@ class QuestionInformation(Question): | ||
| 368 | 368 | ||
| 369 | # ------------------------------------------------------------------------ | 369 | # ------------------------------------------------------------------------ |
| 370 | # can return negative values for wrong answers | 370 | # can return negative values for wrong answers |
| 371 | - def correct(self): | 371 | + def correct(self) -> None: |
| 372 | super().correct() | 372 | super().correct() |
| 373 | self['grade'] = 1.0 # always "correct" but points should be zero! | 373 | self['grade'] = 1.0 # always "correct" but points should be zero! |
| 374 | 374 |
aprendizations/serve.py
| @@ -96,12 +96,10 @@ class RankingsHandler(BaseHandler): | @@ -96,12 +96,10 @@ class RankingsHandler(BaseHandler): | ||
| 96 | def get(self): | 96 | def get(self): |
| 97 | uid = self.current_user | 97 | uid = self.current_user |
| 98 | rankings = self.learn.get_rankings(uid) | 98 | rankings = self.learn.get_rankings(uid) |
| 99 | - # performance = self.learn.get_performance() | ||
| 100 | self.render('rankings.html', | 99 | self.render('rankings.html', |
| 101 | uid=uid, | 100 | uid=uid, |
| 102 | name=self.learn.get_student_name(uid), | 101 | name=self.learn.get_student_name(uid), |
| 103 | rankings=rankings) | 102 | rankings=rankings) |
| 104 | - # performance=performance) | ||
| 105 | 103 | ||
| 106 | 104 | ||
| 107 | # ---------------------------------------------------------------------------- | 105 | # ---------------------------------------------------------------------------- |