summaryrefslogtreecommitdiff
path: root/yaksh/python_assertion_evaluator.py
diff options
context:
space:
mode:
authorankitjavalkar2016-03-17 16:42:33 +0530
committerankitjavalkar2016-05-05 19:00:33 +0530
commit0520bf284f9b34782fa90b433d714c887049f339 (patch)
treeff1d81f59ea849740bd37ca70a11eaa821187fbc /yaksh/python_assertion_evaluator.py
parent195aead9b0fab0d8cdb86a9fc884ac3edca5db84 (diff)
downloadonline_test-0520bf284f9b34782fa90b433d714c887049f339.tar.gz
online_test-0520bf284f9b34782fa90b433d714c887049f339.tar.bz2
online_test-0520bf284f9b34782fa90b433d714c887049f339.zip
- Add test cases for multiple python evaluators
- Change name from python_standard_evaluator to python_assertion_evaluator
Diffstat (limited to 'yaksh/python_assertion_evaluator.py')
-rw-r--r--yaksh/python_assertion_evaluator.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py
new file mode 100644
index 0000000..0615d84
--- /dev/null
+++ b/yaksh/python_assertion_evaluator.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+import sys
+import traceback
+import os
+from os.path import join
+import importlib
+
+# local imports
+from code_evaluator import CodeEvaluator, TimeoutException
+
+
+class PythonAssertionEvaluator(CodeEvaluator):
+ """Tests the Python code obtained from Code Server"""
+
+ # def check_code(self, test, user_answer, ref_code_path):
+ def check_code(self, user_answer, test_case_data):
+ success = False
+ try:
+ tb = None
+ submitted = compile(user_answer, '<string>', mode='exec')
+ g = {}
+ exec submitted in g
+ for test_code in test_case_data:
+ _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)
+ except TimeoutException:
+ raise
+ except Exception:
+ err = traceback.format_exc(limit=0)
+ else:
+ success = True
+ err = 'Correct answer'
+
+ del tb
+ return success, err