Skip to main content

A data contracts system for python loosly modeled after django forms.

Project description

A declarative data contract container type for Python

Pycontracts is currently an internal beta and not ready for production use yet.

Python data contracts (pycontracts in PyPi) is a library for exchanging data between systems. It is losesly based on the django forms api to help ease adoption. A simple example:

from pycontract import DataContract, StringField, NumberField

class Person(DataContract):
    name = StringField()
    phone = StringField(null=True)
    age = NumberField(null=True)

bob = Person()
bob["name"] = "Bob Smith"
bob["phone"] = "999-555-1234"
bob["age"] = 32

In this example the contract for person is declared and the record for bob is instantiated. In addition to the basic data we can add processors which manipulate or clean the data as well as validators which validate the data similar to django validators.

Features

  • Customizable validation (compatible with django validators).

  • Data processors to clean up and format data.

  • Declarative field definitions.

  • Enforced value assignment helps avoid regression errors.

  • Licensed under the MIT License.

Basic Usage

The first step is define a basic contract. This is done by inheriting from pycontract.DataContract. Next we decide if there is standard processing that is needed for each field. For example we could use the strip_white_space processor to call pythons strip on each value. Finally we decide if there is any extra validation that is needed on the values. For example to ensure that the persons name starts with an “a” we could use the RegexValidator. So with this information we would declare this DataContract like this:

from pycontract import DataContract, StringField, NumberField, strip_white_space, RegexValidator

class Person(DataContract):
    name = StringField(processors=(strip_white_space,), validators=(RegexValidator(r"[aA].+"),))
    phone = StringField(null=True)
    age = NumberField(null=True)

Now at runtime we can set the values, check for a valid contract and finally access the values. That would like something like this:

>>> manny = Person()
>>> manny["name"] = "Angel Man "
>>> manny["phone"] = "999555-1234"
>>> manny["age"] = 22
>>> manny.is_valid()
True
>>> print manny.name
'Angel Man'

This is early release and the code base is very short, so for more information see the code. The source code can be found at github.

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

pycontract-0.1.3.tar.gz (8.7 kB view details)

Uploaded Source

File details

Details for the file pycontract-0.1.3.tar.gz.

File metadata

  • Download URL: pycontract-0.1.3.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pycontract-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5cd53f2cbf93c4ae2a15fdf1828d33c860e1e11e584815bafb5c7e8d1e31dc72
MD5 80706a8faf469201e9f9fcee6279bea9
BLAKE2b-256 b271e70f3e940384e8bb0727884af0b0e423c27e5eddb02d143bbd15bac23c18

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