Commit b141e132f9a54301ddde76daa748f202318a8898

Authored by Miguel Barão
1 parent e02d725b
Exists in master and in 1 other branch dev

adds some type annotations

aprendizations/learnapp.py
... ... @@ -31,11 +31,11 @@ async def _bcrypt_hash(a, b):
31 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 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 39 return await _bcrypt_hash(new_pw, bcrypt.gensalt())
40 40  
41 41  
... ... @@ -75,7 +75,10 @@ class LearnApp(object):
75 75 for c in config_files:
76 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 82 self.db_add_missing_topics(self.deps.nodes())
80 83  
81 84 if check:
... ... @@ -83,14 +86,19 @@ class LearnApp(object):
83 86  
84 87 # ------------------------------------------------------------------------
85 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 91 try:
89 92 q.generate()
90 93 except Exception as e:
91 94 logger.error(f'Sanity check failed in "{qref}"')
92 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 103 # login
96 104 # ------------------------------------------------------------------------
... ... @@ -226,11 +234,13 @@ class LearnApp(object):
226 234 # ------------------------------------------------------------------------
227 235 def db_add_missing_topics(self, topics):
228 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 246 # setup and check database
... ...
aprendizations/questions.py
... ... @@ -121,7 +121,7 @@ class QuestionRadio(Question):
121 121  
122 122 # ------------------------------------------------------------------------
123 123 # can return negative values for wrong answers
124   - def correct(self):
  124 + def correct(self) -> None:
125 125 super().correct()
126 126  
127 127 if self['answer'] is not None:
... ... @@ -187,7 +187,7 @@ class QuestionCheckbox(Question):
187 187  
188 188 # ------------------------------------------------------------------------
189 189 # can return negative values for wrong answers
190   - def correct(self):
  190 + def correct(self) -> None:
191 191 super().correct()
192 192  
193 193 if self['answer'] is not None:
... ... @@ -234,7 +234,7 @@ class QuestionText(Question):
234 234 self['correct'] = [str(a) for a in self['correct']]
235 235  
236 236 # ------------------------------------------------------------------------
237   - def correct(self):
  237 + def correct(self) -> None:
238 238 super().correct()
239 239  
240 240 if self['answer'] is not None:
... ... @@ -260,7 +260,7 @@ class QuestionTextRegex(Question):
260 260 })
261 261  
262 262 # ------------------------------------------------------------------------
263   - def correct(self):
  263 + def correct(self) -> None:
264 264 super().correct()
265 265 if self['answer'] is not None:
266 266 try:
... ... @@ -291,7 +291,7 @@ class QuestionNumericInterval(Question):
291 291 })
292 292  
293 293 # ------------------------------------------------------------------------
294   - def correct(self):
  294 + def correct(self) -> None:
295 295 super().correct()
296 296 if self['answer'] is not None:
297 297 lower, upper = self['correct']
... ... @@ -331,7 +331,7 @@ class QuestionTextArea(Question):
331 331 self['correct'] = path.join(self['path'], self['correct']) # FIXME
332 332  
333 333 # ------------------------------------------------------------------------
334   - def correct(self):
  334 + def correct(self) -> None:
335 335 super().correct()
336 336  
337 337 if self['answer'] is not None: # correct answer and parse yaml ouput
... ... @@ -368,7 +368,7 @@ class QuestionInformation(Question):
368 368  
369 369 # ------------------------------------------------------------------------
370 370 # can return negative values for wrong answers
371   - def correct(self):
  371 + def correct(self) -> None:
372 372 super().correct()
373 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 96 def get(self):
97 97 uid = self.current_user
98 98 rankings = self.learn.get_rankings(uid)
99   - # performance = self.learn.get_performance()
100 99 self.render('rankings.html',
101 100 uid=uid,
102 101 name=self.learn.get_student_name(uid),
103 102 rankings=rankings)
104   - # performance=performance)
105 103  
106 104  
107 105 # ----------------------------------------------------------------------------
... ...