Skip to main content

Validation and parsing library

Project description

Trafaret
========

-----

|pypi_downloads| |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.

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.


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

.. |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.7.4.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

trafaret-0.7.4-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trafaret-0.7.4.tar.gz
Algorithm Hash digest
SHA256 5032ce22b10c8bb3a45a3b2cc135f2540c9703d7b1181504115c3d78cad08ae4
MD5 4276e591d3239c4e09318bbf955c7c41
BLAKE2b-256 5b4fe5311bae84e7a5375d0c145adbada3f2b5b33898bbe3410f850bd5a7bf79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for trafaret-0.7.4-py3-none-any.whl
Algorithm Hash digest
SHA256 afe6cafb80fd32533ea583247a6a7c29e0c6095725d3f26e2cf52c5f43f83a86
MD5 1a5db57fa3cefc724a5306a73b4afd28
BLAKE2b-256 cf4c758fb3b0e8daa734b6663134ceb42e3a5d8a744527667b8f79497873e8e8

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