Skip to main content

Validation and parsing library

Project description

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:

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:

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

Rename dict keys:

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

Arrow date checking:

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.

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

Uploaded Source

Built Distribution

trafaret-0.7.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trafaret-0.7.0.tar.gz
Algorithm Hash digest
SHA256 f78a54e59430babebecb4d5f09847f9a9e9fd4d0cd365417584474bf25183f5c
MD5 06a2a95c1f7b530a64be89dcfd50768d
BLAKE2b-256 f79a9a0e14027a8383098b12634f9eb8da76b033f45fd774675e31f0c118936b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for trafaret-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e5f71caf6e0b6b978f480d41ad7920f557c6a5243e8197e80c6fdf44b41acc
MD5 ed8bc22fc57984a14bf5245adef33b02
BLAKE2b-256 5717e0b4a611f1f60ccc1f11fc33171cd1b9f2382924d0b5fa408b1a9e614112

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