1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/Common/QuestionPlayer/collect_responses.py Tue Nov 25 00:07:07 2008 +0100
1.3 @@ -0,0 +1,105 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Collect responses from multiple answer files.
1.8 +"""
1.9 +
1.10 +from XSLForms.Resources.WebResources import resources
1.11 +from glob import glob
1.12 +import os
1.13 +import libxml2dom
1.14 +
1.15 +question_path = "/questionnaire/question"
1.16 +
1.17 +def collect(questions, responses_dir):
1.18 + answers = {}
1.19 + for filename in glob(os.path.join(responses_dir, "distilled-*")):
1.20 + responses = libxml2dom.parse(filename)
1.21 + collect_answers(answers, questions, responses)
1.22 + return answers
1.23 +
1.24 +def collect_answers(answers, questions, responses):
1.25 +
1.26 + # Get a mapping from questions to responses.
1.27 +
1.28 + for n, (question, response_question) in enumerate(map(None, questions.xpath(question_path), responses.xpath(question_path))):
1.29 +
1.30 + # Each response container can contain a number of responses.
1.31 +
1.32 + for response in response_question.xpath("response"):
1.33 +
1.34 + # Get the question number (starting from 1) and the selected response,
1.35 + # if any.
1.36 +
1.37 + qn = n + 1
1.38 + selected = response.getAttribute("selected")
1.39 + if selected is None:
1.40 + continue
1.41 +
1.42 + # Determine the selected response and whether a textual answer is
1.43 + # expected.
1.44 +
1.45 + selected = int(selected)
1.46 + textual_answer = bool(question.xpath("choice[position() = %d]/response-type[@value != 'choice']" % selected))
1.47 +
1.48 + # Collect the answer.
1.49 +
1.50 + if not answers.has_key(qn):
1.51 + answers[qn] = {}
1.52 + if not answers[qn].has_key(selected):
1.53 + if textual_answer:
1.54 + answers[qn][selected] = []
1.55 + else:
1.56 + answers[qn][selected] = 0
1.57 +
1.58 + # Either record counts or specific answers.
1.59 +
1.60 + if textual_answer:
1.61 + text = response.getAttribute("response-text")
1.62 + if text:
1.63 + answers[qn][selected].append(text)
1.64 + else:
1.65 + answers[qn][selected] += 1
1.66 +
1.67 +if __name__ == "__main__":
1.68 + resource_dir = resources(__file__)
1.69 + responses_dir = os.path.join(os.getcwd(), "Responses")
1.70 + questions = libxml2dom.parse(os.path.join(resource_dir, "questions.xml"))
1.71 + answers = collect(questions, responses_dir)
1.72 +
1.73 + # Get the collected answers in order.
1.74 +
1.75 + items = answers.items()
1.76 + items.sort()
1.77 +
1.78 + # Visit each answer record...
1.79 +
1.80 + for qn, answers_for_question in items:
1.81 + question = questions.xpath(question_path + ("[position() = %d]" % qn))[0]
1.82 + print "Question %d: %s" % (qn, question.getAttribute("question-text").strip())
1.83 +
1.84 + # Get the response details in order.
1.85 +
1.86 + items_for_question = answers_for_question.items()
1.87 + items_for_question.sort()
1.88 +
1.89 + # Visit each distinct kind of response.
1.90 +
1.91 + for selected, results in items_for_question:
1.92 + choice = question.xpath("choice[position() = %d]" % selected)[0]
1.93 + choice_text = choice.getAttribute("response-choice").strip()
1.94 + if choice_text:
1.95 + print " ", ("%s:" % choice_text),
1.96 +
1.97 + if isinstance(results, int):
1.98 + print results
1.99 + else:
1.100 + print
1.101 + sorted_results = results[:]
1.102 + sorted_results.sort()
1.103 + for result in sorted_results:
1.104 + print " ", result
1.105 +
1.106 + print
1.107 +
1.108 +# vim: tabstop=4 expandtab shiftwidth=4