Package: konval
BackgroundThe problem of sanitizing data (checking correctness and transforming to a
useful form) is widespread throughout programming:How do I verify user input is correct?How do I munge data from a spreadsheet into dates and numbers?How do I convert raw database fields into a programmatic object?Ian Bicking came up with a sensible idiom for this problem, embodied in his
Formencode library[formencode]: validation and conversion are one and the same
thing, and can be handled by passing raw data through a chain of validators.
Each validator checks and/or transforms the data and passes it on to the next.In this spirit,konvalis a package that provides:a rich library of validation objectsbase classes for easily producing custom validatorsfunctions for easily using validators in a variety of waysStatuskonvalis in an exploratory state, having been produced to support another
package and to see if use can be got out of generalising conversion. As such. it
is still an early release and the API may change. Comment is invited.InstallationThe simplest way to installkonvalis viaeasy_install[setuptools]or an
equivalent program:% easy_install konvalAlternatively the tarball can be downloaded[konval-pypi], unpacked andsetup.pyrun:% tar zxvf konval.tgz
% cd konval
% python set.py installkonvalhas no prerequisites and should work with just about any version of
Python.Using konvalA full API is included in the source distribution.ExamplesMost commonly, konval will be used to check or clean values. Failures result in
exceptions being thrown:# convert user input to a actual integer
>>> from konval import *
>>> sanitize ('1.0', ToInt())
1
>>> sanitize ('one', ToInt())
Traceback (most recent call last)
...
ValueError: can't convert 'one' to integerA single validator or list can be passed tosanitize. Failure in any will
result in any exception:# check a list has no more than 3 members
>>> sanitize (['a', 'b', 'c'], [ToLength(), IsEqualOrLess(3)])
3
# check a password is long enough
>>> sanitize ('mypass', [ToLength(), IsEqualOrMore(8)])
Traceback (most recent call last)
...
ValueError: 6 is lower than 8Any callable object that accepts and returns a single value can be used as a
validator:>>> from string import *
>>> sanitize (" my title ", [strip, capitalize])
'My title'A rich library of prebuilt validators is supplied:>>> sanitize ('abcde', IsNonblank())
'abcde'
>>> sanitize (5, IsInRange(1,6))
5
>>> sanitize ('foo', ToSynonym({'foo': 'bar', 'baz': 'quux'}))
'bar'Custom validators can easily be subclassed from a supplied base class:class IsFoo (BaseValidator):
        def validate_value (self, value):
                if value != 'foo':
                        self.raise_validation_error (value)
                return TrueLimitationskonvalis aimed at a one-way transformation of data, turning user input or
stored dataintoPython objects. Certainly it could be used in the reverse
direction, but this is not a primary use case. FormEncode is based around
two-way (round trip) conversion of data, so that may be a useful alternative.The namekonvalwas chosen because:there’s already a Python library called “sanity”out of “valcon”, “valkon”, “conval” etc. it was the one with the fewest hits
on GoogleCreditThanks to Ian Bicking for inspiration in FormEncode. Thanks also to Egor Ryabkov.References[konval-home]konval home page[konval-pypi]konval on PyPi[setuptools]Setuptools & easy_install[konval-github]konval on github[formencode]FormEncodeHistoryv0.3, 20110802Renamed a few validators to more consistent scheme. Names should be fixed now.v0.2, 20110726Fixed incorrect exampleAdded logic validators (Or, Default, Constant)v0.1, 20110722Initial release