Skip to main content

Validation and parsing library

Project description

Build status Gitter Downloads Downloads

Ultimate transformation library that supports validation, contexts and aiohttp.

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:

>>> 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)})}

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

Trafaret can even generate Trafarets instances to build transformators from json, like in json schema implementation Trafaret Schema

New

  • converters and convert=False are deleted in favor of And and &

  • String parameter regex deleted in favor of Regexp and RegexpRaw usage

  • new OnError to customize error message

  • context=something argument for __call__ and check Trafaret methods. Supported by Or, And, Forward etc.

  • new customizable method transform like change_and_return but takes context= arg

  • new trafaret_instance.async_check method that works with await

Doc

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

This version

1.1.1

Download files

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

Source Distribution

trafaret-1.1.1.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

trafaret-1.1.1-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trafaret-1.1.1.tar.gz
Algorithm Hash digest
SHA256 687b0af21ab304067b684f1dd14ad2c6b3c30462d0403a6c3d4977733d92364e
MD5 c2de4ea400a40d1340742c4759cff8dc
BLAKE2b-256 c00577eed1251eb64ff4ea5b9f5a003ba77b32f50eec0c5c2c275f68e605c9fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for trafaret-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 473ba0c5b6dd065d80f5ae8600c1dd81a17be46dbd1a3bf073d0e1c343f1fbf3
MD5 c34fb844153327c769e7454d07f94963
BLAKE2b-256 4a3b88f1b32563e6ab95b70848e490b74ca1f9777cd221fd92731beafb890992

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