hbutils.expression.native.base

Native expression base utilities for building composable callable expressions.

This module provides the core building blocks for the native expression system. It defines a lightweight Expression base class for composing callables and a helper function efunc() for converting arbitrary values into callable objects. These utilities allow you to combine constants, callables, and expression objects into reusable, composable expressions.

The module contains the following main components:

  • efunc() - Convert any object into a callable function

  • Expression - Base class for building custom expression types

Note

Only public interfaces are listed above. Internal helpers are used for caching and value conversion, but they are not part of the public API.

Example:

>>> from hbutils.expression.native.base import Expression, efunc
>>>
>>> class MyExpression(Expression):
...     def add(self, other):
...         return self._func(lambda x, y: x + y, self, other)
...
>>> e1 = MyExpression()
>>> efunc(e1.add(1))(5)
6

__all__

hbutils.expression.native.base.__all__ = ['efunc', 'Expression']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

Expression

class hbutils.expression.native.base.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.

Parameters:

func (Optional[Callable]) – Callable function used by the expression. If None, the identity function lambda x: x is used.

Variables:

_fcall (Callable) – Internal callable associated with this expression.

__init__(func: Callable | None = None)[source]

Initialize an Expression instance.

Parameters:

func (Optional[Callable]) – Callable function, defaults to None which means the identity function lambda x: x is used.

efunc

hbutils.expression.native.base.efunc(e: Any) Callable[source]

Get a callable object from any type.

This function is the primary entry point for converting arbitrary values into callables:

  • If e is an Expression, its internal callable is returned.

  • If e is a callable, a wrapped expression callable is returned.

  • Otherwise, a callable that always returns e is returned.

Parameters:

e (Any) – Original object.

Returns:

Callable object derived from e.

Return type:

Callable

Note

This is the key feature of native expressions. Use efunc() to transform expressions into callable functions.

Examples:

>>> from hbutils.expression.native.base import Expression, efunc
>>>
>>> class MyExpression(Expression):
...     def add(self, other):
...         return self._func(lambda x, y: x + y, self, other)
...
>>> e1 = MyExpression()
>>> efunc(e1.add(1))(5)
6