Skip to content

litwisha/trafaret

This branch is 44 commits behind Deepwalker/trafaret_forked:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

876f03c · Mar 1, 2017
Jan 5, 2017
Jan 26, 2012
Mar 12, 2012
Mar 1, 2017
Mar 1, 2017
Aug 3, 2016
May 26, 2011
Dec 30, 2014
Sep 26, 2016
Jan 26, 2015
Jan 26, 2015
May 3, 2016
Feb 22, 2017
Feb 10, 2017
Jan 25, 2016

Repository files navigation

Trafaret


Downloads Downloads Downloads


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.

About

validate arbitrary data structures in python

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 92.5%
  • JavaScript 7.1%
  • HTML 0.4%