diff options
author | ankitjavalkar | 2015-04-24 14:25:26 +0530 |
---|---|---|
committer | ankitjavalkar | 2015-04-26 21:11:52 +0530 |
commit | 8664a766406d6acf0d6a1688948153c407ea27f2 (patch) | |
tree | 7505ef02b5e36395fc7e92ff5cde7700f2025979 /testapp/exam/evaluate_python_code.py | |
parent | 17752a69114e7dbad266337e768013920aec8c0c (diff) | |
download | online_test-8664a766406d6acf0d6a1688948153c407ea27f2.tar.gz online_test-8664a766406d6acf0d6a1688948153c407ea27f2.tar.bz2 online_test-8664a766406d6acf0d6a1688948153c407ea27f2.zip |
Code Review: Code refactoring
- Rename files
- Create function for @classmethod call
- Fix current, add new testcases
- Fix views to fetch solution/ref_code_path fields in question post save
- Fix errors
Diffstat (limited to 'testapp/exam/evaluate_python_code.py')
-rw-r--r-- | testapp/exam/evaluate_python_code.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/testapp/exam/evaluate_python_code.py b/testapp/exam/evaluate_python_code.py new file mode 100644 index 0000000..2682897 --- /dev/null +++ b/testapp/exam/evaluate_python_code.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +import sys +import traceback +import os +from os.path import join +import importlib + +# local imports +from evaluate_code import EvaluateCode +from language_registry import registry + + +class EvaluatePythonCode(EvaluateCode): + """Tests the Python code obtained from Code Server""" + ## Public Protocol ########## + def evaluate_code(self): + success = False + + try: + tb = None + test_code = self._create_test_case() + submitted = compile(self.user_answer, '<string>', mode='exec') + g = {} + exec submitted in g + _tests = compile(test_code, '<string>', mode='exec') + exec _tests in g + except AssertionError: + type, value, tb = sys.exc_info() + info = traceback.extract_tb(tb) + fname, lineno, func, text = info[-1] + text = str(test_code).splitlines()[lineno-1] + err = "{0} {1} in: {2}".format(type.__name__, str(value), text) + else: + success = True + err = 'Correct answer' + + del tb + return success, err + + ## Private Protocol ########## + def _create_test_case(self): + """ + Create assert based test cases in python + """ + test_code = "" + for test_case in self.test_case_data: + pos_args = ", ".join(str(i) for i in test_case.get('pos_args')) if test_case.get('pos_args') \ + else "" + kw_args = ", ".join(str(k+"="+a) for k, a in test_case.get('kw_args').iteritems()) \ + if test_case.get('kw_args') else "" + args = pos_args + ", " + kw_args if pos_args and kw_args else pos_args or kw_args + tcode = "assert {0}({1}) == {2}" \ + .format(test_case.get('func_name'), args, test_case.get('expected_answer')) + test_code += tcode + "\n" + return test_code + + +registry.register('python', EvaluatePythonCode)
\ No newline at end of file |