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

Uploaded Source

Built Distribution

trafaret-0.6.2-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trafaret-0.6.2.tar.gz
Algorithm Hash digest
SHA256 1455c47ff202bde02976c462efa79aafb7d878ae321907bdd93754afa98654dc
MD5 5cb89878554ccdfebeb675f0200c510e
BLAKE2b-256 5f61979e8f8cd0ce167116bdfa8852b5497d7be2c7c773f605e500102e045566

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for trafaret-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b3a6f19c3ded23255940d8632a94ead1b567d61e6f40ab3f771cc5196f9b90c7
MD5 7c24b0d68b1e2991fa8bb03f3af151d1
BLAKE2b-256 429285917cf6962c5ce94040b3a1c59ac5240eac8491730c31b7aa937a3fc411

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