Skip to content

Commit

Permalink
adds Lazy factory for PythonExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
huettenhain committed Oct 24, 2024
1 parent 0e0ec1c commit 52d2d22
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions refinery/lib/argformats.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ def __call__(self, mapping: dict = None, **values):
variables.update(self.constants)
return eval(self.expression, None, variables)

@classmethod
def Lazy(cls, definition: str):
return cls(definition, all_variables_allowed=True)

@classmethod
def Evaluate(cls, definition: str, values: dict):
"""
Expand Down Expand Up @@ -508,7 +512,7 @@ def LazyPythonExpression(expression: str) -> MaybeDelayedType[Any]:
if unit.startswith(symbol):
expression = match['digits'] + (k * 3 * '0')
break
parser = PythonExpression(expression, all_variables_allowed=True)
parser = PythonExpression.Lazy(expression)
if parser.variables:
def evaluate(data: Chunk):
try:
Expand Down Expand Up @@ -1177,10 +1181,10 @@ def accu(
except KeyError:
raise ArgumentTypeError(F'The generator type {spec} is unknown.')
update, _, feed = spec.partition('#')
update = PythonExpression(update, all_variables_allowed=True)
update = PythonExpression.Lazy(update)
seed = seed or '0'
seed = PythonExpression(seed, all_variables_allowed=True)
feed = feed and PythonExpression(feed, all_variables_allowed=True)
seed = PythonExpression.Lazy(seed)
feed = feed and PythonExpression.Lazy(feed)
skip = 1 if skip is None else int(skip, 0)
precision = precision and int(precision, 0) or _DEFAULT_BITS
mask = precision and (1 << precision) - 1
Expand Down Expand Up @@ -1254,8 +1258,8 @@ def reduce(self, it: Iterable[int], reduction: str, seed: Optional[str] = None)
integer sequence and assigned back to `S`. The starting value of `S` is given by `seed`,
which has a default value of `0` and must also be given as a Python expression.
"""
seed = seed and PythonExpression(seed, all_variables_allowed=True)
reduction = PythonExpression(reduction, all_variables_allowed=True)
seed = seed and PythonExpression.Lazy(seed)
reduction = PythonExpression.Lazy(reduction)

def finalize(data: Optional[Chunk] = None):
def _reduction(S, B):
Expand Down

0 comments on commit 52d2d22

Please sign in to comment.