diff --git a/demo/questions/questions-tutorial.yaml b/demo/questions/questions-tutorial.yaml index ea43cb2..9cee83a 100644 --- a/demo/questions/questions-tutorial.yaml +++ b/demo/questions/questions-tutorial.yaml @@ -264,7 +264,28 @@ ``` Neste caso, as respostas aceites são `azul`, `Azul` ou `AZUL`. - correct: ['azul', 'Azul', 'AZUL'] + + Em alguns casos pode ser conveniente transformar a resposta antes de a + comparar, por exemplo para remover espaços ou converter para maiúsculas ou + maiúsculas. + A opção `transform` permite dar uma sequência de transformações a aplicar à + resposta do aluno, por exemplo: + + ```yaml + transform: ['trim', 'lower'] + correct: ['azul'] + ``` + + Neste momento estão disponíveis as seguintes transformações: + + * `trim` remove os espaços do início e fim da resposta, os do meio mantêm-se + inalterados. + * `remove_space` remove todos os espaços (início, meio e fim). + * `normalize_space` remove espaços do início e fim (trim), e substitui + múltiplos espaços por um único espaço (no meio). + * `lower` e `upper` convertem respectivamente para minúsculas e maiúsculas. + transform: ['remove_spaces', 'lower'] + correct: ['azul'] # --------------------------------------------------------------------------- - type: text-regex diff --git a/perguntations/questions.py b/perguntations/questions.py index cb15512..d66c2c6 100644 --- a/perguntations/questions.py +++ b/perguntations/questions.py @@ -234,7 +234,8 @@ class QuestionText(Question): self.set_defaults(QDict({ 'text': '', - 'correct': [], + 'correct': [], # no correct answers, always wrong + 'transform': [], # transformations applied to the answer, in order })) # make sure its always a list of possible correct answers @@ -244,12 +245,36 @@ class QuestionText(Question): # make sure all elements of the list are strings self['correct'] = [str(a) for a in self['correct']] + # make sure that the answers are invariant with respect to the filters + if any(c != self.transform(c) for c in self['correct']): + logger.warning(f'in "{self["ref"]}", correct answers are not ' + 'invariant wrt transformations') + + # ------------------------------------------------------------------------ + # apply optional filters to the answer + def transform(self, ans): + for f in self['transform']: + if f == 'remove_space': + ans = ans.replace(' ', '') + elif f == 'trim': + ans = ans.strip() + elif f == 'normalize_space': + ans = re.sub(r'\s+', ' ', ans.strip()) + elif f == 'lower': + ans = ans.lower() + elif f == 'upper': + ans = ans.upper() + else: + logger.warning(f'in "{self["ref"]}", unknown transform "{f}"') + return ans + # ------------------------------------------------------------------------ def correct(self) -> None: super().correct() if self['answer'] is not None: - self['grade'] = 1.0 if self['answer'] in self['correct'] else 0.0 + answer = self.transform(self['answer']) # apply transformations + self['grade'] = 1.0 if answer in self['correct'] else 0.0 # ============================================================================ diff --git a/perguntations/static/css/test.css b/perguntations/static/css/test.css index ad56d4f..25dbd39 100644 --- a/perguntations/static/css/test.css +++ b/perguntations/static/css/test.css @@ -1,6 +1,6 @@ /* Fixes navigation panel overlaying content */ html { - font-size: 13px; + font-size: 11pt; } body { @@ -15,9 +15,12 @@ body { } .card { - margin-top: 5em; + margin-bottom: 1em; } +.alert { + margin-top: 3em; +} textarea { font-family: monospace !important; diff --git a/perguntations/templates/question.html b/perguntations/templates/question.html index c666ccf..3482416 100644 --- a/perguntations/templates/question.html +++ b/perguntations/templates/question.html @@ -1,7 +1,7 @@ {% autoescape %} {% block question %} -
+
{{ q['number'] }}. {{ q['title'] }}
diff --git a/perguntations/test.py b/perguntations/test.py index e3f407e..d85f9db 100644 --- a/perguntations/test.py +++ b/perguntations/test.py @@ -1,7 +1,6 @@ # python standard library -from os import path, listdir -import fnmatch +from os import path import random from datetime import datetime import logging -- libgit2 0.21.2