Commit 7f92606e by Lyla Fischer

responses to code review

parent 4c0cc81d
...@@ -6,6 +6,7 @@ import random ...@@ -6,6 +6,7 @@ import random
import scipy import scipy
import traceback import traceback
import copy import copy
import abc
from calc import evaluator, UndefinedVariable from calc import evaluator, UndefinedVariable
from django.conf import settings from django.conf import settings
...@@ -38,10 +39,17 @@ def compare_with_tolerance(v1, v2, tol): ...@@ -38,10 +39,17 @@ def compare_with_tolerance(v1, v2, tol):
return abs(v1-v2) <= tolerance return abs(v1-v2) <= tolerance
class GenericResponse(object): class GenericResponse(object):
__metaclass__=abc.ABCMeta
@abc.abstractmethod
def grade(self, student_answers): def grade(self, student_answers):
pass pass
@abc.abstractmethod
def get_answers(self): def get_answers(self):
pass pass
#not an abstract method because plenty of responses will not want to preprocess anything, and we should not require that they override this method.
def preprocess_response(self): def preprocess_response(self):
pass pass
...@@ -57,7 +65,8 @@ class MultipleChoiceResponse(GenericResponse): ...@@ -57,7 +65,8 @@ class MultipleChoiceResponse(GenericResponse):
self.answer_id = xml.xpath('//*[@id=$id]//choicegroup/@id', self.answer_id = xml.xpath('//*[@id=$id]//choicegroup/@id',
id=xml.get('id')) id=xml.get('id'))
assert len(self.answer_id) == 1, "should have exactly one choice group per multiplechoicceresponse" if not len(self.answer_id) == 1:
raise Exception("should have exactly one choice group per multiplechoicceresponse")
self.answer_id=self.answer_id[0] self.answer_id=self.answer_id[0]
def grade(self, student_answers): def grade(self, student_answers):
...@@ -79,19 +88,14 @@ class TrueFalseResponse(MultipleChoiceResponse): ...@@ -79,19 +88,14 @@ class TrueFalseResponse(MultipleChoiceResponse):
response.set("type", "TrueFalse") response.set("type", "TrueFalse")
def grade(self, student_answers): def grade(self, student_answers):
correct = copy.deepcopy(self.correct_choices) correct = set(self.correct_choices)
if self.answer_id in student_answers and student_answers[self.answer_id]: answers = set(student_answers.get(self.answer_id, []))
for answer in student_answers[self.answer_id]:
if answer in correct: if correct == answers:
correct.remove(answer) return { self.answer_id : 'correct'}
else:
return {self.answer_id:'incorrect'} return {self.answer_id : 'incorrect'}
if len(correct) != 0:
return {self.answer_id:'incorrect'}
else:
return{self.answer_id:'correct'}
else:
return {self.answer_id:'incorrect'}
class NumericalResponse(GenericResponse): class NumericalResponse(GenericResponse):
def __init__(self, xml, context): def __init__(self, xml, context):
......
<problem> <problem>
<multiplechoiceresponse max="10" randomize="yes"> <multiplechoiceresponse>
<choicegroup> <choicegroup>
<choice location="random" correct="false" name="foil1"> <choice correct="false" name="foil1">
<startouttext />This is foil One.<endouttext /> <startouttext />This is foil One.<endouttext />
</choice> </choice>
<choice location="random" correct="false" name="foil2"> <choice correct="false" name="foil2">
<startouttext />This is foil Two.<endouttext /> <startouttext />This is foil Two.<endouttext />
</choice> </choice>
<choice location="random" correct="true" name="foil3"> <choice correct="true" name="foil3">
<startouttext />This is foil Three.<endouttext /> <startouttext />This is foil Three.<endouttext />
</choice> </choice>
<choice location="random" correct="false" name="foil4"> <choice correct="false" name="foil4">
<startouttext />This is foil Four.<endouttext /> <startouttext />This is foil Four.<endouttext />
</choice> </choice>
<choice location="random" correct="false" name="foil5"> <choice correct="false" name="foil5">
<startouttext />This is foil Five.<endouttext /> <startouttext />This is foil Five.<endouttext />
</choice> </choice>
</choicegroup> </choicegroup>
......
...@@ -63,7 +63,7 @@ class ModelsTest(unittest.TestCase): ...@@ -63,7 +63,7 @@ class ModelsTest(unittest.TestCase):
class MultiChoiceTest(unittest.TestCase): class MultiChoiceTest(unittest.TestCase):
def test_MC_grade(self): def test_MC_grade(self):
multichoice_file = os.getcwd()+"/djangoapps/courseware/test_files/multichoice.xml" multichoice_file = os.path.dirname(__file__)+"/test_files/multichoice.xml"
test_lcp = lcp.LoncapaProblem(multichoice_file, '1') test_lcp = lcp.LoncapaProblem(multichoice_file, '1')
correct_answers = {'1_2_1':'foil3'} correct_answers = {'1_2_1':'foil3'}
self.assertEquals(test_lcp.grade_answers(correct_answers)['1_2_1'], 'correct') self.assertEquals(test_lcp.grade_answers(correct_answers)['1_2_1'], 'correct')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment