Use question.id instead of question.prompt for answer indexing
authorW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 15:08:52 +0000 (10:08 -0500)
committerW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 15:08:52 +0000 (10:08 -0500)
This decouples the keying from the prompt, which is useful if you want
to use another value for the `id` (e.g. a question number).

If `id` is not specified, it defaults to `prompt`.

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

index 7ba4ba79b6db1c7f2d44726b682f7d63ed60056d..3b8aa6ccd8c798668f6a4959045f2de94c274bc4 100644 (file)
@@ -37,23 +37,23 @@ class AnswerDatabase (dict):
             f.write('\n')
 
     def add(self, question, answer, correct):
-        if question.prompt not in self:
-            self[question.prompt] = []
-        self[question.prompt].append({
+        if question.id not in self:
+            self[question.id] = []
+        self[question.id].append({
                 'answer': answer,
                 'correct': correct,
                 })
 
     def get_answered(self, questions):
-        return [q for q in questions if q.prompt in self]
+        return [q for q in questions if q.id in self]
 
     def get_unanswered(self, questions):
-        return [q for q in questions if q.prompt not in self]
+        return [q for q in questions if q.id not in self]
 
     def get_correctly_answered(self, questions):
         return [q for q in questions
-                if True in [a['correct'] for a in self.get(q.prompt, [])]]
+                if True in [a['correct'] for a in self.get(q.id, [])]]
 
     def get_never_correctly_answered(self, questions):
         return [q for q in questions
-                if True not in [a['correct'] for a in self.get(q.prompt, [])]]
+                if True not in [a['correct'] for a in self.get(q.id, [])]]
index 0421fb4570af8f9ba693bc332010e9417814b34a..411d5e54c4396208ed1158e2bc3293c4e118153f 100644 (file)
@@ -1,17 +1,23 @@
 class Question (object):
-    def __init__(self, prompt=None, answer=None, help=None):
+    def __init__(self, id=None, prompt=None, answer=None, help=None):
+        if id is None:
+            id = prompt
+        self.id = id
         self.prompt = prompt
         self.answer = answer
         self.help = help
 
     def __getstate__(self):
         return {
+            'id': self.id,
             'prompt': self.prompt,
             'answer': self.answer,
             'help': self.help,
             }
 
     def __setstate__(self, state):
+        if 'id' not in state:
+            state['id'] = state.get('prompt', None)
         self.__dict__.update(state)
 
     def check(self, answer):
index 9b17339166fed9df843d5da511a62d17b83e21c8..52b6a8328be9b9d4a6af7337445df620108d1ff6 100644 (file)
@@ -29,7 +29,7 @@ class CommandLineInterface (UserInterface):
     def display_results(self):
         print('results:')
         for question in self.quiz:
-            for answer in self.answers.get(question.prompt, []):
+            for answer in self.answers.get(question.id, []):
                 self.display_result(question=question, answer=answer)
         self.display_totals()