Validate and generate data from templates
Project description
PyCont
Validate and genereate pythpon objects from templates. Library is powered by Trafaret and helps to validate and genereate data from template.
Documentation
https://pycont.readthedocs.io/en/latest/index.html
Usage
pip install pycont
Simple data template
>>> from pycont import Template, Contract
>>> import trafaret as t
>>> contract = Contract(Template(t.Int()))
>>> print(contract(42))
42
>>> print(contract('test'))
Traceback (most recent call last):
...
ValueError: Invalid value: value can't be converted to int
Simple list template
>>> from pycont import Template, Contract
>>> import trafaret as t
>>> contract = Contract([
... Template(t.Int())
...])
>>> print(contract([42]))
[42]
>>> print(contract([1, 2, 3, 4, 5]))
[1, 2, 3, 4, 5]
>>> print(contract([1, 2, 3, 'error']))
Traceback (most recent call last):
...
ValueError: Invalid value: value can't be converted to int
Static list template
>>> from pycont import Template, Contract
>>> import trafaret as t
>>> contract = Contract([
... Template(t.Int()),
... Template(t.String()),
... Template(t.Float()),
...])
>>> print(contract([42, 'test', 12.5]))
[42, 'test', 12.5]
Dict template
>>> from pycont import Template, Contract
>>> import trafaret as t
>>> contract = Contract(Template(t.Int(), default=42))
>>> print(contract('error'))
42
>>> contract = Contract({
... 'id': Template(t.Int()),
... 'value': Template(t.String()),
...})
# Key 'key' not contains in template
>>> print(contract({'id': 1, 'value': 'test', 'key': None}))
{'id': 1, 'value': 'test'}
Deafult value
pycont.Template
contains an optional argument default
(the default type must be valid to Tremplate checker). If the argument is set, then if the check fails, this value will be returned.
>>> from pycont import Template, Contract
>>> import trafaret as t
>>> contract = Contract({
... 'id': Template(t.Int()),
... 'value': Template(t.String(), default='None'), # 'None' is a string
...})
>>> print(contract({'id': 1, 'key': None})) # key 'value' is not set
{'id': 1, 'value': 'None'}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pycont-0.0.1.tar.gz
(6.7 kB
view details)
Built Distribution
File details
Details for the file pycont-0.0.1.tar.gz
.
File metadata
- Download URL: pycont-0.0.1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f44d6087bcbd34eea83fa032aa18f0b1bbfe88a5046b441ae7fd99131facb7d1 |
|
MD5 | 63f68039bc8cc793a07e7d011959a0c0 |
|
BLAKE2b-256 | b7c4b6d0de1523efa4ae19537b1a081512265d16b938629413c2c65371289fa1 |
File details
Details for the file pycont-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: pycont-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 504cb0d18ad4e81bdb13b0e8f0212960531e8bcf9aaba9c7207b254cdcc6a820 |
|
MD5 | 43995d68530c404b5d7e807aee1b36f4 |
|
BLAKE2b-256 | f833f0b7a22620d5b52bb43089cbb7deea02b5038a584566c960e3d645760768 |