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'
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.
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.1.tar.gz
(16.5 kB
view details)
Built Distribution
trafaret-0.6.1-py3-none-any.whl
(18.1 kB
view details)
File details
Details for the file trafaret-0.6.1.tar.gz
.
File metadata
- Download URL: trafaret-0.6.1.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28bcb77ba4f4eccb1f8b3cd4a5400202ddf3840783f9235e515a9d2f25ad715b |
|
MD5 | 69450f27c22448ccecc580ba81f83f02 |
|
BLAKE2b-256 | cf68f4ea1722fc1aa7b73f3eeffc21eaf9c338fe6ae02d703759a40c88269d7c |
File details
Details for the file trafaret-0.6.1-py3-none-any.whl
.
File metadata
- Download URL: trafaret-0.6.1-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc29178eb80a5e76cb4414f36bbdc8e91351c04e3479000ec675a5df1910b72f |
|
MD5 | 0c6d7c9852974d3b377055fb410e63ba |
|
BLAKE2b-256 | 6a58b41ba61d692880decbebe786015243cdfabe5c7dfa77afd804b4c8c2adb4 |