question: Add support for list-of-lines prompts
authorW. Trevor King <wking@tremily.us>
Wed, 6 Feb 2013 17:47:06 +0000 (12:47 -0500)
committerW. Trevor King <wking@tremily.us>
Wed, 6 Feb 2013 17:59:06 +0000 (12:59 -0500)
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='<br/>'` for HTML).

quizzer/cli.py
quizzer/question.py
quizzer/ui/cli.py

index 60cd645ebebfe7cff03283a3a8c043682d72ac43..89d972f17a82934887bb081104d780b1156fa967 100644 (file)
@@ -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)
index 13dc335ab02c54a14fbef5d49f5821c32f9f3f22..8b2f6dc15dc6ed42e98f0c60f30f20b6bf72df3c 100644 (file)
@@ -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):
index 009fae56cb839d8eb2620b18cb1523a26fe4dd4d..a9bb100e3302ee01940b4da425fd5bc1fb871fcc 100644 (file)
@@ -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))