diff options
author | Prabhu Ramachandran | 2016-05-10 20:09:08 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2016-05-10 20:09:08 +0530 |
commit | 5c74697b00ea08a2b78615637d8b322410fca4b0 (patch) | |
tree | d5b937e90bc7d3051b9c9128c4e1560b09db1c2c /yaksh/python_stdout_evaluator.py | |
parent | d386d24aaa662f91e4314060926dc9bc02426c7d (diff) | |
parent | c384c60c6d7fb5d30f3f929c518e0b41e084c4c4 (diff) | |
download | online_test-5c74697b00ea08a2b78615637d8b322410fca4b0.tar.gz online_test-5c74697b00ea08a2b78615637d8b322410fca4b0.tar.bz2 online_test-5c74697b00ea08a2b78615637d8b322410fca4b0.zip |
Merge pull request #96 from ankitjavalkar/code-eval-refactor-clean2
Code evaluator refactor
Diffstat (limited to 'yaksh/python_stdout_evaluator.py')
-rw-r--r-- | yaksh/python_stdout_evaluator.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py new file mode 100644 index 0000000..6606581 --- /dev/null +++ b/yaksh/python_stdout_evaluator.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +import sys +import traceback +import os +from os.path import join +import importlib +from contextlib import contextmanager + +# local imports +from code_evaluator import CodeEvaluator + + +@contextmanager +def redirect_stdout(): + from StringIO import StringIO + 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 PythonStdoutEvaluator(CodeEvaluator): + """Tests the Python code obtained from Code Server""" + + def compile_code(self, user_answer, expected_output): + if hasattr(self, 'output_value'): + return None + else: + submitted = compile(user_answer, '<string>', mode='exec') + with redirect_stdout() as output_buffer: + exec_scope = {} + exec submitted in exec_scope + self.output_value = output_buffer.getvalue() + return self.output_value + + def check_code(self, user_answer, 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" + + del tb + return success, err + |