diff options
author | Greg S <enimihil@gmail.com> | 2009-05-14 17:05:26 (GMT) |
---|---|---|
committer | Greg S <enimihil@gmail.com> | 2009-05-14 17:05:26 (GMT) |
commit | 712bcfe12a8edb2a6cfbe55d20ef7f1ef3735e3c (patch) | |
tree | 6c9e67f92e7efdd55c2f2b3a7ba0357409542bc9 | |
parent | c8c4cc4ba699f26703c72a958786cb3a792ea952 (diff) |
Working multiple choice and true/false questions.
-rw-r--r-- | quizdata/_format_gift.py | 19 | ||||
-rw-r--r-- | quizdata/question.py | 12 | ||||
-rw-r--r-- | quizdata/text.py | 3 | ||||
-rwxr-xr-x | tests/complete_test.py | 20 |
4 files changed, 30 insertions, 24 deletions
diff --git a/quizdata/_format_gift.py b/quizdata/_format_gift.py index 6977d90..6078cab 100644 --- a/quizdata/_format_gift.py +++ b/quizdata/_format_gift.py @@ -101,15 +101,16 @@ def parse(stream, params): return ret def _question_maker(q): - answers = q['answers'][0] + answers = q['answers'] try: + #print "ANSWER KEYS: ", [ answer.keys() for answer in answers if hasattr(answer, 'keys') ] if 'text_additional' in q: #XXX This really a different question type? return _missing_word_question_maker(q) elif any('tf_text' in answer.keys() for answer in answers if hasattr(answer, 'keys')): return _true_false_question_maker(q) elif any('numeric_ans' in answer.keys() for answer in answers if hasattr(answer, 'keys')): print "XXX: SKIPPING NUMERICAL QUESTION!" - elif any("wrong_text" not in answer.keys() for answer in answers if hasattr(answer, 'keys')): + elif all("wrong_text" not in answer.keys() for answer in answers if hasattr(answer, 'keys')): return _short_answer_question_maker(q) elif all("left_match" in answer.keys() for answer in answers if hasattr(answer, 'keys')): _matching_question_maker(q) @@ -129,7 +130,7 @@ def _question_maker(q): def _missing_word_question_maker(q): answers = _make_answers(q['answers']) - correct = _get_correct_answer(answers) + correct = _get_correct_answer(q['answers']) params = { 'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode, 'tail_text': str(q['text_additional'][0]), @@ -143,7 +144,7 @@ def _missing_word_question_maker(q): def _short_answer_question_maker(q): answers = _make_answers(q['answers']) - correct = _get_correct_answer(answers) + correct = _get_correct_answer(q['answers']) params = { 'text': str(q['text'][0]), 'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode, @@ -176,7 +177,7 @@ def _true_false_question_maker(q): def _multi_choice_question_maker(q): answers = _make_answers(q['answers']) - correct = _get_correct_answer(answers) + correct = _get_correct_answer(q['answers']) params = { 'text': str(q['text'][0]), 'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode, @@ -191,8 +192,8 @@ class AnswerList(list): pass def _make_answers(a): - if all(['left_match'] in answer.keys() for answer in a): - return _make_matching_answers(a) + #if all('left_match' in a for answer in a): + # return _make_matching_answers(a) ret = AnswerList() for i in a: ret.append(str(i[1])) @@ -210,4 +211,6 @@ def _get_correct_answer(answers): #XXX: needs help... for a in answers: if a[0] == '=': - return a + return a[1] + else: + raise Exception("Couldn't find correct answer!?") diff --git a/quizdata/question.py b/quizdata/question.py index 0e3469c..3350cd7 100644 --- a/quizdata/question.py +++ b/quizdata/question.py @@ -52,7 +52,7 @@ class Question(object): self.markup_type = str if 'text' in kwargs: - self.title = self.markup_type(kwargs['text']) + self.text = self.markup_type(kwargs['text']) else: raise Exception("Questions must have text!") @@ -106,7 +106,9 @@ class MultipleChoiceQuestion(Question): @property def correct(self): - return self.answer == self._correct: + #print "DEBUG: self.answer: ", self.answer + #print "DEBUG: self._correct: ", self._correct + return self.answer == self._correct class TrueFalseQuestion(MultipleChoiceQuestion): @@ -114,6 +116,12 @@ class TrueFalseQuestion(MultipleChoiceQuestion): kwargs['answers'] = ['True', 'False'] super(TrueFalseQuestion, self).__init__(*args, **kwargs) + @property + def correct(self): + print "DEBUG: self.answer: ", self.answer + print "DEBUG: self._correct: ", self._correct + return self.answer.lower().startswith(self._correct.lower()) #XXX: hack? + #### XXX: The below classes aren't completely implemented. #### diff --git a/quizdata/text.py b/quizdata/text.py index 8ac56ed..a1113b6 100644 --- a/quizdata/text.py +++ b/quizdata/text.py @@ -25,9 +25,10 @@ def txt2html(s): def html2html(s): return s -@when(plain_text, (str, unicode)) def txt2txt(s): return s +when(plain_text, (str,))(txt2txt) +when(plain_text, (unicode,))(txt2txt) @when(plain_text, (htmlstr, uhtmlstr)) def html2txt(s): diff --git a/tests/complete_test.py b/tests/complete_test.py index 769dcd9..c81c424 100755 --- a/tests/complete_test.py +++ b/tests/complete_test.py @@ -12,29 +12,23 @@ from quizdata.question import MultipleChoiceQuestion, MissingWordQuestion from peak.rules import when def do_question(q): - print "Unhandled question type." + print "Unhandled question type.", type(q) @when(do_question, (MultipleChoiceQuestion,)) def do_multi_questions(q): print plain_text(q.text) for a in zip('0123456789', q.answers): - print a - answer = raw_input() + print "%5s: %s" % a + answer = int(raw_input()) + q.answer = q.answers[answer] print q.correct @when(do_question, (MissingWordQuestion,)) -def do_missing_multi_question(q): - print dir(q) - print plain_text(q.text), '__________', - print plain_text(q.tail_text) - for a in zip('0123456789', q.answers): - print a - answer = raw_input() - print q.correct - +def do_mw_question(q): # XXX: inheritance is annoying here... + print "Unhandled question type. (MissingWordQuestion)" def main(): - questions = quizdata.open("file://%s?format=gift" % path.join(base_path, 'tests', 'examples.txt')) + questions = quizdata.open("file://%s?format=gift" % path.join(base_path, 'tests', 'multi_choice.txt')) for q in questions: do_question(q) |