From b5acdc84412cf27ef3f5cbd053815720549c9f21 Mon Sep 17 00:00:00 2001 From: "Brandon T. Willard" Date: Thu, 1 Sep 2022 16:51:57 -0500 Subject: [PATCH] Add a method for generic ExpressionTuple evaluation --- etuples/core.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/etuples/core.py b/etuples/core.py index d029008..926195c 100644 --- a/etuples/core.py +++ b/etuples/core.py @@ -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 @@ -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( @@ -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.") @@ -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 @@ -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: