summaryrefslogtreecommitdiff
path: root/testapp/exam/python_code_evaluator.py
blob: 05a5063485e38a2983e47aca9824401029622f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
import sys
import traceback
import os
from os.path import join
import importlib

# local imports
from code_evaluator import CodeEvaluator
# from language_registry import registry


class PythonCodeEvaluator(CodeEvaluator):
    """Tests the Python code obtained from Code Server"""
    # Private Protocol ##########
    def check_code(self):
        success = False

        try:
            tb = None
            test_code = self._create_test_case()
            submitted = compile(self.user_answer, '<string>', mode='exec')
            g = {}
            exec submitted in g
            _tests = compile(test_code, '<string>', 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)
        else:
            success = True
            err = 'Correct answer'

        del tb
        return success, err

    # # Public Protocol ##########
    # def evaluate_code(self):
    #     success = False

    #     try:
    #         tb = None
    #         test_code = self._create_test_case()
    #         submitted = compile(self.user_answer, '<string>', mode='exec')
    #         g = {}
    #         exec submitted in g
    #         _tests = compile(test_code, '<string>', 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)
    #     else:
    #         success = True
    #         err = 'Correct answer'

    #     del tb
    #     return success, err

    # Private Protocol ##########
    def _create_test_case(self):
        """
            Create assert based test cases in python
        """
        test_code = ""
        for test_case in self.test_case_data:
            pos_args = ", ".join(str(i) for i in test_case.get('pos_args')) \
                                if test_case.get('pos_args') else ""
            kw_args = ", ".join(str(k+"="+a) for k, a
                             in test_case.get('kw_args').iteritems()) \
                            if test_case.get('kw_args') else ""
            args = pos_args + ", " + kw_args if pos_args and kw_args \
                                                else pos_args or kw_args
            tcode = "assert {0}({1}) == {2}".format(test_case.get('func_name'),
                                                        args, test_case.get('expected_answer'))
            test_code += tcode + "\n"
        return test_code


# registry.register('python', EvaluatePythonCode)