potc.fixture.common

UnprocessableError

class potc.fixture.common.UnprocessableError[source]
Overview:

Signal exception which means the rules before cannot process this data.

unprocessable

potc.fixture.common.unprocessable()[source]
Overview:

Raise an UnprocessableError.

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 is None 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 is None 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)

is_rule

potc.fixture.common.is_rule(func: Callable) → bool[source]
Overview:

Check if a function is a potc rule.

Arguments:
  • func (Callable): Function object.

Returns:
  • is_rule (bool): Is a rule or not.

Examples:
>>> is_rule(builtin_int)            # True
>>> is_rule(lambda x, addon: None)  # False

rule_name

potc.fixture.common.rule_name(rule_: Callable) → str[source]
Overview:

Name of the rule.

Arguments:
  • rule_ (Callable): Rule object.

Returns:
  • name (str): Name of the rule.

Examples:
>>> rule_name(builtin_int)  # 'builtin_int'

rule_origin

potc.fixture.common.rule_origin(rule_: Callable) → Callable[source]
Overview:

Original function of the rule.

Arguments:
  • rule_ (Callable): Rule object.

Returns:
  • func (str): Original function.