Biryani - A Python conversion and validation toolbox

Biryani is a Python library to convert and validate data (for web forms, CSV files, XML files, etc).

Biryani seeks to provide the same functionality as FormEncode, while being more easy to use and extend.

For more informations on Biryani features, see chapter Biryani Design.

Usage Examples

Example 1: Email validator

Every converter returns the converted value and an optional error:

>>> from biryani import baseconv as conv
>>> conv.input_to_email(u'John@DOE.name')
(u'john@doe.name', None)
>>> conv.input_to_email(u'john.doe.name')
(u'john.doe.name', u'An email must contain exactly one "@"')
>>> conv.input_to_email(u'   ')
(None, None)

Example 2: Required email validator

Converters can be combined together to form more complex converters:

>>> input_to_required_email = conv.pipe(conv.input_to_email, conv.not_none)
>>> input_to_required_email(u'John@DOE.name')
(u'john@doe.name', None)
>>> input_to_required_email(u'   ')
(None, u'Missing value')

Example 3: Web form validator

A sample validator for a web form containing the following fields:

  • Username
  • Password (2 times)
  • Email
>>> validate_form = conv.struct(dict(
...     username = conv.pipe(conv.cleanup_line, conv.not_none),
...     password = conv.pipe(
...         conv.test(lambda passwords: len(passwords) == 2 and passwords[0] == passwords[1],
...             error = u'Password mismatch'),
...         conv.function(lambda passwords: passwords[0]),
...         ),
...     email = conv.input_to_email,
...     ))
>>> validate_form({
...     'username': u'   John Doe',
...     'password': [u'secret', u'secret'],
...     'email': u'John@DOE.name',
...     })
({'username': u'John Doe', 'password': u'secret', 'email': u'john@doe.name'}, None)
>>> result, errors = validate_form({
...     'password': [u'secret', u'other secret'],
...     'email': u'John@DOE.name',
...     })
>>> result
{'username': None, 'password': [u'secret', u'other secret'], 'email': u'john@doe.name'}
>>> errors
{'username': u'Missing value', 'password': u'Password mismatch'}

See Tutorial 1: Validating a web form for a complete explanation of a variant of this example.

Get Biryani

Biryani is available as an easy-installable package on the Python Package Index.

The code can be found in a Git repository, at http://gitorious.org/biryani.