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 anExpression
, its callable method will be returned. If givene
is a function, an equivalent method will be returned. Otherwise, a method which always returne
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 anExpression
,v
will be returned.If the given
v
is a callable object, expression withv
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 isNone
which meansGeneralExpression
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 isNone
which meansGeneralExpression
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 isNone
which meansGeneralExpression
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 alambda 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¶
ComparableExpression¶
-
class
hbutils.expression.native.
ComparableExpression
(func=None)[source]¶ - Overview:
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=None)[source]¶ - Overview:
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=None)[source]¶ - Overview:
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=None)[source]¶ - Overview:
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=None)[source]¶ - Overview:
General expression.
- Features:
Inherited from
ComparableExpression
,IndexedExpression
,ObjectExpression
,LogicalExpression
andMathExpression
.