Skip to main content

Validation and parsing library

Project description

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'

Change dict names:

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

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

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

Uploaded Source

Built Distribution

trafaret-0.6.0-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trafaret-0.6.0.tar.gz
Algorithm Hash digest
SHA256 f39499b79f20ca3137af10983c0a44b047fc25eef16b71f748802eacf3e2ea8c
MD5 22b11b3db20fcdf2b455d8d94b3e4330
BLAKE2b-256 3f48328b647a514dbcc11618db81cc6dfd7184862b96b82804d09f88b5ddad88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for trafaret-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82a9b1e1c731271825cbae78df8dd8ad54c1240de78997db510fdc0bc35c3f52
MD5 c1a3a1dd9bca49a160b2e116a8a5cb61
BLAKE2b-256 69e488e21f009532b413b2ad8ee6cf7570ca051990a023b9d56b61302a5933a2

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