argsloader.units.func¶
ProcUnit¶
proc¶
-
argsloader.units.func.
proc
(f) → argsloader.units.func.ProcUnit[source]¶ - Overview:
Wrap a processor function to unit.
- Parameters
f – Processor function.
- Returns
A processor unit.
- Examples::
Use defined function
>>> from argsloader.units import proc >>> u = proc(lambda x: x ** 2) >>> u(2) 4 >>> u(10) 100
Use native function
>>> u = proc(str.upper) >>> u('word') 'WORD' >>> u('this is message') 'THIS IS MESSAGE'
Native support in unit calculation
>>> from argsloader.units import add >>> u = add.by(2) >> (lambda x: x ** 2) >>> u(10) 144 >>> u(20) 484
Warning
Only simple processor function is supported, which is like
lambda x: None
.Please make sure no error will be raised in the processor function.
ufunc¶
-
argsloader.units.func.
ufunc
(errors=())[source]¶ - Overview:
Wrap a function to unit supported function.
- Parameters
errors – Errors need to be processed.
- Returns
A wrapped function unit.
- Examples::
>>> import math >>> from argsloader.units import ufunc, add, sub, keep >>> @ufunc(errors=(ValueError,)) # this is necessary, or this error will not be recorded ... def myfunc(a, b, c): ... p = (a + b + c) / 2 ... try: ... return math.sqrt(p * (p - a) * (p - b) * (p - c)) ... except ValueError: ... raise ValueError(f'Invalid triangle - ({a}, {b}, {c}).') ... >>> u = myfunc(add.by(2), sub.by(2), keep()) >>> u(10) # area of triangle (12, 8, 10) 39.68626966596886 >>> u(3) # area of triangle (5, 1, 3) ValueParseError: Invalid triangle - (5, 1, 3).