Container for a compiled condition.
| 364 | |
| 365 | |
| 366 | class CompiledCondition: |
| 367 | """Container for a compiled condition.""" |
| 368 | |
| 369 | @lazyattr |
| 370 | def index_variables(self) -> frozenset: |
| 371 | """Columns participating in the index expression.""" |
| 372 | idxexprs = self.index_expressions |
| 373 | idxvars = [] |
| 374 | for expr in idxexprs: |
| 375 | idxvar = expr[0] |
| 376 | if idxvar not in idxvars: |
| 377 | idxvars.append(idxvar) |
| 378 | return frozenset(idxvars) |
| 379 | |
| 380 | def __init__( |
| 381 | self, |
| 382 | func: ne.interpreter.NumExpr, |
| 383 | params: list[str], |
| 384 | idxexprs: list[tuple[Any, tuple[str, ...], Any]], |
| 385 | strexpr: str, |
| 386 | **kwargs, |
| 387 | ) -> None: |
| 388 | self.function = func |
| 389 | """The compiled function object corresponding to this condition.""" |
| 390 | self.parameters = params |
| 391 | """A list of parameter names for this condition.""" |
| 392 | self.index_expressions = idxexprs |
| 393 | """A list of expressions in the form ``(var, (ops), (limits))``.""" |
| 394 | self.string_expression = strexpr |
| 395 | """The indexable expression in string format.""" |
| 396 | self.kwargs = kwargs |
| 397 | """NumExpr kwargs (used to pass ex_uses_vml to numexpr)""" |
| 398 | |
| 399 | def __repr__(self) -> str: |
| 400 | return f"""idxexprs: {self.index_expressions} |
| 401 | strexpr: {self.string_expression} |
| 402 | idxvars: {self.index_variables}""" |
| 403 | |
| 404 | def with_replaced_vars( |
| 405 | self, condvars: dict[str, Column | np.ndarray] |
| 406 | ) -> CompiledCondition: |
| 407 | """Replace index limit variables with their values in-place. |
| 408 | |
| 409 | A new compiled condition is returned. Values are taken from |
| 410 | the `condvars` mapping and converted to Python scalars. |
| 411 | """ |
| 412 | exprs = self.index_expressions |
| 413 | exprs2 = [] |
| 414 | for expr in exprs: |
| 415 | idxlims = expr[2] # the limits are in third place |
| 416 | limit_values = [] |
| 417 | for idxlim in idxlims: |
| 418 | if isinstance(idxlim, tuple): # variable |
| 419 | idxlim = condvars[idxlim[0]] # look up value |
| 420 | idxlim = idxlim.tolist() # convert back to Python |
| 421 | limit_values.append(idxlim) |
| 422 | # Add this replaced entry to the new exprs2 |
| 423 | var, ops, _ = expr |
no outgoing calls
no test coverage detected