forked from Project-Platypus/Rhodium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
expr.py
110 lines (88 loc) · 3.44 KB
/
expr.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
# Copyright 2015-2016 David Hadka
#
# This file is part of Rhodium, a Python module for robust decision making and
# exploratory modeling.
#
# Rhodium is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rhodium is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rhodium. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, print_function, absolute_import
import six
import ast
_eval_env = {}
_modules = ["math"]
for modulename in _modules:
module = __import__(modulename, fromlist=[''])
for name in dir(module):
if not name.startswith("_"):
_eval_env[name] = getattr(module, name)
def _has_assignment(tree):
return isinstance(tree.body[0], ast.Assign)
def _get_result_keys(tree):
keys = []
if _has_assignment(tree):
target = tree.body[0].targets[0]
if isinstance(target, ast.Tuple):
keys.extend([t.id for t in target.elts if isinstance(t, ast.Name)])
elif isinstance(target, ast.Name):
keys.append(target.id)
return keys
def _evaluate(expr, env, update_env=True):
tmp_env = {}
tmp_env.update(_eval_env)
tmp_env.update(env)
if isinstance(expr, six.string_types):
tree = ast.parse(expr, mode="exec")
result_keys = _get_result_keys(tree)
if update_env and len(result_keys) > 0:
six.exec_(expr, {}, tmp_env)
for key in result_keys:
env[key] = tmp_env[key]
if len(result_keys) == 1:
return tmp_env[result_keys[0]]
else:
return [tmp_env[key] for key in result_keys]
else:
return eval(expr, {}, tmp_env)
elif six.callable(expr):
tmp_env = {}
tmp_env.update(env)
return expr(tmp_env)
else:
raise ValueError("expr must be a string or a callable")
def _evaluate_all(expr, iterable, update_env=True):
result = []
if isinstance(expr, six.string_types):
tree = ast.parse(expr, mode="exec")
result_keys = _get_result_keys(tree)
for env in iterable:
tmp_env = {}
tmp_env.update(_eval_env)
tmp_env.update(env)
if update_env and len(result_keys) > 0:
six.exec_(expr, {}, tmp_env)
for key in result_keys:
env[key] = tmp_env[key]
if len(result_keys) == 1:
result.append(tmp_env[result_keys[0]])
else:
result.append([tmp_env[key] for key in result_keys])
else:
result.append(eval(expr, {}, tmp_env))
elif six.callable(expr):
for env in iterable:
tmp_env = {}
tmp_env.update(env)
result.append(expr(tmp_env))
else:
raise ValueError("expr must be a string or a callable")
return result