Skip to content

Commit

Permalink
Add a method for generic ExpressionTuple evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonwillard committed Sep 2, 2022
1 parent 1e9a057 commit b5acdc8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions etuples/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import warnings
from collections import deque
from collections.abc import Generator, Sequence
from typing import Any, Callable

etuple_repr = reprlib.Repr()
etuple_repr.maxstring = 100
Expand Down Expand Up @@ -164,6 +165,10 @@ def evaled_obj(self):
res = self._eval_step()
return trampoline_eval(res)

@evaled_obj.setter
def evaled_obj(self, obj):
raise ValueError("Value of evaluated expression cannot be set!")

@property
def eval_obj(self):
warnings.warn(
Expand All @@ -173,6 +178,9 @@ def eval_obj(self):
)
return trampoline_eval(self._eval_step())

def _eval_apply(self, op: Callable, op_args: inspect.BoundArguments) -> Any:
return op(*op_args.args, **op_args.kwargs)

def _eval_step(self):
if len(self._tuple) == 0:
raise InvalidExpression("Empty expression.")
Expand Down Expand Up @@ -212,7 +220,7 @@ def _eval_step(self):
)
op_args.apply_defaults()

_evaled_obj = op(*op_args.args, **op_args.kwargs)
_evaled_obj = self._eval_apply(op, op_args)

if isinstance(_evaled_obj, Generator):
self._evaled_obj = _evaled_obj
Expand All @@ -221,10 +229,6 @@ def _eval_step(self):
self._evaled_obj = _evaled_obj
yield self._evaled_obj

@evaled_obj.setter
def evaled_obj(self, obj):
raise ValueError("Value of evaluated expression cannot be set!")

def __add__(self, x):
res = self._tuple + x
if self._parent is not None and res == self._parent._tuple:
Expand Down

0 comments on commit b5acdc8

Please sign in to comment.