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.
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)
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')
A sample validator for a web form containing the following fields:
>>> 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.
Biryani is a free software.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
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.