diff --git a/aprendizations/learnapp.py b/aprendizations/learnapp.py index 8162b3a..d3abc98 100644 --- a/aprendizations/learnapp.py +++ b/aprendizations/learnapp.py @@ -44,8 +44,8 @@ class LearnApp(object): yield session session.commit() except Exception: - session.rollback() logger.error('DB rollback!!!') + session.rollback() finally: session.close() diff --git a/aprendizations/questions.py b/aprendizations/questions.py index 65c7635..8ac5bec 100644 --- a/aprendizations/questions.py +++ b/aprendizations/questions.py @@ -50,8 +50,6 @@ class Question(dict): async def correct_async(self) -> None: self.correct() - # loop = asyncio.get_running_loop() - # await loop.run_in_executor(None, self.correct) def set_defaults(self, d: QDict) -> None: 'Add k:v pairs from default dict d for nonexistent keys' @@ -94,7 +92,9 @@ class QuestionRadio(Question): for x in range(n)] if len(self['correct']) != n: - msg = f'Options and correct mismatch in "{self["ref"]}"' + msg = (f'Options and correct mismatch in ' + f'"{self["ref"]}", file "{self["filename"]}".') + logger.error(msg) raise QuestionException(msg) if self['shuffle']: @@ -168,18 +168,20 @@ class QuestionCheckbox(Question): })) if len(self['correct']) != n: - msg = f'Options and correct mismatch in "{self["ref"]}"' + msg = (f'Options and correct size mismatch in ' + f'"{self["ref"]}", file "{self["filename"]}".') + logger.error(msg) raise QuestionException(msg) # if an option is a list of (right, wrong), pick one - # FIXME it's possible that all options are chosen wrong options = [] correct = [] for o, c in zip(self['options'], self['correct']): if isinstance(o, list): r = random.randint(0, 1) o = o[r] - c = c if r == 0 else -c + if r == 1: + c = -c options.append(str(o)) correct.append(float(c)) @@ -187,8 +189,11 @@ class QuestionCheckbox(Question): # and apply to `options` and `correct` if self['shuffle']: perm = random.sample(range(n), k=self['choose']) - self['options'] = [str(options[i]) for i in perm] - self['correct'] = [float(correct[i]) for i in perm] + self['options'] = [options[i] for i in perm] + self['correct'] = [correct[i] for i in perm] + else: + self['options'] = options[:self['choose']] + self['correct'] = correct[:self['choose']] # ------------------------------------------------------------------------ # can return negative values for wrong answers @@ -443,20 +448,20 @@ class QFactory(object): 'textarea': QuestionTextArea, # -- informative panels -- 'information': QuestionInformation, + 'success': QuestionInformation, 'warning': QuestionInformation, 'alert': QuestionInformation, - 'success': QuestionInformation, } - def __init__(self, question_dict: QDict = QDict({})) -> None: - self.question = question_dict + def __init__(self, qdict: QDict = QDict({})) -> None: + self.question = qdict # ----------------------------------------------------------------------- # Given a ref returns an instance of a descendent of Question(), # i.e. a question object (radio, checkbox, ...). # ----------------------------------------------------------------------- def generate(self) -> Question: - logger.debug(f'[generate] "{self.question["ref"]}"...') + logger.debug(f'[QFactory.generate] "{self.question["ref"]}"...') # Shallow copy so that script generated questions will not replace # the original generators q = self.question.copy() @@ -487,7 +492,7 @@ class QFactory(object): # ----------------------------------------------------------------------- async def generate_async(self) -> Question: - logger.debug(f'[generate_async] "{self.question["ref"]}"...') + logger.debug(f'[QFactory.generate_async] "{self.question["ref"]}"...') # Shallow copy so that script generated questions will not replace # the original generators q = self.question.copy() diff --git a/aprendizations/tools.py b/aprendizations/tools.py index a4f7e5c..be62ab3 100644 --- a/aprendizations/tools.py +++ b/aprendizations/tools.py @@ -145,7 +145,7 @@ def load_yaml(filename: str, default: Any = None) -> Any: logger.error(f'Cannot open "{filename}": not found') except PermissionError: logger.error(f'Cannot open "{filename}": no permission') - except IOError: + except OSError: logger.error(f'Cannot open file "{filename}"') else: with f: -- libgit2 0.21.2