Output Decorators

Function decorators to decorate output values or error raises.

Multiple decorators can be used together. When multiple decorators are used, the order of decorators is from inside to outside.

extepy.fillerr(value=None)[source]

Decorator that returns a default value when an error occurs.

Parameters:

value (optional) – Default value to return when an error occurs.

Return type:

callable

Examples

>>> @fillerr()
... def f(x):
...     return 1.0 / x
>>> f(0.0)  # Return None, print nothing.
>>> f(1.0)
1.0
extepy.fillnone(value=nan)[source]

Decorator that returns a default value if the result is None.

Parameters:

value – Value to return if the result is None.

Return type:

callable

Examples

>>> @fillnone(-1)
... def f(x):
...     return x
>>> f(None)
-1
>>> f(float('nan'))
nan
>>> f(False)
False
>>> f(0)
0
extepy.fillwhen(check, value)[source]

Decorator that returns a default value if a condition is met.

Parameters:
  • check (callable) – Condition checker.

  • value – Value to return when the condition is met.

Return type:

callable

Examples

>>> from math import isnan
>>> @fillwhen(isnan, 0)
... def f(x):
...     return x
>>> f(float('nan'))
0
>>> f(1)
1