From cf52baf30e5902c2f3c79cfbd8df88789ccd09ce Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Thu, 2 Jun 2016 16:03:21 +0530 Subject: added python stdio evaluator --- yaksh/python_stdio_evaluator.py | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 yaksh/python_stdio_evaluator.py (limited to 'yaksh/python_stdio_evaluator.py') diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py new file mode 100644 index 0000000..5db80f5 --- /dev/null +++ b/yaksh/python_stdio_evaluator.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +import sys +import traceback +import os +from os.path import join +import importlib +from contextlib import contextmanager +from ast import literal_eval +# local imports +from code_evaluator import CodeEvaluator +from StringIO import StringIO + + +@contextmanager +def redirect_stdout(): + new_target = StringIO() + + old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout + try: + yield new_target # run some code with the replaced stdout + finally: + sys.stdout = old_target # restore to the previous value + + +class PythonStdioEvaluator(CodeEvaluator): + """Tests the Python code obtained from Code Server""" + + def compile_code(self, user_answer, expected_input, expected_output): + submitted = compile(user_answer, '', mode='exec') + if expected_input: + input_buffer = StringIO() + try: + literal_input = literal_eval(expected_input.split("\n")) + except ValueError: + literal_input = expected_input.split("\n") + for inputs in literal_input: + input_buffer.write(str(inputs)+'\n') + input_buffer.seek(0) + sys.stdin = input_buffer + + with redirect_stdout() as output_buffer: + exec_scope = {} + exec submitted in exec_scope + self.output_value = output_buffer.getvalue().rstrip("\n") + return self.output_value + + def check_code(self, user_answer, expected_input, expected_output): + success = False + + tb = None + if expected_output in user_answer: + success = False + err = ("Incorrect Answer: Please avoid " + "printing the expected output directly" + ) + elif self.output_value == expected_output: + + success = True + err = "Correct Answer" + + else: + success = False + err = """Incorrect Answer:\nExpected output - {0} + and your output - {1}"""\ + .format(expected_output, self.output_value) + + del tb + return success, err -- cgit From e46bd90fb2907a7a43786bee95985072e256cd56 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Thu, 2 Jun 2016 16:30:44 +0530 Subject: removed literal_eval from python_stdio_evaluator --- yaksh/python_stdio_evaluator.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'yaksh/python_stdio_evaluator.py') diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index 5db80f5..003337d 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -29,12 +29,7 @@ class PythonStdioEvaluator(CodeEvaluator): submitted = compile(user_answer, '', mode='exec') if expected_input: input_buffer = StringIO() - try: - literal_input = literal_eval(expected_input.split("\n")) - except ValueError: - literal_input = expected_input.split("\n") - for inputs in literal_input: - input_buffer.write(str(inputs)+'\n') + input_buffer.write(expected_input) input_buffer.seek(0) sys.stdin = input_buffer -- cgit From 1b71abc9437d721a41f017db406f312755f5a4c4 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Thu, 28 Jul 2016 18:24:18 +0530 Subject: added expected input in traceback for python and made minor changes in bash_stio_evaluator and stdio_evaluator --- yaksh/python_stdio_evaluator.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'yaksh/python_stdio_evaluator.py') diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index 003337d..aeec744 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -10,11 +10,10 @@ from ast import literal_eval from code_evaluator import CodeEvaluator from StringIO import StringIO - +from textwrap import dedent @contextmanager def redirect_stdout(): new_target = StringIO() - old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout try: yield new_target # run some code with the replaced stdout @@ -32,7 +31,6 @@ class PythonStdioEvaluator(CodeEvaluator): input_buffer.write(expected_input) input_buffer.seek(0) sys.stdin = input_buffer - with redirect_stdout() as output_buffer: exec_scope = {} exec submitted in exec_scope @@ -43,21 +41,19 @@ class PythonStdioEvaluator(CodeEvaluator): success = False tb = None - if expected_output in user_answer: - success = False - err = ("Incorrect Answer: Please avoid " - "printing the expected output directly" - ) - elif self.output_value == expected_output: - + if self.output_value == expected_output: success = True err = "Correct Answer" - else: success = False - err = """Incorrect Answer:\nExpected output - {0} - and your output - {1}"""\ - .format(expected_output, self.output_value) - + err = dedent(""" + Incorrect Answer: + Given input - {0}, + Expected output - {1} and your output - {2} + """ + .format(expected_input, + expected_output, self.output_value + ) + ) del tb return success, err -- cgit