diff options
author | Greg S <enimihil@gmail.com> | 2009-04-26 20:45:41 (GMT) |
---|---|---|
committer | Greg S <enimihil@gmail.com> | 2009-04-26 20:45:41 (GMT) |
commit | a4b2efd6002ed877a8138899f7be9da6ddf3b250 (patch) | |
tree | 5782ed9578c9cf9232a86ddd932340104c6aae91 | |
parent | 92670e0950f57746d52728b38dd6252607fa7765 (diff) |
Implement basic question types.
-rw-r--r-- | quizdata/question.py | 118 |
1 files changed, 71 insertions, 47 deletions
diff --git a/quizdata/question.py b/quizdata/question.py index 10f7c87..f0efcac 100644 --- a/quizdata/question.py +++ b/quizdata/question.py @@ -1,63 +1,87 @@ ''' quizdata.Question implementation and support functions ''' -from peak.util.symbols import Symbol - -TRUE_FALSE = Symbol('TRUE_FALSE', __name__) -MULTI_CHOICE = Symbol('MULTI_CHOICE', __name__) -SHORT_ANSWER = Symbol('SHORT_ANSWER', __name__) -MATCHING = Symbol('MATCHING', __name__) -MISSING_WORD = Symbol('MISSING_WORD', __name__) -NUMERICAL = Symbol('NUMERICAL', __name__) - -_FRIENDLY_NAMES = { - TRUE_FALSE : 'true/false', - MULTI_CHOICE : 'multiple choice', - SHORT_ANSWER : 'short answer', - MATCHING : 'matching', - MISSING_WORD : 'missing word', - NUMERICAL : 'numerical', - } - class Question(object): - def __init__(self, text, type, answers=None, title=None, category=None, - diffculty=None, level=None, tags=None): - self.text = text + ''' + Base question type, abstract. + ''' + def __init__(self, *args, **kwargs): + if 'title' in kwargs: + self.title = kwargs['title'] + if 'markup_type' in kwargs: + self.markup_type = kwargs['markup_type'] + else: + self.markup_type = str + if 'text' in kwargs: + if hasattr(self, 'markup_type'): + self.title = self.markup_type(kwargs['text']) + self.title = kwargs['text'] + else: + raise Exception("Questions must have text!") + if 'tags' in kwargs: + self.tags = kwargs['tags'] - if type not in [ TRUE_FALSE, MULTI_CHOICE, SHORT_ANSWER, MATCHING, - MISSING_WORD, NUMERICAL ]: - raise Exception("Type must be one of the defined question types") - self.type = type + def __repr__(self): + if hasattr(self, 'title'): + txt = self.title + else: + txt = self.text + return "<%s %r>" % (self.__class__.__name__, txt) - if answers is None and self.type != TRUE_FALSE: - raise Exception("Answers are required for questions of type %d" % self.type) - if title is None: - self.title = self.text +class MultipleChoiceQuestion(Question): + def __init__(self, *args, **kwargs): + super(MultipleChoiceQuestion, self).__init__(*args, **kwargs) + if 'answers' in kwargs: + self.answers = [] + for ans in kwargs['answers']: + self.answers.append(self.markup_type(ans)) + else: + raise Exception("MultipleChoiceQuestion requires answers!") + if 'correct' in kwargs: + self.correct = self.markup_type(kwargs['correct']) + else: + raise Exception("MultipleChoiceQuestion requires a correct answer!") - self.category = category - self.difficulty = difficulty - self.level = level - if tags is None: - self.tags = [] +class MissingWordQuestion(MultipleChoiceQuestion): + def __init__(self, *args, **kwargs): + super(MissingWordQuestion, self).__init__(*args, **kwargs) + if 'tail_text' in kwargs: + self.tail_text = self.markup_type(kwargs['tail_text']) + else: + raise Exception("MissingWordQuestion requires a tail_text!") - self.submitted = False - self.response = None - def show(self, surface, x, y, width=-1, height=-1): - raise NotImplementedError() +class TrueFalseQuestion(MultipleChoiceQuestion): + def __init__(self, *args, **kwargs): + kwargs['answers'] = ['True', 'False'] + super(TrueFalseQuestion, self).__init__(*args, **kwargs) - def clear(self): - raise NotImplementedError() - def answered(self): - return bool(self.response) +class ShortAnswerQuestion(Question): + def __init__(self, *args, **kwargs): + super(ShortAnswerQuestion, self).__init__(*args, **kwargs) + if 'correct' in kwargs: + self.correct = self.markup_type(kwargs['correct']) + else: + raise Exception("ShortAnswerQuestion requires a correct answer!") - def correct(self): - raise NotImplementedError() - def __repr__(self): - return "<%s Question: %s>" % (_FRIENDLY_NAMES[self.type].title(), - self.title) +class NumericalQuestion(Question): + def __init__(self, *args, **kwargs): + super(NumericalQuestion, self).__init__(self, *args, **kwargs) + raise NotImplementedError("Not implemented yet!") + + +class MatchingQuestion(Question): + def __init__(self, *args, **kwargs): + super(MatchingQuestion, self).__init__(*args, **kwargs) + if 'answers' in kwargs: + ans = [] + for a1, a2 in kwargs['answers']: + ans.append((self.markup_type(a1), self.markup_type(a2))) + self.answers = ans + else: + raise Exception("MatchingQuestion must have answers!") |