built-in coercers¶
The following are built in validators in records
-
class
CoercionToken¶ A base class for all object that should be interpreted as coercers
-
class
CallCoercion(CoercionToken)¶ A coercion token to call arbitrary functions
-
__init__(func: Callable[[…], T], *args, **kwargs)¶ - Parameters
func – The callable to use as the coercion callback.
args – Optional positional arguments to pass to
func, after the validation argument.kwargs – Optional keyword arguments to pass to
func.
Note
calling
CallCoercionwithargsorkwargsis akin to calling it with afunctools.partial()asfunc.>>> CallCoercion(foo, a, b, c=d) >>> # is equivalent to >>> CallCoercion(lambda v: foo(v, a, b, c=d))
-
-
class
MapCoercion(CoercionToken)¶ A coercion token to map values by arbitrary, pre-defined mappings
-
__init__(value_map: Optional[Mapping[Any, T]] = None, factory_map: Optional[Mapping[Any, Callable[], T]]] = None)¶ - Parameters
value_map – a mapping to map values to coerced values
factory_map – a mapping to map values to factory callbacks that create coerced values
-
-
class
ClassMethodCoercion(CoercionToken)¶ A coercion token to call a class method in the target class
-
class
ComposeCoercer(CoercionToken)¶ A coercion token to chain two coercion callbacks one after the other
-
__init__(*inner_coercers: Union[Type[records.fillers.coercers.CoercionToken], records.fillers.coercers.CoercionToken])¶ - Parameters
inner_coercers – an iterable of coercion tokens to apply upon the argument, in reverse order.
Note
the inner coercion callbacks are called in reverse order. So if a token
Awill result in callbacka, and tokenBwill result in callbackb, then the tokenComposeCoercer(A,B)will result in callbacklambda v: a(b(v)).
-
-
class
Eval(CoercionToken)¶ A coercion token to evaluate a string input as a python expression using
eval()Warning
evaluating arbitrary strings is always risky!
-
class
LiteralEval(CoercionToken)¶ A coercion token to evaluate string inputs with
literal_eval()
-
class
Loose(CoercionToken)¶ A coercion token to call the class constructor with the input as an argument
-
__init__(*args, **kwargs)¶ - Parameters
args – arguments forwarded to the callback
kwargs – keywords forwarded to the callback
-
-
class
LooseUnpack(CoercionToken)¶ A coercion token to call the class constructor with the input as an unpacked iterable
-
__init__(*args, **kwargs)¶ - Parameters
args – arguments forwarded to the callback
kwargs – keywords forwarded to the callback
-
classmethod
constrain(*items: Union[type, Tuple[type, …], ellipsis])¶ Can be used to constrain LooseUnpack to only accept specific inputs of specific types.
- Parameters
items – the type or types to constrain inputs by. If there are exactly two values, and the second is
Ellipsis, the coercion will accept any number of arguments of type items[0].- Returns
a factory function to a constrained LooseUnpack token.
-
-
class
LooseUnpackMap(CoercionToken)¶ A coercion token to call the class constructor with the input as an unpacked mapping
-
__init__(*args, **kwargs)¶ - Parameters
args – arguments forwarded to the callback
kwargs – keywords forwarded to the callback
-
-
class
Whole(CoercionToken)¶ A coercion token to attempt to convert a whole Number to an Integer type
-
__init__(*args, **kwargs)¶ - Parameters
args – arguments forwarded to the callback
kwargs – keywords forwarded to the callback
-
-
class
ToBytes(CoercionToken)¶ A coercion token to convert a n Integer value to a bytestring
-
__init__(*args, **kwargs)¶ - Parameters
args – arguments forwarded to the callback
kwargs – keywords forwarded to the callback
-
-
class
FromInteger(CoercionToken)¶ A coercion token to convert an integer to a boolean, failing if the value is not 0 or 1.
-
class
Falsish(CoercionToken)¶ - A coercion token to construct an instance of the target type ignoring the input, only if the argument is falsish.
Useful to create empty objects from None inputs.
-
__init__(*args, **kwargs)¶ - Parameters
args – arguments forwarded to the callback
kwargs – keywords forwarded to the callback