Skip to content

Commit

Permalink
pre-compile expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed May 11, 2023
1 parent 6a8da3f commit feec08d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions motile/constraints/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
except SyntaxError:
raise ValueError(f"Invalid expression: {expression}") from None

self.expression = expression
self._expression = compile(expression, "<string>", "eval")
self.eval_nodes = eval_nodes
self.eval_edges = eval_edges

Expand All @@ -95,7 +95,7 @@ def instantiate(self, solver: Solver) -> list[ilpy.Constraint]:
# if the expression uses a variable name that is not in the dict,
# a NameError will be raised.
# contextlib.suppress (above) will just skip it and move on...
if eval(self.expression, None, node_or_edge):
if eval(self._expression, None, node_or_edge):
# if the expression evaluates to True, we select the node/edge
select.set_coefficient(indicator_variables[id_], 1)
n_selected += 1
Expand Down

0 comments on commit feec08d

Please sign in to comment.