Commit 1ad906edd57901ac8781e224a084e50104fdb512
1 parent
f2567db3
Exists in
dev
remove synchronous question generators
Showing
3 changed files
with
39 additions
and
35 deletions
Show diff stats
aprendizations/learnapp.py
@@ -125,7 +125,7 @@ class LearnApp(): | @@ -125,7 +125,7 @@ class LearnApp(): | ||
125 | for qref, qfactory in self.factory.items(): | 125 | for qref, qfactory in self.factory.items(): |
126 | logger.debug('checking %s...', qref) | 126 | logger.debug('checking %s...', qref) |
127 | try: | 127 | try: |
128 | - question = qfactory.generate() | 128 | + question = asyncio.run(qfactory.generate()) |
129 | except QuestionException as exc: | 129 | except QuestionException as exc: |
130 | logger.error(exc) | 130 | logger.error(exc) |
131 | errors += 1 | 131 | errors += 1 |
aprendizations/questions.py
@@ -5,6 +5,7 @@ Description: Classes the implement several types of questions. | @@ -5,6 +5,7 @@ Description: Classes the implement several types of questions. | ||
5 | 5 | ||
6 | 6 | ||
7 | # python standard library | 7 | # python standard library |
8 | +import asyncio | ||
8 | from datetime import datetime | 9 | from datetime import datetime |
9 | import logging | 10 | import logging |
10 | from os import path | 11 | from os import path |
@@ -14,7 +15,7 @@ from typing import Any, Dict, NewType | @@ -14,7 +15,7 @@ from typing import Any, Dict, NewType | ||
14 | import uuid | 15 | import uuid |
15 | 16 | ||
16 | # this project | 17 | # this project |
17 | -from aprendizations.tools import run_script, run_script_async | 18 | +from aprendizations.tools import run_script_async |
18 | 19 | ||
19 | # setup logger for this module | 20 | # setup logger for this module |
20 | logger = logging.getLogger(__name__) | 21 | logger = logging.getLogger(__name__) |
@@ -506,36 +507,39 @@ class QuestionTextArea(Question): | @@ -506,36 +507,39 @@ class QuestionTextArea(Question): | ||
506 | self['correct'] = path.join(self['path'], self['correct']) | 507 | self['correct'] = path.join(self['path'], self['correct']) |
507 | 508 | ||
508 | # ------------------------------------------------------------------------ | 509 | # ------------------------------------------------------------------------ |
509 | - def correct(self) -> None: | ||
510 | - super().correct() | ||
511 | - | ||
512 | - if self['answer'] is not None: # correct answer and parse yaml ouput | ||
513 | - out = run_script( | ||
514 | - script=self['correct'], | ||
515 | - args=self['args'], | ||
516 | - stdin=self['answer'], | ||
517 | - timeout=self['timeout'] | ||
518 | - ) | ||
519 | - | ||
520 | - if out is None: | ||
521 | - logger.warning('No grade after running "%s".', self["correct"]) | ||
522 | - self['comments'] = ('Erro interno durante a correcção. ' | ||
523 | - 'Por favor reporte este erro') | ||
524 | - self['grade'] = 0.0 | ||
525 | - elif isinstance(out, dict): | ||
526 | - self['comments'] = out.get('comments', '') | ||
527 | - try: | ||
528 | - self['grade'] = float(out['grade']) | ||
529 | - except ValueError: | ||
530 | - logger.error('Output error in "%s".', self["correct"]) | ||
531 | - except KeyError: | ||
532 | - logger.error('No grade in "%s".', self["correct"]) | ||
533 | - else: | ||
534 | - try: | ||
535 | - self['grade'] = float(out) | ||
536 | - except (TypeError, ValueError): | ||
537 | - logger.error('Invalid grade in "%s".', self["correct"]) | ||
538 | - | 510 | + # def correct(self) -> None: |
511 | + # super().correct() | ||
512 | + # | ||
513 | + # if self['answer'] is not None: # correct answer and parse yaml ouput | ||
514 | + # out = run_script( | ||
515 | + # script=self['correct'], | ||
516 | + # args=self['args'], | ||
517 | + # stdin=self['answer'], | ||
518 | + # timeout=self['timeout'] | ||
519 | + # ) | ||
520 | + # | ||
521 | + # if out is None: | ||
522 | + # logger.warning('No grade after running "%s".', self["correct"]) | ||
523 | + # self['comments'] = ('Erro interno durante a correcção. ' | ||
524 | + # 'Por favor reporte este erro') | ||
525 | + # self['grade'] = 0.0 | ||
526 | + # elif isinstance(out, dict): | ||
527 | + # self['comments'] = out.get('comments', '') | ||
528 | + # try: | ||
529 | + # self['grade'] = float(out['grade']) | ||
530 | + # except ValueError: | ||
531 | + # logger.error('Output error in "%s".', self["correct"]) | ||
532 | + # except KeyError: | ||
533 | + # logger.error('No grade in "%s".', self["correct"]) | ||
534 | + # else: | ||
535 | + # try: | ||
536 | + # self['grade'] = float(out) | ||
537 | + # except (TypeError, ValueError): | ||
538 | + # logger.error('Invalid grade in "%s".', self["correct"]) | ||
539 | + # | ||
540 | + def correct(self) -> None: # FIXME | ||
541 | + asyncio.run(self.correct_async()) | ||
542 | + | ||
539 | # ------------------------------------------------------------------------ | 543 | # ------------------------------------------------------------------------ |
540 | async def correct_async(self) -> None: | 544 | async def correct_async(self) -> None: |
541 | super().correct() | 545 | super().correct() |
@@ -663,7 +667,7 @@ class QFactory(): | @@ -663,7 +667,7 @@ class QFactory(): | ||
663 | self.qdict = qdict | 667 | self.qdict = qdict |
664 | 668 | ||
665 | # ------------------------------------------------------------------------ | 669 | # ------------------------------------------------------------------------ |
666 | - async def gen_async(self) -> Question: | 670 | + async def generate(self) -> Question: |
667 | ''' | 671 | ''' |
668 | generates a question instance of QuestionRadio, QuestionCheckbox, ..., | 672 | generates a question instance of QuestionRadio, QuestionCheckbox, ..., |
669 | which is a descendent of base class Question. | 673 | which is a descendent of base class Question. |
aprendizations/student.py
@@ -114,7 +114,7 @@ class StudentState(): | @@ -114,7 +114,7 @@ class StudentState(): | ||
114 | questions = topic['questions'][:k] | 114 | questions = topic['questions'][:k] |
115 | logger.debug('selected questions: %s', ', '.join(questions)) | 115 | logger.debug('selected questions: %s', ', '.join(questions)) |
116 | 116 | ||
117 | - self.questions: List[Question] = [await self.factory[ref].gen_async() | 117 | + self.questions: List[Question] = [await self.factory[ref].generate() |
118 | for ref in questions] | 118 | for ref in questions] |
119 | 119 | ||
120 | logger.debug('generated %s questions', len(self.questions)) | 120 | logger.debug('generated %s questions', len(self.questions)) |
@@ -171,7 +171,7 @@ class StudentState(): | @@ -171,7 +171,7 @@ class StudentState(): | ||
171 | elif question['status'] == 'wrong': | 171 | elif question['status'] == 'wrong': |
172 | if question['append_wrong']: | 172 | if question['append_wrong']: |
173 | logger.debug(' wrong answer => append new question') | 173 | logger.debug(' wrong answer => append new question') |
174 | - new_question = await self.factory[question['ref']].gen_async() | 174 | + new_question = await self.factory[question['ref']].generate() |
175 | self.questions.append(new_question) | 175 | self.questions.append(new_question) |
176 | self.next_question() | 176 | self.next_question() |
177 | 177 |