Skip to main content

Validation and parsing library

Project description

Trafaret
========

-----

|circleci_build| |pypi_version| |pypi_license|

-----


Read The Docs hosted documentation <http://trafaret.readthedocs.org/en/latest/>
or look to the docs/api/intro.rst for start.

Trafaret is rigid and powerful lib to work with foreign data, configs etc.
It provides simple way to check anything, and convert it accordingly to your needs.

It have shortcut syntax and ability to express anything that you can code:

.. code-block:: python

>>> from trafaret.constructor import construct
>>> validator = construct({'a': int, 'b': [str]})
>>> validator({'a': 5, 'b': ['lorem', 'ipsum']})
{'a': 5, 'b': ['lorem', 'ipsum']}

>>> validator({'a': 5, 'b': ['gorky', 9]})
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 204, in __call__
return self.check(val)
File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 144, in check
return self._convert(self.check_and_return(value))
File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 1105, in check_and_return
raise DataError(error=errors, trafaret=self)
trafaret.DataError: {'b': DataError({1: DataError(value is not a string)})}

For simple example what can be done:

.. code-block:: python

import datetime
import trafaret as t

date = t.Dict(year=t.Int, month=t.Int, day=t.Int) >> (lambda d: datetime.datetime(**d))
assert date.check({'year': 2012, 'month': 1, 'day': 12}) == datetime.datetime(2012, 1, 12)

Work with regex:

.. code-block:: python

>>> c = t.String(regex=r'^name=(\w+)$') >> (lambda m: m.groups()[0])
>>> c.check('name=Jeff')
'Jeff'

Rename dict keys:

.. code-block:: python

>>> c = t.Dict(t.Key('uNJ') >> 'user_name': t.String})
>>> c.check({'uNJ': 'Adam'})
{'user_name': 'Adam'}

``Arrow`` date checking:

.. code-block:: python

import arrow

def check_datetime(str):
try:
return arrow.get(str).naive
except arrow.parser.ParserError:
return t.DataError('value is not in proper date/time format')

Yes, you can write trafarets that simple.


Related projects
----------------

`Trafaret Config <https://github.com/tailhook/trafaret-config>`_

`Trafaret Validator <https://github.com/Lex0ne/trafaret_validator>`_


.. |circleci_build| image:: https://circleci.com/gh/Deepwalker/trafaret.svg?style=shield
:target: https://circleci.com/gh/Deepwalker/trafaret
:alt: Build status

.. |pypi_version| image:: https://img.shields.io/pypi/v/trafaret.svg?style=flat-square
:target: https://pypi-hypernode.com/pypi/trafaret
:alt: Downloads

.. |pypi_license| image:: https://img.shields.io/pypi/l/trafaret.svg?style=flat-square
:target: https://pypi-hypernode.com/pypi/trafaret
:alt: Downloads

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trafaret-0.9.1.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

trafaret-0.9.1-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file trafaret-0.9.1.tar.gz.

File metadata

  • Download URL: trafaret-0.9.1.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for trafaret-0.9.1.tar.gz
Algorithm Hash digest
SHA256 07dbfbfe08e18a897c43a973df6433e99d68666728f58d4fb151d47908c378a6
MD5 cc9a3adfa673fffbc4cba0cd2d215fff
BLAKE2b-256 a665617911c3e2d9f18711c7d11e99452b865d1ac1a77d33a5f3e3961f60cd6c

See more details on using hashes here.

File details

Details for the file trafaret-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for trafaret-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d418c43a4976a9a5ca70cdebf583c5c2f903fc80ed8639b3eba59a64e0b0d840
MD5 c84e5ee5832fb0835d04b2babadb8271
BLAKE2b-256 1fc1a56e33004fe4513d99cadd6ed99b7151d8c15cc4edb9a90bef73fc8ba3b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page