From 1c5945db9ca1ef5a95a92dd37fdcfe02a16a1eb5 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Thu, 29 Mar 2012 14:46:53 -0400 Subject: [PATCH] Fixed bug with showing answers related to complex numbers --- djangoapps/courseware/capa/responsetypes.py | 1 - djangoapps/courseware/modules/capa_module.py | 9 ++++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/djangoapps/courseware/capa/responsetypes.py b/djangoapps/courseware/capa/responsetypes.py index c173eb6faf..9ba1bd6256 100644 --- a/djangoapps/courseware/capa/responsetypes.py +++ b/djangoapps/courseware/capa/responsetypes.py @@ -38,7 +38,6 @@ class numericalresponse(object): def __init__(self, xml, context): self.xml = xml self.correct_answer = contextualize_text(xml.get('answer'), context) - self.correct_answer = complex(self.correct_answer) self.tolerance_xml = xml.xpath('//*[@id=$id]//responseparam[@type="tolerance"]/@default', id=xml.get('id'))[0] self.tolerance = contextualize_text(self.tolerance_xml, context) diff --git a/djangoapps/courseware/modules/capa_module.py b/djangoapps/courseware/modules/capa_module.py index 17ee6264bd..7ec92ecd44 100644 --- a/djangoapps/courseware/modules/capa_module.py +++ b/djangoapps/courseware/modules/capa_module.py @@ -26,6 +26,12 @@ import courseware.content_parser as content_parser log = logging.getLogger("mitx.courseware") +class ComplexEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, complex): + return "{real:.7g}{imag:+.7g}*j".format(real = obj.real,imag = obj.imag) + return json.JSONEncoder.default(self, obj) + class Module(XModule): ''' Interface between capa_problem and x_module. Originally a hack meant to be refactored out, but it seems to be serving a useful @@ -240,7 +246,8 @@ class Module(XModule): if not self.answer_available(): raise Http404 else: - return json.dumps(self.lcp.get_question_answers()) + return json.dumps(self.lcp.get_question_answers(), + cls=ComplexEncoder) # Figure out if we should move these to capa_problem?