From 712bcfe12a8edb2a6cfbe55d20ef7f1ef3735e3c Mon Sep 17 00:00:00 2001 From: Greg S Date: Thu, 14 May 2009 17:05:26 +0000 Subject: Working multiple choice and true/false questions. --- 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) -- cgit v0.9.1