forked from micado-scale/component-policy-keeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.py
120 lines (101 loc) · 3.49 KB
/
evaluator.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import sys
import time
import multiprocessing
import copy
from asteval import Interpreter, make_symbol_table
class TimeoutException(Exception):
""" It took too long to compile and execute. """
class RunnableProcessing(multiprocessing.Process):
""" Run a function in a child process.
Pass back any exception received.
"""
def __init__(self, func, *args, **kwargs):
self.queue = multiprocessing.Queue(maxsize=1)
args = (func,) + args
multiprocessing.Process.__init__(self, target=self.run_func,
args=args, kwargs=kwargs)
def run_func(self, func, *args, **kwargs):
try:
result = func(*args, **kwargs)
self.queue.put((True, result))
except Exception as e:
self.queue.put((False, e))
def done(self):
return self.queue.full()
def result(self):
return self.queue.get()
def timeout(seconds, force_kill=True):
""" Timeout decorator using Python multiprocessing.
Courtesy of http://code.activestate.com/recipes/577853-timeout-decorator-with-multiprocessing/
"""
def wrapper(function):
def inner(*args, **kwargs):
now = time.time()
proc = RunnableProcessing(function, *args, **kwargs)
proc.start()
proc.join(seconds)
if proc.is_alive():
if force_kill:
proc.terminate()
runtime = time.time() - now
raise TimeoutException('timed out after {0} seconds'.format(runtime))
assert proc.done()
success, result = proc.result()
if success:
return result
else:
raise result
return inner
return wrapper
@timeout(3)
def evaluate(eval_code, input_variables={}, output_variables=[]):
"""Evaluates a given expression, with the timeout given as decorator.
Args:
eval_code (str): The code to be evaluated.
input_variables (dict): dictionary of input variables and their values.
output_variables (array): array of names of output variables.
Returns:
dict: the output variables or empty.
"""
# FIXME: use_numpy the process blocks infinitely at the return statement
import time
sym = make_symbol_table(time=time, use_numpy=False, range=range, **input_variables)
aeval = Interpreter(
symtable = sym,
use_numpy = False,
no_if = False,
no_for = False,
no_while = False,
no_try = True,
no_functiondef = True,
no_ifexp = False,
no_listcomp = True,
no_augassign = False, # e.g., a += 1
no_assert = True,
no_delete = True,
no_raise = True,
no_print = False)
aeval(eval_code)
symtable = {x: sym[x] for x in sym if x in output_variables}
return symtable
if __name__ == "__main__":
# Example 1
code_example_1 = """
y = 0
for i in range(10000):
y = y + i
x = 5
"""
values_1 = evaluate(code_example_1, output_variables=['x', 'y'])
print("Example 1: Output variables: {}".format(values_1))
assert values_1['x'] == 5, "x value should be 5"
assert values_1['y'] == 49995000, "y value should be 49995000"
# Example 2
code_example_2 = """
for i in range(101):
y = y + i
"""
values_2 = evaluate(code_example_2, input_variables={'y': 10},
output_variables=['y'])
print("Example 2: Output variables: {}".format(values_2))
assert values_2['y'] == 5060, "y value should be 5060"