Fixed bug with showing answers related to complex numbers
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user