summaryrefslogtreecommitdiff
path: root/yaksh/python_assertion_evaluator.py
diff options
context:
space:
mode:
authorankitjavalkar2016-12-07 14:59:07 +0530
committerankitjavalkar2016-12-20 12:46:02 +0530
commitdee13fa4f8006d5266c02d6290b0e98d31413a9f (patch)
treed34bfed626a5e23689047318eda966c2495a2cfa /yaksh/python_assertion_evaluator.py
parent1400eeb1d5af1cd1d69e015a19a319ab35d357c4 (diff)
downloadonline_test-dee13fa4f8006d5266c02d6290b0e98d31413a9f.tar.gz
online_test-dee13fa4f8006d5266c02d6290b0e98d31413a9f.tar.bz2
online_test-dee13fa4f8006d5266c02d6290b0e98d31413a9f.zip
Refactor code server and python evaluator
Diffstat (limited to 'yaksh/python_assertion_evaluator.py')
-rw-r--r--yaksh/python_assertion_evaluator.py63
1 files changed, 43 insertions, 20 deletions
diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py
index 986dbf2..5f1b29e 100644
--- a/yaksh/python_assertion_evaluator.py
+++ b/yaksh/python_assertion_evaluator.py
@@ -11,32 +11,60 @@ from .code_evaluator import CodeEvaluator, TimeoutException
from .file_utils import copy_files, delete_files
-class PythonAssertionEvaluator(CodeEvaluator):
+class PythonAssertionEvaluator(object):
"""Tests the Python code obtained from Code Server"""
- def setup(self):
- super(PythonAssertionEvaluator, self).setup()
+ def __init__(self, metadata, test_case_data):
self.exec_scope = None
self.files = []
- def teardown(self):
+ # Set metadata values
+ self.user_answer = metadata.get('user_answer')
+ self.file_paths = metadata.get('file_paths')
+ self.partial_grading = metadata.get('partial_grading')
+
+ # Set test case data values
+ self.test_case = test_case_data.get('test_case')
+ self.weight = test_case_data.get('weight')
+
+ def __del__(self):
# Delete the created file.
if self.files:
delete_files(self.files)
- super(PythonAssertionEvaluator, self).teardown()
- def compile_code(self, user_answer, file_paths, test_case, weight):
- if file_paths:
- self.files = copy_files(file_paths)
+
+ # def setup(self):
+ # super(PythonAssertionEvaluator, self).setup()
+ # self.exec_scope = None
+ # self.files = []
+
+
+ # def teardown(self):
+ # # Delete the created file.
+ # if self.files:
+ # delete_files(self.files)
+ # super(PythonAssertionEvaluator, self).teardown()
+
+ # def teardown(self):
+ # # Delete the created file.
+ # if self.files:
+ # delete_files(self.files)
+ # # Cancel the signal
+ # delete_signal_handler()
+ # self._change_dir(dirname(MY_DIR))
+
+ def compile_code(self):
+ if self.file_paths:
+ self.files = copy_files(self.file_paths)
if self.exec_scope:
return None
else:
- submitted = compile(user_answer, '<string>', mode='exec')
+ submitted = compile(self.user_answer, '<string>', mode='exec')
self.exec_scope = {}
exec(submitted, self.exec_scope)
return self.exec_scope
- def check_code(self, user_answer, file_paths, partial_grading, test_case, weight):
+ def check_code(self):
""" Function validates user answer by running an assertion based test case
against it
@@ -61,26 +89,21 @@ class PythonAssertionEvaluator(CodeEvaluator):
test_case_weight = 0.0
try:
tb = None
- _tests = compile(test_case, '<string>', mode='exec')
+ _tests = compile(self.test_case, '<string>', mode='exec')
exec(_tests, self.exec_scope)
except AssertionError:
type, value, tb = sys.exc_info()
info = traceback.extract_tb(tb)
fname, lineno, func, text = info[-1]
- text = str(test_case).splitlines()[lineno-1]
+ text = str(self.test_case).splitlines()[lineno-1]
err = ("-----\nExpected Test Case:\n{0}\n"
- "Error - {1} {2} in: {3}\n-----").format(test_case,
- type.__name__,
- str(value), text
- )
- except TimeoutException:
- raise
+ "Error - {1} {2} in: {3}\n-----").format(self.test_case, type.__name__, str(value), text)
except Exception:
msg = traceback.format_exc(limit=0)
err = "Error in Test case: {0}".format(msg)
else:
success = True
- err = '-----\nCorrect answer\nTest Case: {0}\n-----'.format(test_case)
- test_case_weight = float(weight) if partial_grading else 0.0
+ err = '-----\nCorrect answer\nTest Case: {0}\n-----'.format(self.test_case)
+ test_case_weight = float(self.weight) if self.partial_grading else 0.0
del tb
return success, err, test_case_weight