potc.fixture.common¶
UnprocessableError¶
unprocessable¶
rule¶
-
potc.fixture.common.
rule
(alias: Optional[str] = None, type_: Optional[Union[Type, Tuple[Type, …]]] = None)[source]¶ - Overview:
Make a common function to a rule function.
- Arguments:
alias (
Optional[str]
): Alias name of this rule, default isNone
which means just use the name of the wrapped function.type_ (
Union[Type, Tuple[Type, ...], None]
): Type of the data, can be a type or a tuple of types, default isNone
which means no type limit.
- Returns:
decorator: A function decorator of the raw rule function.
- Examples:
There are some example from builtin part.
builtin_int (A very easy case)
>>> @rule(type_=int) >>> def builtin_int(v: int): >>> return repr(v)
builtin_float (A little complex case)
>>> @rule(type_=float) >>> def builtin_float(v: float, addon: Addons): >>> if math.isinf(v): >>> return ('+' if v > 0 else '-') + str(addon.obj(math).inf) >>> elif math.isnan(v): >>> return addon.obj(math).nan >>> else: >>> if math.isclose(v, math.e): >>> return addon.obj(math).e >>> elif math.isclose(v, math.pi): >>> return addon.obj(math).pi >>> elif math.isclose(v, math.tau): >>> return addon.obj(math).tau >>> else: >>> return repr(v)