diff options
author | Greg S <enimihil@gmail.com> | 2009-05-08 05:30:40 (GMT) |
---|---|---|
committer | Greg S <enimihil@gmail.com> | 2009-05-08 05:30:40 (GMT) |
commit | 79df3829e013f1d32f9c87804a5941f866354299 (patch) | |
tree | 75faaf7979cfff0ae59bd1ec027ad544ca2e5aa2 | |
parent | 1290ff4b499b14f20e625c5c7132df12e24a59c3 (diff) |
yep, closer to end-to-end complete.
-rw-r--r-- | quizdata/_format_gift.py | 59 | ||||
-rw-r--r-- | quizdata/text.py | 2 | ||||
-rwxr-xr-x | tests/complete_test.py | 43 |
3 files changed, 79 insertions, 25 deletions
diff --git a/quizdata/_format_gift.py b/quizdata/_format_gift.py index cd5c73b..6977d90 100644 --- a/quizdata/_format_gift.py +++ b/quizdata/_format_gift.py @@ -51,11 +51,11 @@ qtitle = dcolon + title_text + Optional(NL) + dcolon + Optional(NL) qformat = Literal("[").suppress() + format_text + Literal("]").suppress() + Optional(NL) -correct_ans = (Literal("=") + ans_text) -wrong_ans = Literal("~") + ans_text -ans_explain = Literal("#") + ans_text -ans_tf = oneOf("T F TRUE FALSE") -ans_match = Literal("=") + ans_text + Literal("->") + ans_text +correct_ans = (Literal("=") + ans_text)("correct_text") +wrong_ans = Literal("~") + ans_text("wrong_text") +ans_explain = Literal("#") + ans_text("explain_text") +ans_tf = oneOf("T F TRUE FALSE")("tf_text") +ans_match = Literal("=") + ans_text("left_match") + Literal("->") + ans_text("right_match") ans_numeric = Group(Optional(Literal("=")) + (number("number") + Optional( Literal(":") + number("range")) @@ -63,7 +63,7 @@ ans_numeric = Group(Optional(Literal("=")) + Optional( ans_explain )("explain")) + Optional(NL) answer = Group(( ( correct_ans ^ wrong_ans ^ ans_tf )("ans_text") + Optional(ans_explain)("explain")) - ^ ans_match ) + Optional(NL) + ^ ans_match("matching_answer") ) + Optional(NL) answer_list = Group((Literal("{").suppress() + Optional(NL) @@ -72,7 +72,7 @@ answer_list = Group((Literal("{").suppress() ^ (Literal("{#").suppress() + Optional(NL) - + OneOrMore(ans_numeric) + + OneOrMore(ans_numeric)("numeric_ans") + Literal("}").suppress())) question = ZeroOrMore(NL) + Group( @@ -94,24 +94,27 @@ def parse(stream, params): parsed = _search_file(question, stream) ret = [] for q in parsed: - ret.append(_question_maker(q[0])) + ques = _question_maker(q[0]) + if ques: + ret.append(ques) return ret def _question_maker(q): + answers = q['answers'][0] try: - if 'text_additional' in q: - _missing_word_question_maker(q) - elif q['answers'][0][0] in ['T', 'F', 'TRUE', 'FALSE']: - _true_false_question_maker(q) - elif "->" in list(q['answers'][0]): - _matching_question_maker(q) - elif ':' in list(q['answers'][0]): + 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 len(q['answers']) == 1 and q['answers'][0][0] == '=': - _short_answer_question_maker(q) - elif len(q['answers']) > 1: - _multi_choice_question_maker(q) + elif any("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) + elif len(answers) > 1: + return _multi_choice_question_maker(q) else: print ("XXX: Couldn't identify question type!") print q @@ -126,7 +129,7 @@ def _question_maker(q): def _missing_word_question_maker(q): answers = _make_answers(q['answers']) - correct = answers.correct + correct = _get_correct_answer(answers) params = { 'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode, 'tail_text': str(q['text_additional'][0]), @@ -140,7 +143,7 @@ def _missing_word_question_maker(q): def _short_answer_question_maker(q): answers = _make_answers(q['answers']) - correct = answers.correct + correct = _get_correct_answer(answers) params = { 'text': str(q['text'][0]), 'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode, @@ -173,7 +176,7 @@ def _true_false_question_maker(q): def _multi_choice_question_maker(q): answers = _make_answers(q['answers']) - correct = answers.correct + correct = _get_correct_answer(answers) params = { 'text': str(q['text'][0]), 'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode, @@ -188,7 +191,7 @@ class AnswerList(list): pass def _make_answers(a): - if len(a[0]) > 2: + if all(['left_match'] in answer.keys() for answer in a): return _make_matching_answers(a) ret = AnswerList() for i in a: @@ -199,6 +202,12 @@ def _make_answers(a): def _make_matching_answers(a): ret = AnswerList() - for i in a: - ret.append((str(i[1]), str(i[3]))) + for i in a[0]: + ret.append((str(i['left_match']), str(i['right_match']))) return ret + +def _get_correct_answer(answers): + #XXX: needs help... + for a in answers: + if a[0] == '=': + return a diff --git a/quizdata/text.py b/quizdata/text.py index 2ac6d7a..8ac56ed 100644 --- a/quizdata/text.py +++ b/quizdata/text.py @@ -1,6 +1,8 @@ ''' markup / media -- conversion / coercion ''' +from peak.rules import abstract, when + class htmlstr(str): pass diff --git a/tests/complete_test.py b/tests/complete_test.py new file mode 100755 index 0000000..769dcd9 --- /dev/null +++ b/tests/complete_test.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +import sys +from os import path + +base_path = path.abspath(path.join(path.dirname(path.abspath(__file__)),'..')) +sys.path.append(base_path) + +import quizdata +from quizdata.text import plain_text +from quizdata.question import MultipleChoiceQuestion, MissingWordQuestion + +from peak.rules import when + +def do_question(q): + print "Unhandled question type." + +@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 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 main(): + questions = quizdata.open("file://%s?format=gift" % path.join(base_path, 'tests', 'examples.txt')) + for q in questions: + do_question(q) + + +if __name__=='__main__': + sys.exit(main()) |