From d5ac4a8b612e977d9b6102f0e73de16a1fc1cb96 Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Thu, 26 Jan 2017 18:02:42 +0000 Subject: [PATCH] - Added new question type text_numeric. Expects a numerical answer and checks if it's in a given interval. --- demo/questions/questions.yaml | 6 ++++++ demo/test.yaml | 42 ++++++++++++++++++++++-------------------- questions.py | 37 +++++++++++++++++++++++++++++++++++++ templates/review.html | 2 +- templates/test.html | 2 +- 5 files changed, 67 insertions(+), 22 deletions(-) diff --git a/demo/questions/questions.yaml b/demo/questions/questions.yaml index d0b0cec..aa86e9a 100644 --- a/demo/questions/questions.yaml +++ b/demo/questions/questions.yaml @@ -67,6 +67,12 @@ type: text_regex text: O nosso planeta chama-se planeta... correct: !regex '[Tt]erra' + # --------------------------------------------------------------------------- +- + ref: fractions + type: text_numeric + text: Quanto é 1/4? + correct: [0.249, 0.251] # --------------------------------------------------------------------------- - ref: basic-colors diff --git a/demo/test.yaml b/demo/test.yaml index f5097e7..781cb8e 100644 --- a/demo/test.yaml +++ b/demo/test.yaml @@ -40,30 +40,32 @@ files: # The order is preserved. # There are several ways to define each question (explained below). questions: - # show question where ref=instructions - - ref: instructions + # # show question where ref=instructions + # - ref: instructions - # show question where ref=flags and assigns 0.5 points (unnormalized) - - ref: flags - points: 0.5 + # # show question where ref=flags and assigns 0.5 points (unnormalized) + # - ref: flags + # points: 0.5 - # idem - - ref: math-expressions - points: 2.0 + # # idem + # - ref: math-expressions + # points: 2.0 - # show question where ref=solar-system and assign the default of 1.0 point (unnormalized) - - ref: solar-system + # # show question where ref=solar-system and assign the default of 1.0 point (unnormalized) + # - ref: solar-system - # select one questions from the list [our_planet1, our_planet2] - # and assign 0.75 points (unnormalized) - - ref: - - our_planet1 - - our_planet2 - points: 0.75 + # # select one questions from the list [our_planet1, our_planet2] + # # and assign 0.75 points (unnormalized) + # - ref: + # - our_planet1 + # - our_planet2 + # points: 0.75 - # the key 'ref:' can be omitted, a default of 1.0 points is assigned - - basic-colors + # # the key 'ref:' can be omitted, a default of 1.0 points is assigned + # - basic-colors - - question-whatever + - fractions - - markdown_instructions + # - question-whatever + + # - markdown_instructions diff --git a/questions.py b/questions.py index ffa2fb8..1d1e4c1 100644 --- a/questions.py +++ b/questions.py @@ -122,6 +122,7 @@ class QuestionFactory(dict): 'checkbox' : QuestionCheckbox, 'text' : QuestionText, 'text_regex': QuestionTextRegex, + 'text_numeric': QuestionTextNumeric, 'textarea' : QuestionTextArea, # informative panels 'information': QuestionInformation, @@ -383,6 +384,42 @@ class QuestionTextRegex(Question): # =========================================================================== +class QuestionTextNumeric(Question): + '''An instance of QuestionTextNumeric will always have the keys: + type (str) + text (str) + correct (list [lower bound, upper bound]) + answer (None or an actual answer) + An answer is correct if it's in the closed interval. + ''' + + #------------------------------------------------------------------------ + def __init__(self, q): + super().__init__(q) + + self.set_defaults({ + 'text': '', + 'correct': [1.0, -1.0], # will always return false + }) + + #------------------------------------------------------------------------ + # can return negative values for wrong answers + def correct(self): + super().correct() + if self['answer'] is not None: + lower, upper = self['correct'] + print(lower) + print(upper) + try: + self['grade'] = 1.0 if lower <= float(self['answer']) <= upper else 0.0 + except TypeError: + logger.error('While matching regex {0} with answer {1}.'.format(self['correct'], self['answer'])) + except ValueError: + self['comments'] = f'A resposta "{self["answer"]}" não é numérica.' + return self['grade'] + + +# =========================================================================== class QuestionTextArea(Question): '''An instance of QuestionTextArea will always have the keys: type (str) diff --git a/templates/review.html b/templates/review.html index 949898b..56706ee 100644 --- a/templates/review.html +++ b/templates/review.html @@ -207,7 +207,7 @@ % endfor - % elif q['type'] in ('text', 'text_regex', 'textarea'): + % elif q['type'] in ('text', 'text_regex', 'text_numeric', 'textarea'):
${q['answer'] if q['answer'] is not None else ''}
% endif diff --git a/templates/test.html b/templates/test.html index a0b40ee..f0c1fcd 100644 --- a/templates/test.html +++ b/templates/test.html @@ -157,7 +157,7 @@ % endfor - % elif q['type'] in ('text', 'text_regex'): + % elif q['type'] in ('text', 'text_regex', 'text_numeric'): % elif q['type'] == 'textarea':
-- libgit2 0.21.2