From 913dbfb228dbe9631224a56a7539c01502862c5b Mon Sep 17 00:00:00 2001 From: Miguel BarĂ£o Date: Wed, 3 Apr 2019 23:32:11 +0100 Subject: [PATCH] fix args in textarea questions. fix some flake8 errors in test.py. add debug log on individual question correction. --- perguntations/questions.py | 7 ++++--- perguntations/test.py | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/perguntations/questions.py b/perguntations/questions.py index 8068461..636ad32 100644 --- a/perguntations/questions.py +++ b/perguntations/questions.py @@ -335,9 +335,10 @@ class QuestionTextArea(Question): self.set_defaults({ 'text': '', - 'lines': 8, - 'timeout': 5, # seconds - 'correct': '' # trying to execute this will fail => grade 0.0 + 'lines': 8, # FIXME not being used??? + 'timeout': 5, # seconds + 'correct': '', # trying to execute this will fail => grade 0.0 + 'args': [] }) self['correct'] = path.join(self['path'], self['correct']) diff --git a/perguntations/test.py b/perguntations/test.py index e750f05..d57dc38 100644 --- a/perguntations/test.py +++ b/perguntations/test.py @@ -84,7 +84,7 @@ class TestFactory(dict): logger.critical(f'Found {nerrs} errors generating questions.') raise TestFactoryException() else: - logger.info(f'No errors found. Test factory ready for "{self["ref"]}".') + logger.info(f'No errors found. Factory ready for "{self["ref"]}".') # ----------------------------------------------------------------------- # Checks for valid keys and sets default values. @@ -126,14 +126,17 @@ class TestFactory(dict): f'Using {path.abspath(path.curdir)}') self['questions_dir'] = path.curdir elif not path.isdir(path.expanduser(self['questions_dir'])): - logger.critical(f'Can\'t find questions directory "{self["questions_dir"]}"') + logger.critical(f'Can\'t find questions directory ' + f'"{self["questions_dir"]}"') raise TestFactoryException() # --- files if 'files' not in self: - logger.warning('Missing "files" key. Loading all YAML files from "questions_dir"... DANGEROUS!!!') + logger.warning('Missing "files" key. Loading all YAML files from ' + '"questions_dir"... DANGEROUS!!!') try: - self['files'] = fnmatch.filter(listdir(self['questions_dir']), '*.yaml') + self['files'] = fnmatch.filter(listdir(self['questions_dir']), + '*.yaml') except OSError: logger.critical('Couldn\'t get list of YAML question files.') raise TestFactoryException() @@ -165,13 +168,13 @@ class TestFactory(dict): n = 1 loop = asyncio.get_running_loop() - + qgenerator = self.question_factory.generate for qq in self['questions']: # generate Question() selected randomly from list of references qref = random.choice(qq['ref']) try: - q = await loop.run_in_executor(None, self.question_factory.generate, qref) + q = await loop.run_in_executor(None, qgenerator, qref) except Exception: logger.error(f'Can\'t generate question "{qref}". Skipping.') continue @@ -209,8 +212,9 @@ class TestFactory(dict): }) # ----------------------------------------------------------------------- - # def __repr__(self): - # return '{\n' + '\n'.join(' {0:14s}: {1}'.format(k, v) for k,v in self.items()) + '\n}' + def __repr__(self): + testsettings = '\n'.join(f' {k:14s}: {v}' for k, v in self.items()) + return '{\n' + testsettings + '\n}' # =========================================================================== @@ -241,8 +245,8 @@ class Test(dict): # Given a dictionary ans={index: 'some answer'} updates the # answers of the test. Only affects questions referred. def update_answers(self, ans): - for i in ans: - self['questions'][i]['answer'] = ans[i] + for ref, answer in ans.items(): + self['questions'][ref]['answer'] = answer logger.info(f'Student {self["student"]["number"]}: ' f'{len(ans)} answers updated.') @@ -253,8 +257,11 @@ class Test(dict): self['state'] = 'FINISHED' grade = 0.0 for q in self['questions']: - grade += await q.correct_async() * q['points'] - self['grade'] = max(0, round(grade, 1)) # avoid negative grade + g = await q.correct_async() + grade += g * q['points'] + logger.debug(f'Correcting "{q["ref"]}": {g*100.0} %') + + self['grade'] = max(0, round(grade, 1)) # avoid negative grades logger.info(f'Student {self["student"]["number"]}: ' f'{self["grade"]} points.') return self['grade'] -- libgit2 0.21.2