mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-19 11:22:06 +08:00
DOC/TEST: Add doctest and docs for coerce kwargs.
This commit is contained in:
@@ -289,6 +289,15 @@ def coerce(from_, to, **to_kwargs):
|
||||
A preprocessing decorator that coerces inputs of a given type by passing
|
||||
them to a callable.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
from : type or tuple or types
|
||||
Inputs types on which to call ``to``.
|
||||
to : function
|
||||
Coercion function to call on inputs.
|
||||
**to_kwargs
|
||||
Additional keywords to forward to every call to ``to``.
|
||||
|
||||
Usage
|
||||
-----
|
||||
>>> @preprocess(x=coerce(float, int), y=coerce(float, int))
|
||||
@@ -297,6 +306,13 @@ def coerce(from_, to, **to_kwargs):
|
||||
...
|
||||
>>> floordiff(3.2, 2.5)
|
||||
1
|
||||
|
||||
>>> @preprocess(x=coerce(str, int, base=2), y=coerce(str, int, base=2))
|
||||
... def add_binary_strings(x, y):
|
||||
... return bin(x + y)[2:]
|
||||
...
|
||||
>>> add_binary_strings('101', '001')
|
||||
'110'
|
||||
"""
|
||||
def preprocessor(func, argname, arg):
|
||||
if isinstance(arg, from_):
|
||||
|
||||
Reference in New Issue
Block a user