Commit 81c4e4f7 by Ned Batchelder

Make check_function more flexible so symbolicresponse can pass in more information.

parent 42eee48e
...@@ -927,15 +927,19 @@ class CustomResponse(LoncapaResponse): ...@@ -927,15 +927,19 @@ class CustomResponse(LoncapaResponse):
# actual function that will re-execute the original script, # actual function that will re-execute the original script,
# and invoke the function with the data needed. # and invoke the function with the data needed.
def make_check_function(script_code, cfn): def make_check_function(script_code, cfn):
def check_function(expect, ans): def check_function(expect, ans, **kwargs):
code = (script_code + "\n" + code = [script_code, "kwargs = {}"]
"cfn_return = %s(expect, ans)\n" % cfn) for name, val in kwargs.iteritems():
if isinstance(val, (str, list, tuple, int, long, float, dict)):
code.append("kwargs[%r] = %r" % (name, val))
code.append("cfn_return = %s(expect, ans, **kwargs)" % cfn)
code.append("") # a final newline
globals_dict = { globals_dict = {
'expect': expect, 'expect': expect,
'ans': ans, 'ans': ans,
} }
locals_dict = {} locals_dict = {}
safe_exec.safe_exec(code, globals_dict, locals_dict) safe_exec.safe_exec("\n".join(code), globals_dict, locals_dict)
return locals_dict['cfn_return'] return locals_dict['cfn_return']
return check_function return check_function
...@@ -954,6 +958,8 @@ class CustomResponse(LoncapaResponse): ...@@ -954,6 +958,8 @@ class CustomResponse(LoncapaResponse):
else: else:
self.code = answer.text self.code = answer.text
self.cfn_kwargs_keys = []
def get_score(self, student_answers): def get_score(self, student_answers):
''' '''
student_answers is a dict with everything from request.POST, but with the first part student_answers is a dict with everything from request.POST, but with the first part
...@@ -1054,7 +1060,8 @@ class CustomResponse(LoncapaResponse): ...@@ -1054,7 +1060,8 @@ class CustomResponse(LoncapaResponse):
log.debug(" submission = %s" % submission) log.debug(" submission = %s" % submission)
try: try:
answer_given = submission[0] if (len(idset) == 1) else submission answer_given = submission[0] if (len(idset) == 1) else submission
ret = fn(self.expect, answer_given) kwargs = {n:self.context.get(n) for n in self.cfn_kwargs_keys}
ret = fn(self.expect, answer_given, **kwargs)
except Exception as err: except Exception as err:
self._handle_exec_exception(err) self._handle_exec_exception(err)
...@@ -1203,6 +1210,7 @@ class SymbolicResponse(CustomResponse): ...@@ -1203,6 +1210,7 @@ class SymbolicResponse(CustomResponse):
self.context['script_code'] += "from symmath import symmath_check\n" self.context['script_code'] += "from symmath import symmath_check\n"
self.xml.set('cfn', 'symmath_check') self.xml.set('cfn', 'symmath_check')
CustomResponse.setup_response(self) CustomResponse.setup_response(self)
self.cfn_kwargs_keys.extend(['dynamath', 'options', 'debug'])
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
......
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