[Tutor] help with augumented assignment y += 1

Norman Khine norman at khine.net
Wed Jul 30 14:42:35 CEST 2008


Hello,

I am writing a poll application, which has two XML files:

1) survey.xml

<?xml version="1.0" encoding="UTF-8"?>
<survey>
<question code="a1" type="MCQ">
   <title>Is an augumented assignment such as 'x = x + 1' the same as x 
+= 1?</title>
   <option>Yes</option>
   <option>No</option>
   <option>Don't know</option>
</question>
<question code="a2" type="MAQ">
   <title>The statement 'x *= y + 1' is equivalent to:</title>
   <option>'x = (x * y) + 1'</option>
   <option>'x = x * (y + 1)</option>
   <option>'x = (y + 1) * x'</option>
   <option>All of the above</option>
</question>
</survey>

2) respons.xml

<?xml version="1.0" encoding="UTF-8"?>
<response>
<attempt username="1" date="2008-07-28 14:40">
   <question code="a1">
     <answer>1</answer>
   </question>
   <question code="a2">
     <answer>2</answer>
     <answer>3</answer>
   </question>
</attempt>
<attempt username="5" date="2008-07-28 15:00">
   <question code="a1">
     <answer>1</answer>
   </question>
   <question code="a2">
     <answer>1</answer>
     <answer>2</answer>
   </question>
</attempt>
<attempt username="3" date="2008-07-28 14:47">
   <question code="a1">
     <answer>3</answer>
   </question>
   <question code="a2">
     <answer>4</answer>
   </question>
</attempt>
</response>

Here is the code I have so far, poll.py:

####

from folder import Folder
from handlers import Text
...

class Survey(Text):
     """
     Here we create the actual survey.xml file
     """

     def new(self):
         self.questions = {}

     def _load_state_from_file(self, file):
         ....

     def to_str(self, encoding='UTF-8'):
         lines = ['<?xml version="1.0" encoding="%s"?>\n' % encoding,
                  '<survey>\n']
         # Business functions
         # Questions
         questions = self.questions
         codes = questions.keys()
         codes.sort()
         for code in codes:
             lines.append(questions[code].to_str(encoding))
         lines.append('</survey>')
         return ''.join(lines)


class Response(Text):
     """
     Here we create the actual response.xml file
     """

     def new(self):
         self.attempts = {}

     def _load_state_from_file(self, file):
         # TEST 015
         attempts = {}

     def to_str(self, encoding='UTF-8'):
         lines = ['<?xml version="1.0" encoding="%s"?>' % encoding,
                  '<response>']
         attempts = self.attempts
         for username in attempts:
             for attempt in attempts[username]:
                 lines.append(
                     '<attempt username="%s" date="%s" mark="%2.2f">'
                     % (username, attempt.date.strftime('%Y-%m-%d 
%H:%M'), attempt.mark))
                 questions = attempt.questions
                 for question_code in questions:
                     lines.append('  <question code="%s">' % question_code)
                     for answer in questions[question_code]:
                         lines.append('    <answer>%s</answer>' % answer)
                     lines.append('  </question>')
                 lines.append('</attempt>')
         lines.append('</response>')
         return '\n'.join(lines)

     def get_analysis(self):
         attempts = self.attempts
         for username in attempts:
             user = []
             question = []
             for attempt in attempts[username]:
                 user.append(username)
                 questions = attempt.questions
                 answers = []
                 for question_code in questions:
                     for answer in questions[question_code]:
                         answers.append(answer)
                         question.append(question_code)

             # [SEE NOTE 1]
             print user, question, answers


class Attempt(object):

     def __init__(self, username=None, date=None):
         self.username = username
         self.date = date
         self.questions = {}


class Poll(Folder):

     def new(self):
         Folder.new(self)
         self.cache['.survey'] = Survey()
         self.cache['.response'] = Response()

     def get_definition(self):
         return self.get_handler('.survey')
     definition = property(get_definition, None, None, '')

     def get_results(self):
         return self.get_handler('.response')
     results = property(get_results, None, None, '')

     def fill_form(self, context):
         user = context.user
         ...

     def analyse(self, context):
         user, root = context.user, context.root
         questions = self.definition.questions

         results = [{'question': x,
                     'title': y.title,
                     'options': [y.options]}
                          for x, y in questions.items()]
         # [SEE NOTE 2]
         print results


####

[NOTE 1] In the Response class I have a function 'get_analysis':

     def get_analysis(self):
             attempts = self.attempts
             ....

This returns:
['1'] ['1', '2', '2', '2'] [1, 1, 2, 4]
['0'] ['1', '2', '2'] [2, 2, 3]


[NOTE 2] In the Poll class, I have a function 'analyse':

    def analyse(self, context):
         user, root = context.user, context.root
         questions = self.definition.questions
         ...
This returns:

[{'question': 'a1', 'options': [[u'Yes', u'No', u"Don't know"]], 
'title': u"Is an augumented assignment such as 'x = x + 1' the same as x 
+= 1?"}, {'question': 'a2', 'options': [[u"'x = (x * y) + 1'", u"'x = x 
* (y + 1)", u"'x = (y + 1) * x'", u'All of the above']], 'title': u"The 
statement 'x *= y + 1' is equivalent to:"}]


This is where I get stuck and can't see the solution so that I can link both
.xml files and be able to return the totals for each answered question.

I would like to count the totals for each answer, so that:

options       | yes | no | don't know | 'x = (x * y) + 1' | 'x = x * (y 
+ 1)' |etc...
user 0        | 0   | 1  | 0          | 0                 | 1 
       |etc...
user 1        | 1   | 0  | 0          | 1                 | 1 
       |etc...
---------------------------------------------------------- 
-------------------
total         | 1   | 1  | 0          | 1                 | 2 
       |etc...
---------------------------------------------------------- 
-------------------

Ultimately, I would like to display the results as:

a1. Is an augumented assignment....
     [yes]         [1] [50%] *****
     [no]          [1] [50%] *****
     [don't know]  [0] [0%]

a2. The statement 'x *=....
     ['x = (x * y) + 1']         [1] [33%] ***
     ['x = x * (y + 1)']         [2] [77%] *******
    ....

Perhaps I would need a dictionary containing a list with totals for 
each, such as:

{'question': 'a1',
   'options': [{'option':  u'Yes',  'total': 1,
                 'option':  u'No',  'total': 1,
                 'option': u"Don't know", 'total: 0}],
   'title': u"Is an augumented assignment ...",
   'question': 'a2',
   'options': [{'option':  u'x = (x * y) + 1',  'total': 1,
                 'option':  u'x = x * (y + 1)',  'total': 2,
                 'option': u"...", 'total: 0}],
   'title': u"The statement..."}

But how do I calculate the total value for each option?

Any advise or suggestions and guidance in improving this much appreciated.



More information about the Tutor mailing list