Skip to main content

Validate and generate data from templates

Project description

PyCont

Travis codecov Documentation Status

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

Uploaded Source

Built Distribution

pycont-0.0.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pycont-0.0.2.tar.gz.

File metadata

  • Download URL: pycont-0.0.2.tar.gz
  • Upload date:
  • Size: 8.4 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

Hashes for pycont-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a8aa5bbe720d2dfdedca001f6380c7107dd4606699c893ec6b4c14a9a24c35b0
MD5 4e842fcbed16566158341b97fde1f009
BLAKE2b-256 399a033a8dd598fcd76026f08dc8c98a92c8020b547251573e46c7013afd3c0e

See more details on using hashes here.

File details

Details for the file pycont-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pycont-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.7 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

Hashes for pycont-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a6aa774520423b1855d4711370097694958de1ba20c0e6911826b999bff94e06
MD5 d4887a09dc2d39dea5bc8d58259fc3a7
BLAKE2b-256 105e7a1f00f0e5c2528a0bb09bec3f8d6b3a0a86be0db5e76d733eb1daaaab8f

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