From 0520bf284f9b34782fa90b433d714c887049f339 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 17 Mar 2016 16:42:33 +0530 Subject: - Add test cases for multiple python evaluators - Change name from python_standard_evaluator to python_assertion_evaluator --- yaksh/python_assertion_evaluator.py | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 yaksh/python_assertion_evaluator.py (limited to 'yaksh/python_assertion_evaluator.py') 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, '', mode='exec') + g = {} + exec submitted in g + for test_code in test_case_data: + _tests = compile(test_code, '', 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 -- cgit From d3241512c71d61b355358a691d18e4ff8a8df34c Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 6 Apr 2016 11:26:52 +0530 Subject: Multiple test cases passed as dicts, check_code() is iterated based on no. of test cases --- yaksh/python_assertion_evaluator.py | 10 +++++----- 1 file changed, 5 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 0615d84..15ff8fd 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -13,21 +13,21 @@ 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): + # def check_code(self, user_answer, test_case_data): #@@@v2 + def check_code(self, user_answer, test_case): success = False try: tb = None submitted = compile(user_answer, '', mode='exec') g = {} exec submitted in g - for test_code in test_case_data: - _tests = compile(test_code, '', mode='exec') - exec _tests in g + _tests = compile(test_case, '', 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] + text = str(test_case).splitlines()[lineno-1] err = "{0} {1} in: {2}".format(type.__name__, str(value), text) except TimeoutException: raise -- cgit From 2c7f278382f4fe8071508b0a880aae34f8edfd5e Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 18 Apr 2016 15:22:25 +0530 Subject: add compile_code function to compile before checking --- yaksh/python_assertion_evaluator.py | 17 +++++++++++++---- 1 file changed, 13 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 15ff8fd..c6661d1 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -12,17 +12,26 @@ from code_evaluator import CodeEvaluator, TimeoutException class PythonAssertionEvaluator(CodeEvaluator): """Tests the Python code obtained from Code Server""" + def compile_code(self, user_answer, test_case): + if hasattr(self, 'g'): + return None + else: + submitted = compile(user_answer, '', mode='exec') + self.g = {} + exec submitted in self.g + return self.g + # def check_code(self, test, user_answer, ref_code_path): # def check_code(self, user_answer, test_case_data): #@@@v2 def check_code(self, user_answer, test_case): success = False try: tb = None - submitted = compile(user_answer, '', mode='exec') - g = {} - exec submitted in g + # submitted = compile(user_answer, '', mode='exec') + # g = {} + # exec submitted in g _tests = compile(test_case, '', mode='exec') - exec _tests in g + exec _tests in self.g except AssertionError: type, value, tb = sys.exc_info() info = traceback.extract_tb(tb) -- cgit From 5684b1b19fcb383f494f0bfc04ad1bb760abce74 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 21 Apr 2016 17:31:09 +0530 Subject: Post review improvements: - compiled_output is set during setup - python exec context renamed - _compile_command deprecated --- yaksh/python_assertion_evaluator.py | 14 +++++++++----- 1 file changed, 9 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 c6661d1..82b5912 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -12,14 +12,18 @@ from code_evaluator import CodeEvaluator, TimeoutException class PythonAssertionEvaluator(CodeEvaluator): """Tests the Python code obtained from Code Server""" + def setup(self): + super(PythonAssertionEvaluator, self).setup() + self.exec_scope = None + def compile_code(self, user_answer, test_case): - if hasattr(self, 'g'): + if self.exec_scope: return None else: submitted = compile(user_answer, '', mode='exec') - self.g = {} - exec submitted in self.g - return self.g + self.exec_scope = {} + exec submitted in self.exec_scope + return self.exec_scope # def check_code(self, test, user_answer, ref_code_path): # def check_code(self, user_answer, test_case_data): #@@@v2 @@ -31,7 +35,7 @@ class PythonAssertionEvaluator(CodeEvaluator): # g = {} # exec submitted in g _tests = compile(test_case, '', mode='exec') - exec _tests in self.g + exec _tests in self.exec_scope except AssertionError: type, value, tb = sys.exc_info() info = traceback.extract_tb(tb) -- cgit From 23b7abd3c1125e4c875e214e4f673c48c4bf4752 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 2 May 2016 11:52:58 +0530 Subject: Remove commented code and cleanup --- yaksh/python_assertion_evaluator.py | 5 ----- 1 file changed, 5 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 82b5912..ff56421 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -25,15 +25,10 @@ class PythonAssertionEvaluator(CodeEvaluator): exec submitted in self.exec_scope return self.exec_scope - # def check_code(self, test, user_answer, ref_code_path): - # def check_code(self, user_answer, test_case_data): #@@@v2 def check_code(self, user_answer, test_case): success = False try: tb = None - # submitted = compile(user_answer, '', mode='exec') - # g = {} - # exec submitted in g _tests = compile(test_case, '', mode='exec') exec _tests in self.exec_scope except AssertionError: -- cgit From d953f6f9e62671eeb5d6ea6498475167301dfe91 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 2 May 2016 14:38:13 +0530 Subject: - Fix tests - Fix minor views.py - Fix minor errors caused due to conflict resolution, rebasing - Fix errors in forms, views - Minor Button label change in addquestion.html - Add snippet to question - Remove commented code --- yaksh/python_assertion_evaluator.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'yaksh/python_assertion_evaluator.py') diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index ff56421..bf6a4be 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -39,8 +39,6 @@ class PythonAssertionEvaluator(CodeEvaluator): 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' -- cgit