From 0c1957159f11fc5770a89c7d372f24f691a4399a Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 14 Mar 2013 07:32:02 -0400 Subject: [PATCH] quizzer: Add ChoiceQuestion.display_choices This gives us more traditional multiple-choice questions. --- quizzer/question.py | 9 +++++++++ quizzer/ui/cli.py | 30 ++++++++++++++++++++++++++++-- quizzer/ui/wsgi.py | 9 ++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/quizzer/question.py b/quizzer/question.py index 3e69abb..a4da655 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -106,6 +106,15 @@ class NormalizedStringQuestion (Question): class ChoiceQuestion (Question): + _state_attributes = Question._state_attributes + [ + 'display_choices', + ] + + def __setstate__(self, state): + if 'display_choices' not in state: + state['display_choices'] = False + super(ChoiceQuestion, self).__setstate__(state) + def check(self, answer): correct = answer in self.answer details = None diff --git a/quizzer/ui/cli.py b/quizzer/ui/cli.py index 579d789..7b857b0 100644 --- a/quizzer/ui/cli.py +++ b/quizzer/ui/cli.py @@ -28,6 +28,7 @@ except ImportError as e: print(e) from .. import error as _error +from .. import question as _question from . import UserInterface as _UserInterface @@ -67,10 +68,16 @@ class QuestionCommandLine (_cmd.Cmd): def _set_ps1(self): "Pose a question and prompt" if self.question: - self.prompt = '\n{}\n{}'.format( + lines = [ + '', _colorize( self.ui.colors['question'], self.question.format_prompt()), - _colorize(self.ui.colors['prompt'], self._prompt)) + ] + lines.extend( + _colorize(self.ui.colors['prompt'], line) + for line in self._extra_ps1_lines()) + lines.append(_colorize(self.ui.colors['prompt'], self._prompt)) + self.prompt = '\n'.join(lines) else: self.prompt = _colorize(self.ui.colors['prompt'], self._prompt) @@ -78,6 +85,24 @@ class QuestionCommandLine (_cmd.Cmd): "Just prompt (without the question, e.g. for multi-line answers)" self.prompt = _colorize(self.ui.colors['prompt'], self._prompt) + def _extra_ps1_lines(self): + if (isinstance(self.question, _question.ChoiceQuestion) and + self.question.display_choices): + for i,choice in enumerate(self.question.answer): + yield '{}) {}'.format(i, choice) + return [] + + def _process_answer(self, answer): + "Back out any mappings suggested by _extra_ps1_lines()" + if (isinstance(self.question, _question.ChoiceQuestion) and + self.question.display_choices): + try: + a = int(answer) + return self.question.answer[a] + except (ValueError, IndexError): + pass + return answer + def default(self, line): self.answers.append(line) if self.question.multiline: @@ -98,6 +123,7 @@ class QuestionCommandLine (_cmd.Cmd): kwargs = {} if self._tempdir: kwargs['tempdir'] = self._tempdir + answer = self._process_answer(answer=answer) correct,details = self.ui.process_answer( question=self.question, answer=answer, **kwargs) if correct: diff --git a/quizzer/ui/wsgi.py b/quizzer/ui/wsgi.py index ca63598..c29323c 100644 --- a/quizzer/ui/wsgi.py +++ b/quizzer/ui/wsgi.py @@ -7,6 +7,7 @@ import re as _re import urllib.parse as _urllib_parse import wsgiref.simple_server as _wsgiref_simple_server +from .. import question as _question from . import UserInterface as _UserInterface @@ -241,7 +242,13 @@ class QuestionApp (WSGI_DataObject): if question is None: raise HandlerError( 307, 'Temporary Redirect', headers=[('Location', '/results/')]) - if question.multiline: + if (isinstance(question, _question.ChoiceQuestion) and + question.display_choices): + answer_element = '\n'.join( + ('{0}
' + ).format(answer) + for answer in question.answer) + elif question.multiline: answer_element = ( '') else: -- 2.26.2