--- /dev/null
+{
+ "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"
+ }
+ ]
+}
import argparse as _argparse
+import locale as _locale
from . import __doc__ as _module_doc
from . import __version__
def main():
+ encoding = _locale.getpreferredencoding(do_setlocale=True)
+
parser = _argparse.ArgumentParser(description=_module_doc)
parser.add_argument(
'--version', action='version',
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)
+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)