Added saving, loading, and a test quiz
authorW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 13:49:54 +0000 (08:49 -0500)
committerW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 13:51:05 +0000 (08:51 -0500)
quiz.json [new file with mode: 0644]
quizzer/cli.py
quizzer/question.py
quizzer/quiz.py

diff --git a/quiz.json b/quiz.json
new file mode 100644 (file)
index 0000000..ea89d1d
--- /dev/null
+++ b/quiz.json
@@ -0,0 +1,25 @@
+{
+       "version": "0.1",
+       "questions": [
+               {
+                       "prompt": "What is your name?",
+                       "answer": "",
+                       "hint": "This should be easy"
+               },
+               {
+                       "prompt": "What is your quest?",
+                       "answer": "To seek the Holy Grail",
+                       "hint": "Think like a Pythonista"
+               },
+               {
+                       "prompt": "What is your favourite color?",
+                       "answer": "Blue",
+                       "hint": "Channel Sir Lancelot"
+               },
+               {
+                       "prompt": "What is the capital of Assyria?",
+                       "answer": "I don't know that",
+                       "hint": "Sir Robin didn't know it either"
+               }
+       ]
+}
index 7a63e87a693f445bae041d9def30f873049244f9..8648e25b2c1a9c354944558c7ed2a978e82e80bc 100644 (file)
@@ -1,4 +1,5 @@
 import argparse as _argparse
+import locale as _locale
 
 from . import __doc__ as _module_doc
 from . import __version__
@@ -6,6 +7,8 @@ from . import quiz as _quiz
 
 
 def main():
+    encoding = _locale.getpreferredencoding(do_setlocale=True)
+
     parser = _argparse.ArgumentParser(description=_module_doc)
     parser.add_argument(
         '--version', action='version',
@@ -16,7 +19,7 @@ def main():
 
     args = parser.parse_args()
 
-    quiz = Quiz()
-    quiz.load(args.quiz)
+    quiz = _quiz.Quiz(path=args.quiz, encoding=encoding)
+    quiz.load()
     for question in quiz:
         print(question)
index b9435c8b1be8740744c0da1e711eefed21bc3b2c..0421fb4570af8f9ba693bc332010e9417814b34a 100644 (file)
@@ -4,6 +4,15 @@ class Question (object):
         self.answer = answer
         self.help = help
 
+    def __getstate__(self):
+        return {
+            'prompt': self.prompt,
+            'answer': self.answer,
+            'help': self.help,
+            }
+
+    def __setstate__(self, state):
+        self.__dict__.update(state)
+
     def check(self, answer):
         return answer == self.answer
-
index 651a51488d6b1bdbaf9471133122a41d102dc28d..f0250c7ab83808d3bbd0af6b803abee619b62260 100644 (file)
@@ -1,11 +1,42 @@
+import codecs as _codecs
+import json as _json
+
+from . import __version__
+from . import question as _question
+
+
 class Quiz (list):
-    def __init__(self, questions=None):
+    def __init__(self, questions=None, path=None, encoding=None):
         if questions is None:
             questions = []
         super(Quiz, self).__init__(questions)
+        self.path = path
+        self.encoding = encoding
+
+    def _open(self, mode='r', path=None, encoding=None):
+        if path:
+            self.path = path
+        if encoding:
+            self.encoding = encoding
+        return _codecs.open(self.path, mode, self.encoding)
 
-    def load(self):
-        pass
+    def load(self, **kwargs):
+        with self._open(mode='r', **kwargs) as f:
+            data = _json.load(f)
+        version = data.get('version', None)
+        if version != __version__:
+            raise NotImplementedError('upgrade from {} to {}'.format(
+                    version, __version__))
+        for state in data['questions']:
+            q = _question.Question()
+            q.__setstate__(state)
+            self.append(q)
 
-    def save(self):
-        pass
+    def save(self, **kwargs):
+        data = {
+            'version': __version__,
+            'questions': [question.__getstate__() for question in self],
+            }
+        with self._open(mode='w', **kwargs) as f:
+            _json.dump(
+                data, f, indent=2, separators=(',', ': '), sort_keys=True)