hbutils.expression.native

efunc

hbutils.expression.native.efunc(e) → Callable[source]
Overview:

Get callable object from any types.

Parameters:

e – Original object.

Returns:

Callable object. If given e is an Expression, its callable method will be returned. If given e is a function, an equivalent method will be returned. Otherwise, a method which always return e will be returned.

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: Optional[Type[hbutils.expression.native.base.Expression]] = None)[source]
Overview:

Transform any objects to Expression.

  • If the given v is an Expression, v will be returned.

  • If the given v is a callable object, expression with v will be returned.

  • Otherwise, expression with lambda x: v will be returned.

Parameters:
  • v – Original value.

  • cls – Class of expression, should be a subclass of Expression. Default is None which means GeneralExpression will be used.

Returns:

Generated expression.

keep

hbutils.expression.native.keep(cls: Optional[Type[hbutils.expression.native.base.Expression]] = None)[source]
Overview:

Alias for expr(lambda x: x, cls).

Parameters:

cls – Class of expression, should be a subclass of Expression. Default is None which means GeneralExpression will be used.

Returns:

Generated expression.

raw

hbutils.expression.native.raw(v, cls: Optional[Type[hbutils.expression.native.base.Expression]] = None)[source]
Overview:

Alias for expr(lambda x: v, cls).

Parameters:
  • v – Raw value.

  • cls – Class of expression, should be a subclass of Expression. Default is None which means GeneralExpression will be used.

Returns:

Generated expression.

Expression

class hbutils.expression.native.Expression(func=None)[source]
Overview:

Base class of expressions.

__init__(func=None)[source]

Constructor of Expression.

Parameters:

func – Callable function, default is None which means a lambda x: x will be used.

classmethod _expr(v)[source]

Build expression with this class.

Parameters:

v – Any types of value.

Returns:

An expression object.

_func(func: Callable, *args, **kwargs)[source]

Expression building based on given func and arguments.

Parameters:
  • func – Logical function.

  • args – Positional arguments.

  • kwargs – Key-word arguments.

Returns:

New expression with current class.

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

class hbutils.expression.native.CheckExpression(func=None)[source]
Overview:

Check expression.

Features:
  • __eq__, which means x == y.

  • __ne__, which means x == y.

ComparableExpression

class hbutils.expression.native.ComparableExpression(func=None)[source]
Overview:

Comparable expression.

Features:
  • __eq__, which means x == y (the same as CheckExpression).

  • __ne__, which means x == y (the same as CheckExpression).

  • __ge__, which means x >= y.

  • __gt__, which means x > y.

  • __le__, which means x <= y.

  • __lt__, which means x < y.

IndexedExpression

class hbutils.expression.native.IndexedExpression(func=None)[source]
Overview:

Indexed expression.

Features:
  • __getitem__, which means x[y].

ObjectExpression

class hbutils.expression.native.ObjectExpression(func=None)[source]
Overview:

Object-like expression.

Features:
  • __getattr__, which means x.y.

  • __call__, which means x(*args, **kwargs).

LogicalExpression

class hbutils.expression.native.LogicalExpression(func=None)[source]
Overview:

Logic expression.

Features:
  • __and__, which means x and y (written as x & y).

  • __or__, which means x or y (written as x | y).

  • __invert__, which means not x (written as ~x).

Note

Do not use this with BitwiseExpression, or unexpected conflict will be caused.

MathExpression

class hbutils.expression.native.MathExpression(func=None)[source]
Overview:

Math calculation expression.

Features:
  • __add__, which means x + y.

  • __sub__, which means x - y.

  • __mul__, which means x * y.

  • __truediv__, which means x / y.

  • __floordiv__, which means x // y.

  • __mod__, which means x % y.

  • __pow__, which means x ** y.

  • __pos__, which means +x.

  • __neg__, which means -x.

BitwiseExpression

class hbutils.expression.native.BitwiseExpression(func=None)[source]
Overview:

Bitwise expression class.

Features:
  • __and__, which means x & y (bitwise).

  • __or__, which means x | y (bitwise).

  • __xor__, which means x ^ y (bitwise).

  • __lshift__, which means x << y (bitwise).

  • __rshift__, which means x >> 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=None)[source]
Overview:

General expression.

Features:

Inherited from ComparableExpression, IndexedExpression, ObjectExpression, LogicalExpression and MathExpression.