Package konval :: Module fxn
[hide private]
[frames] | no frames]

Module fxn

source code

Various functions for using validators.
Functions [hide private]
 
sanitize(val, validators)
Check and/or convert a value, throwing an exception on failure.
source code
 
select(vals, validators)
Return those values that pass validation.
source code
 
reject(vals, validators)
Return those values that fail validation.
source code
 
assert_sanity(val, validators)
Use validators for assertion.
source code
Variables [hide private]
  __package__ = 'konval'
Function Details [hide private]

sanitize(val, validators)

source code 

Check and/or convert a value, throwing an exception on failure.

The core service method for konval, this can be used to check and convert data. Note that this accepts a single value - if you want to sanitize a whole list in the same way, use a list comprehension.

For example:

>>> sanitize (1, int)
1
>>> from konval import IsEqualOrMore, ToLength
>>> sanitize ('2', [float, IsEqualOrMore(1)])
2.0
>>> x = sanitize (['a', 'b'], [ToLength(), float, IsEqualOrMore(1)])
>>> x
2.0
>>> sanitize (['a', 'b'], [ToLength(), float, IsEqualOrMore(3)])
Traceback (most recent call last):
...
ValueError: 2.0 is lower than 3
Parameters:
  • val - the original value to be validated and/or converted
  • validators - a validator or sequence of validators or suitable objects
Returns:
the converted value, or original one if only validation has occurred

select(vals, validators)

source code 

Return those values that pass validation.

Note that the converted values are returned.

reject(vals, validators)

source code 

Return those values that fail validation.

Note that non-converted values are returned.

assert_sanity(val, validators)

source code 

Use validators for assertion.

This actually works much like sanitize other than converting errors to AssertionErrors and serving as a signal of intent in code. Note that this accepts a single value - If you want to sanitize a whole list in the same way, use a list comprehension.

For example:

>>> assert_sanity (1, int)
1
>>> from konval import IsEqualOrMore, ToLength
>>> x = assert_sanity ('2', [float, IsEqualOrMore(1)])
>>> x
2.0
>>> assert_sanity (['a', 'b'], [ToLength(), float, IsEqualOrMore(3)])
Traceback (most recent call last):
...
AssertionError: 2.0 is lower than 3