xotl.tools.validators – value validators

Some generic value validators and regular expressions and validation functions for several identifiers.

xotl.tools.validators.check(value, validator, msg=None)[source]

Check a value with a validator.

Argument validator could be a callable, a type, or a tuple of types.

Return True if the value is valid.

Examples:

>>> check(1, int)
True

>>> check(10, lambda x: x <= 100, 'must be less than or equal to 100')
True

>>> check(11/2, (int, float))
True
xotl.tools.validators.check_no_extra_kwargs(kwargs)[source]

Check that no extra keyword arguments are still not processed.

For example:

>>> from xotl.tools.validators import check_no_extra_kwargs
>>> def only_safe_arg(**kwargs):
...     safe = kwargs.pop('safe', False)
...     check_no_extra_kwargs(kwargs)
...     print('OK for safe:', safe)
xotl.tools.validators.is_type(cls)[source]

Return a validator with the same name as the type given as argument value.

Parameters:cls – Class or type or tuple of several types.
xotl.tools.validators.ok(value, *checkers, **kwargs)[source]

Validate a value with several checkers.

Return the value if it is Ok, or raises an ValueError exception if not.

Arguments:

Parameters:
  • value – the value to validate
  • checkers – a variable number of checkers (at least one), each one could be a type, a tuple of types of a callable that receives the value and returns if the value is valid or not. In order the value is considered valid, all checkers must validate the value.
  • message – keyword argument to be used in case of error; will be the argument of ValueError exception; could contain the placeholders {value} and {type}; a default value is used if this argument is not given.
  • msg – an alias for “message”
  • extra_checkers – In order to create validators using partial. Must be a tuple.

Keyword arguments are not validated to be correct.

This function could be used with type-definitions for arguments, see xotl.tools.fp.prove.semantic.TypeCheck.

Examples:

>>> ok(1, int)
1

>>> ok(10, int, lambda x: x < 100, message='Must be integer under 100')
10

>>> ok(11/2, (int, float))
5.5

>>> ok(11/2, int, float)
5.5

>>> try:
...     res = ok(11/2, int)
... except ValueError:
...     res = '---'
>>> res
'---'
xotl.tools.validators.predicate(*checkers, **kwargs)[source]

Return a validation checker for types and simple conditions.

Parameters:
  • checkers

    A variable number of checkers; each one could be:

    • A type, or tuple of types, to test valid values with isinstance(value, checker)
    • A set or mapping of valid values, the value is valid if contained in the checker.
    • A tuple of other inner checkers, if any of the checkers validates a value, the value is valid (OR).
    • A list of other inner checkers, all checkers must validate the value (AND).
    • A callable that receives the value and returns True if the value is valid.
    • True and False could be used as checkers always validating or invalidating the value.

    An empty list or no checker is synonym of True, an empty tuple, set or mapping is synonym of False.

  • name – Keyword argument to be used in case of error; will be the argument of ValueError exception; could contain the placeholders {value} and {type}; a default value is used if this argument is not given.
  • force_name – Keyword argument to force a name if not given.

In order to obtain good documentations, use proper names for functions and lambda arguments.

With this function could be built real type checkers, for example:

>>> is_valid_age = predicate((int, float), lambda age: 0 < age <= 120)
>>> is_valid_age(100)
True

>>> is_valid_age(130)
False

>>> always_true = predicate(True)
>>> always_true(False)
True

>>> always_false = predicate(False)
>>> always_false(True)
False

>>> always_true = predicate()
>>> always_true(1)
True

>>> always_true('any string')
True

>>> always_false = predicate(())
>>> always_false(1)
False

>>> always_false('any string')
False

Contents: