hbutils.expression.native.feature
Operator-oriented expression feature classes.
This module provides a set of expression subclasses that extend
hbutils.expression.native.base.Expression with operator overloading.
Each subclass offers a focused set of operators for building expression trees
that can later be evaluated through hbutils.expression.native.base.efunc().
The main public components are:
CheckExpression- Equality/inequality operators.ComparableExpression- Full comparison operators.IndexedExpression- Item access operator.ObjectExpression- Attribute access and call operators.LogicalExpression- Logical operations implemented via bitwise operators (&,|,~).MathExpression- Arithmetic operators.BitwiseExpression- Bitwise operators.
These classes allow building composable expression trees suitable for domain-specific languages, query builders, or deferred computation.
Example:
>>> from hbutils.expression.native.feature import MathExpression
>>> from hbutils.expression.native.base import efunc
>>> expr = MathExpression()
>>> calc = efunc((expr + 1) * 2)
>>> calc(3)
8
Note
Operators return a new instance of the same class, preserving the expression type during chaining.
__all__
- hbutils.expression.native.feature.__all__ = ['CheckExpression', 'ComparableExpression', 'IndexedExpression', 'ObjectExpression', 'LogicalExpression', 'MathExpression', 'BitwiseExpression']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
CheckExpression
- class hbutils.expression.native.feature.CheckExpression(func: Callable | None = None)[source]
Expression supporting basic equality checks.
This class provides only the
==and!=operators for building comparison expressions.Example:
>>> from hbutils.expression.native.feature import CheckExpression >>> from hbutils.expression.native.base import efunc >>> expr = CheckExpression() >>> is_five = efunc(expr == 5) >>> is_five(5) True
- __eq__(other: Any) Expression[source]
Equality comparison operator.
- Parameters:
other (Any) – The value to compare with.
- Returns:
A new expression representing the equality comparison.
- Return type:
Example:
>>> expr = CheckExpression() >>> result = expr == 5
- __ne__(other: Any) Expression[source]
Inequality comparison operator.
- Parameters:
other (Any) – The value to compare with.
- Returns:
A new expression representing the inequality comparison.
- Return type:
Example:
>>> expr = CheckExpression() >>> result = expr != 5
ComparableExpression
- class hbutils.expression.native.feature.ComparableExpression(func: Callable | None = None)[source]
Expression supporting full comparison operations.
This class extends
CheckExpressionby adding<=,<,>=, and>comparisons.Example:
>>> from hbutils.expression.native.feature import ComparableExpression >>> from hbutils.expression.native.base import efunc >>> expr = ComparableExpression() >>> is_between = efunc((expr >= 1) & (expr < 3)) >>> is_between(2) True
- __ge__(other: Any) Expression[source]
Greater than or equal to comparison operator.
- Parameters:
other (Any) – The value to compare with.
- Returns:
A new expression representing the greater than or equal comparison.
- Return type:
Example:
>>> expr = ComparableExpression() >>> result = expr >= 5
- __gt__(other: Any) Expression[source]
Greater than comparison operator.
- Parameters:
other (Any) – The value to compare with.
- Returns:
A new expression representing the greater than comparison.
- Return type:
Example:
>>> expr = ComparableExpression() >>> result = expr > 5
- __le__(other: Any) Expression[source]
Less than or equal to comparison operator.
- Parameters:
other (Any) – The value to compare with.
- Returns:
A new expression representing the less than or equal comparison.
- Return type:
Example:
>>> expr = ComparableExpression() >>> result = expr <= 10
- __lt__(other: Any) Expression[source]
Less than comparison operator.
- Parameters:
other (Any) – The value to compare with.
- Returns:
A new expression representing the less than comparison.
- Return type:
Example:
>>> expr = ComparableExpression() >>> result = expr < 10
IndexedExpression
- class hbutils.expression.native.feature.IndexedExpression(func: Callable | None = None)[source]
Expression supporting indexed access.
This class enables
expr[key]syntax, which produces a new expression that indexes into the evaluation target.Example:
>>> from hbutils.expression.native.feature import IndexedExpression >>> from hbutils.expression.native.base import efunc >>> expr = IndexedExpression() >>> getter = efunc(expr["key"]) >>> getter({"key": "value"}) 'value'
- __getitem__(item: Any) Expression[source]
Item access operator.
- Parameters:
item (Any) – The index or key to access.
- Returns:
A new expression representing the item access.
- Return type:
Example:
>>> expr = IndexedExpression() >>> result = expr[0] >>> result = expr['key']
ObjectExpression
- class hbutils.expression.native.feature.ObjectExpression(func: Callable | None = None)[source]
Expression supporting attribute access and callable behavior.
This class supports:
Attribute access via
expr.attr.Calling via
expr(*args, **kwargs).
Example:
>>> from hbutils.expression.native.feature import ObjectExpression >>> from hbutils.expression.native.base import efunc >>> expr = ObjectExpression() >>> attr_getter = efunc(expr.real) >>> attr_getter(3+4j) 3.0
- __call__(*args: Any, **kwargs: Any) Expression[source]
Call operator for making the expression callable.
- Parameters:
args (Any) – Positional arguments to pass to the call.
kwargs (Any) – Keyword arguments to pass to the call.
- Returns:
A new expression representing the function call.
- Return type:
Example:
>>> expr = ObjectExpression() >>> result = expr(arg1, arg2, key=value)
- __getattr__(item: str) Expression[source]
Attribute access operator.
- Parameters:
item (str) – The attribute name to access.
- Returns:
A new expression representing the attribute access.
- Return type:
Example:
>>> expr = ObjectExpression() >>> result = expr.attribute_name
LogicalExpression
- class hbutils.expression.native.feature.LogicalExpression(func: Callable | None = None)[source]
Expression supporting logical operations.
The logical operations are implemented using bitwise operators:
&for logicaland.|for logicalor.~for logicalnot.
Note
Do not combine this class with
BitwiseExpressionin a single expression chain because both reuse bitwise operators.Example:
>>> from hbutils.expression.native.feature import LogicalExpression >>> from hbutils.expression.native.base import efunc >>> expr = LogicalExpression() >>> is_true = efunc(expr & True) >>> is_true(True) True
- __and__(other: Any) Expression[source]
Logical AND operator.
- Parameters:
other (Any) – The right operand for the AND operation.
- Returns:
A new expression representing the logical AND.
- Return type:
Example:
>>> expr1 = LogicalExpression() >>> expr2 = LogicalExpression() >>> result = expr1 & expr2
- __invert__() Expression[source]
Logical NOT operator.
- Returns:
A new expression representing the logical NOT.
- Return type:
Example:
>>> expr = LogicalExpression() >>> result = ~expr
- __or__(other: Any) Expression[source]
Logical OR operator.
- Parameters:
other (Any) – The right operand for the OR operation.
- Returns:
A new expression representing the logical OR.
- Return type:
Example:
>>> expr1 = LogicalExpression() >>> expr2 = LogicalExpression() >>> result = expr1 | expr2
- __rand__(other: Any) Expression[source]
Reverse logical AND operator.
- Parameters:
other (Any) – The left operand for the AND operation.
- Returns:
A new expression representing the logical AND.
- Return type:
Example:
>>> expr = LogicalExpression() >>> result = True & expr
- __ror__(other: Any) Expression[source]
Reverse logical OR operator.
- Parameters:
other (Any) – The left operand for the OR operation.
- Returns:
A new expression representing the logical OR.
- Return type:
Example:
>>> expr = LogicalExpression() >>> result = False | expr
MathExpression
- class hbutils.expression.native.feature.MathExpression(func: Callable | None = None)[source]
Expression supporting arithmetic operations.
This class provides the usual arithmetic operators, including unary operators for positive/negative.
Example:
>>> from hbutils.expression.native.feature import MathExpression >>> from hbutils.expression.native.base import efunc >>> expr = MathExpression() >>> f = efunc(-(expr * 2) + 1) >>> f(3) -5
- __add__(other: Any) Expression[source]
Addition operator.
- Parameters:
other (Any) – The value to add.
- Returns:
A new expression representing the addition.
- Return type:
Example:
>>> expr = MathExpression() >>> result = expr + 5
- __floordiv__(other: Any) Expression[source]
Floor division operator.
- Parameters:
other (Any) – The divisor.
- Returns:
A new expression representing the floor division.
- Return type:
Example:
>>> expr = MathExpression() >>> result = expr // 2
- __mod__(other: Any) Expression[source]
Modulo operator.
- Parameters:
other (Any) – The divisor for modulo operation.
- Returns:
A new expression representing the modulo.
- Return type:
Example:
>>> expr = MathExpression() >>> result = expr % 3
- __mul__(other: Any) Expression[source]
Multiplication operator.
- Parameters:
other (Any) – The value to multiply by.
- Returns:
A new expression representing the multiplication.
- Return type:
Example:
>>> expr = MathExpression() >>> result = expr * 3
- __neg__() Expression[source]
Unary negative operator.
- Returns:
A new expression representing the negative value.
- Return type:
Example:
>>> expr = MathExpression() >>> result = -expr
- __pos__() Expression[source]
Unary positive operator.
- Returns:
A new expression representing the positive value.
- Return type:
Example:
>>> expr = MathExpression() >>> result = +expr
- __pow__(power: Any) Expression[source]
Power operator.
- Parameters:
power (Any) – The exponent.
- Returns:
A new expression representing the power operation.
- Return type:
Example:
>>> expr = MathExpression() >>> result = expr ** 2
- __radd__(other: Any) Expression[source]
Reverse addition operator.
- Parameters:
other (Any) – The left operand for addition.
- Returns:
A new expression representing the addition.
- Return type:
Example:
>>> expr = MathExpression() >>> result = 5 + expr
- __rfloordiv__(other: Any) Expression[source]
Reverse floor division operator.
- Parameters:
other (Any) – The dividend.
- Returns:
A new expression representing the floor division.
- Return type:
Example:
>>> expr = MathExpression() >>> result = 10 // expr
- __rmod__(other: Any) Expression[source]
Reverse modulo operator.
- Parameters:
other (Any) – The dividend for modulo operation.
- Returns:
A new expression representing the modulo.
- Return type:
Example:
>>> expr = MathExpression() >>> result = 10 % expr
- __rmul__(other: Any) Expression[source]
Reverse multiplication operator.
- Parameters:
other (Any) – The left operand for multiplication.
- Returns:
A new expression representing the multiplication.
- Return type:
Example:
>>> expr = MathExpression() >>> result = 3 * expr
- __rpow__(other: Any) Expression[source]
Reverse power operator.
- Parameters:
other (Any) – The base for power operation.
- Returns:
A new expression representing the power operation.
- Return type:
Example:
>>> expr = MathExpression() >>> result = 2 ** expr
- __rsub__(other: Any) Expression[source]
Reverse subtraction operator.
- Parameters:
other (Any) – The left operand for subtraction.
- Returns:
A new expression representing the subtraction.
- Return type:
Example:
>>> expr = MathExpression() >>> result = 10 - expr
- __rtruediv__(other: Any) Expression[source]
Reverse true division operator.
- Parameters:
other (Any) – The dividend.
- Returns:
A new expression representing the division.
- Return type:
Example:
>>> expr = MathExpression() >>> result = 10 / expr
- __sub__(other: Any) Expression[source]
Subtraction operator.
- Parameters:
other (Any) – The value to subtract.
- Returns:
A new expression representing the subtraction.
- Return type:
Example:
>>> expr = MathExpression() >>> result = expr - 5
- __truediv__(other: Any) Expression[source]
True division operator.
- Parameters:
other (Any) – The divisor.
- Returns:
A new expression representing the division.
- Return type:
Example:
>>> expr = MathExpression() >>> result = expr / 2
BitwiseExpression
- class hbutils.expression.native.feature.BitwiseExpression(func: Callable | None = None)[source]
Expression supporting bitwise operations.
This class provides operators for bitwise logic and shifts.
Note
Do not combine this class with
LogicalExpressionin a single expression chain because both reuse bitwise operators.Example:
>>> from hbutils.expression.native.feature import BitwiseExpression >>> from hbutils.expression.native.base import efunc >>> expr = BitwiseExpression() >>> f = efunc(expr | 0b0011) >>> f(0b0100) 7
- __and__(other: Any) Expression[source]
Bitwise AND operator.
- Parameters:
other (Any) – The right operand for bitwise AND.
- Returns:
A new expression representing the bitwise AND.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = expr & 0b1010
- __invert__() Expression[source]
Bitwise NOT operator.
- Returns:
A new expression representing the bitwise NOT.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = ~expr
- __lshift__(other: Any) Expression[source]
Left shift operator.
- Parameters:
other (Any) – The number of positions to shift left.
- Returns:
A new expression representing the left shift.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = expr << 2
- __or__(other: Any) Expression[source]
Bitwise OR operator.
- Parameters:
other (Any) – The right operand for bitwise OR.
- Returns:
A new expression representing the bitwise OR.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = expr | 0b1010
- __rand__(other: Any) Expression[source]
Reverse bitwise AND operator.
- Parameters:
other (Any) – The left operand for bitwise AND.
- Returns:
A new expression representing the bitwise AND.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = 0b1010 & expr
- __rlshift__(other: Any) Expression[source]
Reverse left shift operator.
- Parameters:
other (Any) – The value to be shifted left.
- Returns:
A new expression representing the left shift.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = 5 << expr
- __ror__(other: Any) Expression[source]
Reverse bitwise OR operator.
- Parameters:
other (Any) – The left operand for bitwise OR.
- Returns:
A new expression representing the bitwise OR.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = 0b1010 | expr
- __rrshift__(other: Any) Expression[source]
Reverse right shift operator.
- Parameters:
other (Any) – The value to be shifted right.
- Returns:
A new expression representing the right shift.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = 20 >> expr
- __rshift__(other: Any) Expression[source]
Right shift operator.
- Parameters:
other (Any) – The number of positions to shift right.
- Returns:
A new expression representing the right shift.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = expr >> 2
- __rxor__(other: Any) Expression[source]
Reverse bitwise XOR operator.
- Parameters:
other (Any) – The left operand for bitwise XOR.
- Returns:
A new expression representing the bitwise XOR.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = 0b1010 ^ expr
- __xor__(other: Any) Expression[source]
Bitwise XOR operator.
- Parameters:
other (Any) – The right operand for bitwise XOR.
- Returns:
A new expression representing the bitwise XOR.
- Return type:
Example:
>>> expr = BitwiseExpression() >>> result = expr ^ 0b1010