From d6a757f14d6b76c8124d52057cc981403d28d71a Mon Sep 17 00:00:00 2001 From: mahesh Date: Fri, 12 May 2017 18:25:53 +0530 Subject: changed stdio output --- yaksh/python_stdio_evaluator.py | 66 +++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 26 deletions(-) (limited to 'yaksh/python_stdio_evaluator.py') diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index a8c797d..ec5ed71 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -1,12 +1,16 @@ import sys from contextlib import contextmanager - try: from StringIO import StringIO except ImportError: from io import StringIO +try: + from itertools import zip_longest +except ImportError: + from itertools import izip_longest as zip_longest + # Local imports from .file_utils import copy_files, delete_files from .base_evaluator import BaseEvaluator @@ -21,35 +25,42 @@ def redirect_stdout(): finally: sys.stdout = old_target # restore to the previous value - -def _show_expected_given(expected, given): - return "Expected:\n{0}\nGiven:\n{1}\n".format(expected, given) - - -def compare_outputs(given, expected): - given_lines = given.splitlines() +def _incorrect_user_lines(exp_lines, user_lines): + err_line_no = [] + for i, (expected_line, user_line) in enumerate(zip_longest(exp_lines, user_lines)): + if not user_line or not expected_line: + err_line_no.append(i) + else: + if user_line.strip() != expected_line.strip(): + err_line_no.append(i) + return err_line_no + +def compare_outputs(expected_output, user_output,given_input=None): + given_lines = user_output.splitlines() + exp_lines = expected_output.splitlines() + # if given_input: + # given_input = given_input.splitlines() + msg = {"given_input":given_input, + "expected_output": exp_lines, + "user_output":given_lines + } ng = len(given_lines) - exp_lines = expected.splitlines() ne = len(exp_lines) if ng != ne: - msg = "ERROR: Got {0} lines in output, we expected {1}.\n".format( - ng, ne - ) - msg += _show_expected_given(expected, given) + err_line_no = _incorrect_user_lines(exp_lines, given_lines) + msg["error_no"] = err_line_no + msg["error"] = "We had expected {0} number of lines. We got {1} number of lines.".format(ne, ng) return False, msg else: - for i, (given_line, expected_line) in \ - enumerate(zip(given_lines, exp_lines)): - if given_line.strip() != expected_line.strip(): - msg = "ERROR:\n" - msg += _show_expected_given(expected, given) - msg += "\nError in line %d of output.\n" % (i+1) - msg += "Expected line {0}:\n{1}\nGiven line {0}:\n{2}\n"\ - .format( - i+1, expected_line, given_line - ) - return False, msg - return True, "Correct answer." + err_line_no = _incorrect_user_lines(exp_lines, given_lines) + if err_line_no: + msg["error_no"] = err_line_no + msg["error"] = "Line number(s) {0} did not match."\ + .format(", ".join(map(str,[x+1 for x in err_line_no]))) + return False, msg + else: + msg["error"] = "Correct answer" + return True, msg class PythonStdIOEvaluator(BaseEvaluator): @@ -89,5 +100,8 @@ class PythonStdIOEvaluator(BaseEvaluator): def check_code(self): mark_fraction = self.weight - success, err = compare_outputs(self.output_value, self.expected_output) + success, err = compare_outputs(self.expected_output, + self.output_value, + self.expected_input + ) return success, err, mark_fraction -- cgit From 3bbaa1e17778fb3790a9546d365444c38df31e1a Mon Sep 17 00:00:00 2001 From: mahesh Date: Sat, 13 May 2017 02:14:13 +0530 Subject: added pretty stdio functionality for all languages. --- yaksh/python_stdio_evaluator.py | 53 +++++------------------------------------ 1 file changed, 6 insertions(+), 47 deletions(-) (limited to 'yaksh/python_stdio_evaluator.py') diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index ec5ed71..d6201f9 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -6,14 +6,10 @@ try: except ImportError: from io import StringIO -try: - from itertools import zip_longest -except ImportError: - from itertools import izip_longest as zip_longest - # Local imports from .file_utils import copy_files, delete_files from .base_evaluator import BaseEvaluator +from .compare_stdio import CompareOutputs @contextmanager @@ -25,44 +21,6 @@ def redirect_stdout(): finally: sys.stdout = old_target # restore to the previous value -def _incorrect_user_lines(exp_lines, user_lines): - err_line_no = [] - for i, (expected_line, user_line) in enumerate(zip_longest(exp_lines, user_lines)): - if not user_line or not expected_line: - err_line_no.append(i) - else: - if user_line.strip() != expected_line.strip(): - err_line_no.append(i) - return err_line_no - -def compare_outputs(expected_output, user_output,given_input=None): - given_lines = user_output.splitlines() - exp_lines = expected_output.splitlines() - # if given_input: - # given_input = given_input.splitlines() - msg = {"given_input":given_input, - "expected_output": exp_lines, - "user_output":given_lines - } - ng = len(given_lines) - ne = len(exp_lines) - if ng != ne: - err_line_no = _incorrect_user_lines(exp_lines, given_lines) - msg["error_no"] = err_line_no - msg["error"] = "We had expected {0} number of lines. We got {1} number of lines.".format(ne, ng) - return False, msg - else: - err_line_no = _incorrect_user_lines(exp_lines, given_lines) - if err_line_no: - msg["error_no"] = err_line_no - msg["error"] = "Line number(s) {0} did not match."\ - .format(", ".join(map(str,[x+1 for x in err_line_no]))) - return False, msg - else: - msg["error"] = "Correct answer" - return True, msg - - class PythonStdIOEvaluator(BaseEvaluator): """Tests the Python code obtained from Code Server""" def __init__(self, metadata, test_case_data): @@ -100,8 +58,9 @@ class PythonStdIOEvaluator(BaseEvaluator): def check_code(self): mark_fraction = self.weight - success, err = compare_outputs(self.expected_output, - self.output_value, - self.expected_input - ) + compare = CompareOutputs() + success, err = compare.compare_outputs(self.expected_output, + self.output_value, + self.expected_input + ) return success, err, mark_fraction -- cgit From 77f05d3df90a70ff97285deb5bda2d91d99e65d5 Mon Sep 17 00:00:00 2001 From: mahesh Date: Mon, 22 May 2017 14:32:47 +0530 Subject: changes variable and dict key names along with other minor changes in answerpaper. --- yaksh/python_stdio_evaluator.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'yaksh/python_stdio_evaluator.py') diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index d6201f9..2b443a7 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -9,7 +9,7 @@ except ImportError: # Local imports from .file_utils import copy_files, delete_files from .base_evaluator import BaseEvaluator -from .compare_stdio import CompareOutputs +from .compare_stdio import compare_outputs @contextmanager @@ -46,6 +46,7 @@ class PythonStdIOEvaluator(BaseEvaluator): self.files = copy_files(self.file_paths) submitted = compile(self.user_answer, '', mode='exec') if self.expected_input: + self.expected_input = self.expected_input.replace('\r', '') input_buffer = StringIO() input_buffer.write(self.expected_input) input_buffer.seek(0) @@ -58,9 +59,8 @@ class PythonStdIOEvaluator(BaseEvaluator): def check_code(self): mark_fraction = self.weight - compare = CompareOutputs() - success, err = compare.compare_outputs(self.expected_output, - self.output_value, - self.expected_input - ) + success, err = compare_outputs(self.expected_output, + self.output_value, + self.expected_input + ) return success, err, mark_fraction -- cgit