From 4ea24b4d91c104079830870327dd475ccde807eb Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Feb 2013 08:49:54 -0500 Subject: [PATCH] Added saving, loading, and a test quiz --- quiz.json | 25 +++++++++++++++++++++++++ quizzer/cli.py | 7 +++++-- quizzer/question.py | 11 ++++++++++- quizzer/quiz.py | 41 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 quiz.json diff --git a/quiz.json b/quiz.json new file mode 100644 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" + } + ] +} diff --git a/quizzer/cli.py b/quizzer/cli.py index 7a63e87..8648e25 100644 --- a/quizzer/cli.py +++ b/quizzer/cli.py @@ -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) diff --git a/quizzer/question.py b/quizzer/question.py index b9435c8..0421fb4 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -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 - diff --git a/quizzer/quiz.py b/quizzer/quiz.py index 651a514..f0250c7 100644 --- a/quizzer/quiz.py +++ b/quizzer/quiz.py @@ -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) -- 2.26.2