hbutils.expression.native.general
General expression utilities for native expression building.
This module defines a combined expression class that aggregates comparison, indexing, object access, logical, and arithmetic behaviors. It also exposes factory helpers to convert arbitrary values into expression instances, build identity expressions, or create constant expressions.
The module contains the following public components:
GeneralExpression- Combined expression type with multiple features.expr()- Factory to convert values/callables to expressions.keep()- Factory for identity expressions.raw()- Factory for constant expressions.
Example:
>>> from hbutils.expression.native.general import GeneralExpression, expr, keep, raw
>>> e_add = expr(lambda x: x + 1)
>>> e_id = keep()
>>> e_const = raw(42)
>>> # Expressions are composable through their base features.
>>> getter = expr(lambda d: d["value"])
__all__
- hbutils.expression.native.general.__all__ = ['GeneralExpression', 'expr', 'keep', 'raw']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
GeneralExpression
- class hbutils.expression.native.general.GeneralExpression(func: Callable | None = None)[source]
General-purpose expression with combined feature support.
This class mixes in comparison, indexing, object access, logical, and arithmetic behaviors by inheriting from the corresponding feature classes.
Features included:
Example:
>>> expr_obj = GeneralExpression._expr(lambda x: x + 1) >>> # The resulting expression can be used with all supported operators.
expr
- hbutils.expression.native.general.expr(v: Any, cls: Type[Expression] | None = None) Expression[source]
Transform any value into an
Expressioninstance.This function converts values into expressions using the following rules:
If
vis already anExpression, it is returned unchanged.If
vis callable, it is wrapped as an expression.Otherwise, a constant expression that returns
vis created.
- Parameters:
v (Any) – Value to be converted into an expression.
cls (Optional[Type[Expression]]) – Expression class to construct, defaults to
Nonewhich selectsGeneralExpression.
- Returns:
The generated expression object.
- Return type:
Example:
>>> # Create expression from callable >>> e1 = expr(lambda x: x * 2) >>> # Create expression from constant >>> e2 = expr(42) >>> # Pass through existing expression >>> e3 = expr(e1)
keep
- hbutils.expression.native.general.keep(cls: Type[Expression] | None = None) Expression[source]
Create an identity expression that returns its input unchanged.
This is an alias for
expr(lambda x: x, cls).- Parameters:
cls (Optional[Type[Expression]]) – Expression class to construct, defaults to
Nonewhich selectsGeneralExpression.- Returns:
Identity expression.
- Return type:
Example:
>>> identity = keep() >>> # The expression will return input unchanged
raw
- hbutils.expression.native.general.raw(v: Any, cls: Type[Expression] | None = None) Expression[source]
Create a constant expression that always returns the same value.
This is an alias for
expr(lambda x: v, cls)and ignores its input.- Parameters:
v (Any) – Constant value returned by the expression.
cls (Optional[Type[Expression]]) – Expression class to construct, defaults to
Nonewhich selectsGeneralExpression.
- Returns:
Constant expression.
- Return type:
Example:
>>> constant = raw(42) >>> # The expression will always return 42, regardless of input