From c8eb37b15233bec1d33b927880cdbdae3f5001ec Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 6 Feb 2013 12:47:06 -0500 Subject: [PATCH] question: Add support for list-of-lines prompts We use lists of lines for other question attributes (e.g. setup, teardown, answer). Be consistent with multi-line prompts. The handling is in Question.format_prompt() for easy access by any consumer. The `newline` option will make it easy to support non-terminal output formats (e.g. `newline='
'` for HTML). --- quizzer/cli.py | 2 +- quizzer/question.py | 5 +++++ quizzer/ui/cli.py | 7 ++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/quizzer/cli.py b/quizzer/cli.py index 60cd645..89d972f 100644 --- a/quizzer/cli.py +++ b/quizzer/cli.py @@ -77,7 +77,7 @@ def main(): if args.questions: for i,q in enumerate(stack): print('Question {}:'.format(i)) - print(q.prompt) + print(q.format_prompt()) print() return ui = _cli.CommandLineInterface(quiz=quiz, answers=answers, stack=stack) diff --git a/quizzer/question.py b/quizzer/question.py index 13dc335..8b2f6dc 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -74,6 +74,11 @@ class Question (object): def check(self, answer): return answer == self.answer + def format_prompt(self, newline='\n'): + if isinstance(self.prompt, str): + return self.prompt + return newline.join(self.prompt) + class NormalizedStringQuestion (Question): def normalize(self, string): diff --git a/quizzer/ui/cli.py b/quizzer/ui/cli.py index 009fae5..a9bb100 100644 --- a/quizzer/ui/cli.py +++ b/quizzer/ui/cli.py @@ -28,7 +28,7 @@ class CommandLineInterface (UserInterface): question = self.get_question() if not question: break - print(question.prompt) + print(question.format_prompt()) if question.multiline: answers = [] while True: @@ -42,7 +42,7 @@ class CommandLineInterface (UserInterface): return if a in ['?', 'help']: print() - print(question.prompt) + print(question.format_prompt()) print(question.help) continue if question.multiline: @@ -69,7 +69,8 @@ class CommandLineInterface (UserInterface): def display_result(self, question): answers = self.answers.get(question.id, []) - print('question: {}'.format(question.prompt)) + print('question:') + print(' {}'.format(question.format_prompt(newline='\n '))) la = len(answers) lc = len([a for a in answers if a['correct']]) print('answers: {}/{} ({:.2f})'.format(lc, la, float(lc)/la)) -- 2.26.2