hbutils.expression.native
This module provides native expression utilities for the hbutils library.
It aggregates and exports functionalities from three main submodules:
base: Base expression classes and utilities
feature: Feature-related expression components
general: General-purpose expression utilities
This module serves as a convenient entry point to access all native expression functionalities without needing to import from individual submodules.
- Example::
>>> from hbutils.expression.native import * >>> # Now you can use all exported classes and functions from base, feature, and general
efunc
- hbutils.expression.native.efunc(e: Any) Callable[source]
Get callable object from any types.
- Parameters:
e (Any) – Original object.
- Returns:
Callable object. If given
eis anExpression, its callable method will be returned. If giveneis a function, an equivalent method will be returned. Otherwise, a method which always returnewill be returned.- Return type:
Callable
Note
This is the key feature of the native expressions, you need to use
efunc()function to transform expressions to callable functions.- Examples::
>>> from hbutils.expression import keep, efunc, expr >>> >>> e1 = keep() >>> efunc(e1 == 1)(1) True >>> efunc(e1 == 1)(2) False >>> >>> e2 = expr(lambda x: x + 2) >>> efunc(e2 == 1)(-1) True >>> efunc(e2 == 1)(1) False
expr
- hbutils.expression.native.expr(v, cls: Type[Expression] | None = None)[source]
Transform any objects to
Expression.This function intelligently converts different types of values into expressions: - If the given
vis already anExpression, it returnsvunchanged. - If the givenvis a callable object, it creates an expression withvas the transformation function. - Otherwise, it creates an expression withlambda x: vas the transformation function (constant expression).- Parameters:
v (Any) – Original value to be converted into an expression. Can be an Expression, callable, or any other value.
cls (Optional[Type[Expression]]) – Class of expression, should be a subclass of
Expression. Default isNonewhich meansGeneralExpressionwill be used.
- Returns:
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.keep(cls: Type[Expression] | None = None)[source]
Create an identity expression that returns the input unchanged.
This is an alias for
expr(lambda x: x, cls), which creates an expression that simply passes through its input without any transformation.- Parameters:
cls (Optional[Type[Expression]]) – Class of expression, should be a subclass of
Expression. Default isNonewhich meansGeneralExpressionwill be used.- Returns:
Generated identity expression.
- Return type:
- Example::
>>> identity = keep() >>> # The expression will return input unchanged
raw
- hbutils.expression.native.raw(v, cls: Type[Expression] | None = None)[source]
Create a constant expression that always returns the same value.
This is an alias for
expr(lambda x: v, cls), which creates an expression that ignores its input and always returns the constant valuev.- Parameters:
v (Any) – Raw value to be returned by the expression, regardless of input.
cls (Optional[Type[Expression]]) – Class of expression, should be a subclass of
Expression. Default isNonewhich meansGeneralExpressionwill be used.
- Returns:
Generated constant expression.
- Return type:
- Example::
>>> constant = raw(42) >>> # The expression will always return 42, regardless of input
Expression
- class hbutils.expression.native.Expression(func: Callable | None = None)[source]
Base class of expressions.
This class provides the foundation for building composable expressions. It wraps a callable function and provides methods to combine expressions into more complex ones.
The Expression class can be subclassed to create custom expression types with specialized operators and methods.
- __init__(func: Callable | None = None)[source]
Constructor of
Expression.- Parameters:
func (Optional[Callable]) – Callable function, default is
Nonewhich means alambda x: xwill be used.
- classmethod _expr(v: Any) Expression[source]
Build expression with this class.
This class method converts any value into an Expression object: - If v is already an Expression, return it as-is - If v is callable, wrap it in an Expression - Otherwise, create an Expression that returns the constant value v
- Parameters:
v (Any) – Any types of value.
- Returns:
An expression object.
- Return type:
- _func(func: Callable, *args, **kwargs) Expression[source]
Expression building based on given
funcand arguments.This method creates a new expression by composing the given function with the provided arguments. Each argument is converted to a callable using efunc, allowing for nested expression composition.
- Parameters:
func (Callable) – Logical function to apply to the evaluated arguments.
args (Any) – Positional arguments, can be expressions, callables, or constants.
kwargs (Any) – Key-word arguments, can be expressions, callables, or constants.
- Returns:
New expression with current class.
- Return type:
- Examples::
>>> from hbutils.expression import efunc, Expression >>> >>> class MyExpression(Expression): ... def add(self, other): ... return self._func(lambda x, y: x + y, self, other) ... >>> >>> e1 = MyExpression() >>> efunc(e1.add(1))(5) # 5 + 1 = 6 6 >>> efunc(e1.add(e1.add(1)))(5) # 5 + (5 + 1) = 11 11
CheckExpression
ComparableExpression
- class hbutils.expression.native.ComparableExpression(func: Callable | None = None)[source]
Comparable expression.
- Features:
__eq__, which meansx == y(the same asCheckExpression).__ne__, which meansx != y(the same asCheckExpression).__ge__, which meansx >= y.__gt__, which meansx > y.__le__, which meansx <= y.__lt__, which meansx < y.
IndexedExpression
ObjectExpression
LogicalExpression
- class hbutils.expression.native.LogicalExpression(func: Callable | None = None)[source]
Logic expression.
- Features:
__and__, which meansx and y(written asx & y).__or__, which meansx or y(written asx | y).__invert__, which meansnot x(written as~x).
Note
Do not use this with
BitwiseExpression, or unexpected conflict will be caused.
MathExpression
- class hbutils.expression.native.MathExpression(func: Callable | None = None)[source]
Math calculation expression.
- Features:
__add__, which meansx + y.__sub__, which meansx - y.__mul__, which meansx * y.__truediv__, which meansx / y.__floordiv__, which meansx // y.__mod__, which meansx % y.__pow__, which meansx ** y.__pos__, which means+x.__neg__, which means-x.
BitwiseExpression
- class hbutils.expression.native.BitwiseExpression(func: Callable | None = None)[source]
Bitwise expression class.
- Features:
__and__, which meansx & y(bitwise).__or__, which meansx | y(bitwise).__xor__, which meansx ^ y(bitwise).__lshift__, which meansx << y(bitwise).__rshift__, which meansx >> y(bitwise).__invert__, which means~x(bitwise).
Note
Do not use this with
LogicalExpression, or unexpected conflict will be caused.
GeneralExpression
- class hbutils.expression.native.GeneralExpression(func: Callable | None = None)[source]
A general-purpose expression class that combines multiple expression features.
This class inherits from multiple expression base classes to provide a comprehensive set of operations including comparisons, indexing, object operations, logical operations, and mathematical operations.
- Features:
Inherited from
ComparableExpression,IndexedExpression,ObjectExpression,LogicalExpressionandMathExpression.- Example::
>>> expr_obj = GeneralExpression._expr(lambda x: x + 1) >>> # Use the expression object for various operations