From: W. Trevor King Date: Tue, 5 Feb 2013 15:37:41 +0000 (-0500) Subject: ui: Add dependency handling to UserInterface.get_question X-Git-Tag: v0.1~65 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=efac56f80f609dc3e4cd139a300c0be7352f1990;p=quizzer.git ui: Add dependency handling to UserInterface.get_question We start the stack with all the leaf questions (i.e. questions that are not dependencies of other question). If the user gets one wrong, we push the question back on the stack (so they can try again later), and also push all of that questions direct dependencies onto the stack (so they can get the background they need to answer the question they got wrong). --- diff --git a/quizzer/quiz.py b/quizzer/quiz.py index 86b029a..c3fa5a5 100644 --- a/quizzer/quiz.py +++ b/quizzer/quiz.py @@ -41,3 +41,19 @@ class Quiz (list): _json.dump( data, f, indent=2, separators=(',', ': '), sort_keys=True) f.write('\n') + + def leaf_questions(self): + "Questions that are not dependencies of other question" + dependents = set() + for question in self: + dependents.update(question.dependencies) + return [q for q in self if q.id not in dependents] + + def get(self, id=None): + matches = [q for q in self if q.id == id] + if len(matches) == 1: + return matches[0] + elif len(matches) == 0: + raise KeyError(id) + raise NotImplementedError( + 'multiple questions with one ID: {}'.format(matches)) diff --git a/quizzer/ui/__init__.py b/quizzer/ui/__init__.py index 8fb8b06..3a1c37f 100644 --- a/quizzer/ui/__init__.py +++ b/quizzer/ui/__init__.py @@ -3,11 +3,14 @@ from .. import answerdb as _answerdb class UserInterface (object): "Give a quiz over a generic user interface" - def __init__(self, quiz=None, answers=None): + def __init__(self, quiz=None, answers=None, stack=None): self.quiz = quiz if answers is None: answers = _answerdb.AnswerDatabase() self.answers = answers + if stack is None: + stack = quiz.leaf_questions() + self.stack = stack def run(self): raise NotImplementedError() @@ -16,11 +19,15 @@ class UserInterface (object): raise NotImplementedError() def get_question(self): - remaining = self.answers.get_unanswered(questions=self.quiz) - if remaining: - return remaining[0] + if self.stack: + print(self.stack) + return self.stack.pop(0) def process_answer(self, question, answer): correct = question.check(answer) self.answers.add(question=question, answer=answer, correct=correct) + if not correct: + self.stack.insert(0, question) + for qid in reversed(question.dependencies): + self.stack.insert(0, self.quiz.get(id=qid)) return correct