Commit 94b0dd76345d616bd319a464778ad836923185ec

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

fixes error in checkbox questions when shuffle=false.

aprendizations/learnapp.py
... ... @@ -44,8 +44,8 @@ class LearnApp(object):
44 44 yield session
45 45 session.commit()
46 46 except Exception:
47   - session.rollback()
48 47 logger.error('DB rollback!!!')
  48 + session.rollback()
49 49 finally:
50 50 session.close()
51 51  
... ...
aprendizations/questions.py
... ... @@ -50,8 +50,6 @@ class Question(dict):
50 50  
51 51 async def correct_async(self) -> None:
52 52 self.correct()
53   - # loop = asyncio.get_running_loop()
54   - # await loop.run_in_executor(None, self.correct)
55 53  
56 54 def set_defaults(self, d: QDict) -> None:
57 55 'Add k:v pairs from default dict d for nonexistent keys'
... ... @@ -94,7 +92,9 @@ class QuestionRadio(Question):
94 92 for x in range(n)]
95 93  
96 94 if len(self['correct']) != n:
97   - msg = f'Options and correct mismatch in "{self["ref"]}"'
  95 + msg = (f'Options and correct mismatch in '
  96 + f'"{self["ref"]}", file "{self["filename"]}".')
  97 + logger.error(msg)
98 98 raise QuestionException(msg)
99 99  
100 100 if self['shuffle']:
... ... @@ -168,18 +168,20 @@ class QuestionCheckbox(Question):
168 168 }))
169 169  
170 170 if len(self['correct']) != n:
171   - msg = f'Options and correct mismatch in "{self["ref"]}"'
  171 + msg = (f'Options and correct size mismatch in '
  172 + f'"{self["ref"]}", file "{self["filename"]}".')
  173 + logger.error(msg)
172 174 raise QuestionException(msg)
173 175  
174 176 # if an option is a list of (right, wrong), pick one
175   - # FIXME it's possible that all options are chosen wrong
176 177 options = []
177 178 correct = []
178 179 for o, c in zip(self['options'], self['correct']):
179 180 if isinstance(o, list):
180 181 r = random.randint(0, 1)
181 182 o = o[r]
182   - c = c if r == 0 else -c
  183 + if r == 1:
  184 + c = -c
183 185 options.append(str(o))
184 186 correct.append(float(c))
185 187  
... ... @@ -187,8 +189,11 @@ class QuestionCheckbox(Question):
187 189 # and apply to `options` and `correct`
188 190 if self['shuffle']:
189 191 perm = random.sample(range(n), k=self['choose'])
190   - self['options'] = [str(options[i]) for i in perm]
191   - self['correct'] = [float(correct[i]) for i in perm]
  192 + self['options'] = [options[i] for i in perm]
  193 + self['correct'] = [correct[i] for i in perm]
  194 + else:
  195 + self['options'] = options[:self['choose']]
  196 + self['correct'] = correct[:self['choose']]
192 197  
193 198 # ------------------------------------------------------------------------
194 199 # can return negative values for wrong answers
... ... @@ -443,20 +448,20 @@ class QFactory(object):
443 448 'textarea': QuestionTextArea,
444 449 # -- informative panels --
445 450 'information': QuestionInformation,
  451 + 'success': QuestionInformation,
446 452 'warning': QuestionInformation,
447 453 'alert': QuestionInformation,
448   - 'success': QuestionInformation,
449 454 }
450 455  
451   - def __init__(self, question_dict: QDict = QDict({})) -> None:
452   - self.question = question_dict
  456 + def __init__(self, qdict: QDict = QDict({})) -> None:
  457 + self.question = qdict
453 458  
454 459 # -----------------------------------------------------------------------
455 460 # Given a ref returns an instance of a descendent of Question(),
456 461 # i.e. a question object (radio, checkbox, ...).
457 462 # -----------------------------------------------------------------------
458 463 def generate(self) -> Question:
459   - logger.debug(f'[generate] "{self.question["ref"]}"...')
  464 + logger.debug(f'[QFactory.generate] "{self.question["ref"]}"...')
460 465 # Shallow copy so that script generated questions will not replace
461 466 # the original generators
462 467 q = self.question.copy()
... ... @@ -487,7 +492,7 @@ class QFactory(object):
487 492  
488 493 # -----------------------------------------------------------------------
489 494 async def generate_async(self) -> Question:
490   - logger.debug(f'[generate_async] "{self.question["ref"]}"...')
  495 + logger.debug(f'[QFactory.generate_async] "{self.question["ref"]}"...')
491 496 # Shallow copy so that script generated questions will not replace
492 497 # the original generators
493 498 q = self.question.copy()
... ...
aprendizations/tools.py
... ... @@ -145,7 +145,7 @@ def load_yaml(filename: str, default: Any = None) -> Any:
145 145 logger.error(f'Cannot open "{filename}": not found')
146 146 except PermissionError:
147 147 logger.error(f'Cannot open "{filename}": no permission')
148   - except IOError:
  148 + except OSError:
149 149 logger.error(f'Cannot open file "{filename}"')
150 150 else:
151 151 with f:
... ...