From dee13fa4f8006d5266c02d6290b0e98d31413a9f Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 7 Dec 2016 14:59:07 +0530 Subject: Refactor code server and python evaluator --- yaksh/python_assertion_evaluator.py | 63 +++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 20 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') 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, '', mode='exec') + submitted = compile(self.user_answer, '', 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, '', mode='exec') + _tests = compile(self.test_case, '', 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 -- cgit From b59e11188609ef10150f76d75f75882f8ae20269 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 8 Dec 2016 14:41:49 +0530 Subject: Fix test cases to reflect code server and python evaluator changes --- yaksh/python_assertion_evaluator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 5f1b29e..3e172ec 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -27,10 +27,10 @@ class PythonAssertionEvaluator(object): 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) + # def __del__(self): + # # Delete the created file. + # if self.files: + # delete_files(self.files) # def setup(self): -- cgit From 80a4feef3c209e044e8cbe31e44c81d69136e100 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 15 Dec 2016 16:34:18 +0530 Subject: Add further changes to code evaluator --- yaksh/python_assertion_evaluator.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 3e172ec..eb13f53 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -7,11 +7,12 @@ from os.path import join import importlib # Local imports -from .code_evaluator import CodeEvaluator, TimeoutException from .file_utils import copy_files, delete_files +from .base_evaluator import BaseEvaluator +from .code_evaluator import TimeoutException -class PythonAssertionEvaluator(object): +class PythonAssertionEvaluator(BaseEvaluator): """Tests the Python code obtained from Code Server""" def __init__(self, metadata, test_case_data): @@ -32,7 +33,6 @@ class PythonAssertionEvaluator(object): # if self.files: # delete_files(self.files) - # def setup(self): # super(PythonAssertionEvaluator, self).setup() # self.exec_scope = None @@ -98,9 +98,11 @@ class PythonAssertionEvaluator(object): text = str(self.test_case).splitlines()[lineno-1] err = ("-----\nExpected Test Case:\n{0}\n" "Error - {1} {2} in: {3}\n-----").format(self.test_case, type.__name__, str(value), text) + except TimeoutException: + raise except Exception: - msg = traceback.format_exc(limit=0) - err = "Error in Test case: {0}".format(msg) + 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(self.test_case) -- cgit From f1da39aded67efa3da145851f0e9f687a3e434e5 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 19 Dec 2016 11:44:55 +0530 Subject: Change all evaluator structure and make sure eval test cases pass --- yaksh/python_assertion_evaluator.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index eb13f53..003e001 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -39,19 +39,10 @@ class PythonAssertionEvaluator(BaseEvaluator): # 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 teardown(self): + # Delete the created file. + if self.files: + delete_files(self.files) def compile_code(self): if self.file_paths: -- cgit From 4a0f94084bc26559ef3e26470619e87314f9d70e Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 19 Dec 2016 19:18:35 +0530 Subject: Remove commented code --- yaksh/python_assertion_evaluator.py | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 003e001..8e4f0b3 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -28,17 +28,6 @@ class PythonAssertionEvaluator(BaseEvaluator): 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) - - # def setup(self): - # super(PythonAssertionEvaluator, self).setup() - # self.exec_scope = None - # self.files = [] - - def teardown(self): # Delete the created file. if self.files: -- cgit From 798d36aa12e22928e884668ae5c80a25d48393ea Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 19 Dec 2016 19:37:23 +0530 Subject: Change weight variable name to mark_fraction --- yaksh/python_assertion_evaluator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 8e4f0b3..0b49cb0 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -66,7 +66,7 @@ class PythonAssertionEvaluator(BaseEvaluator): the required permissions are not given to the file(s). """ success = False - test_case_weight = 0.0 + mark_fraction = 0.0 try: tb = None _tests = compile(self.test_case, '', mode='exec') @@ -86,6 +86,6 @@ class PythonAssertionEvaluator(BaseEvaluator): else: success = True err = '-----\nCorrect answer\nTest Case: {0}\n-----'.format(self.test_case) - test_case_weight = float(self.weight) if self.partial_grading else 0.0 + mark_fraction = float(self.weight) if self.partial_grading else 0.0 del tb - return success, err, test_case_weight + return success, err, mark_fraction -- cgit From bf5b4e7607bae0b81ceeb99e8bf5d750433e92e8 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Tue, 20 Dec 2016 12:42:44 +0530 Subject: Fix errors and rename resources - code_evaluator module and class renamed to grader - Test cases fixed - Comments removed - weight variable renamed to mark --- yaksh/python_assertion_evaluator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 0b49cb0..4d44838 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -9,7 +9,7 @@ import importlib # Local imports from .file_utils import copy_files, delete_files from .base_evaluator import BaseEvaluator -from .code_evaluator import TimeoutException +from .grader import TimeoutException class PythonAssertionEvaluator(BaseEvaluator): -- cgit