Skip to main content

declarative de/serialization of python structures

Project description

As a result of frustration working with colander, I decided to create something similar…

Objective is a de/serialization tool with following features:

  • declarative definition of objectives.

  • no confusion about how to define something, it is always the same.

  • reuse definitions by inheritance.

  • optional validation.

  • make it simple to override certain parts.

Usage

Simple mapping and validation

import objective
import datetime


now = datetime.datetime(2001, 9, 11)


class UserObjective(objective.Mapping):

    name = objective.Item(objective.Unicode, missing=objective.Ignore)
    email = objective.Item(objective.Unicode, validator=objective.Email())
    password = objective.Item(objective.Unicode)
    since = objective.Item(objective.UtcDateTime, missing=now)


serialized = {
    'email': 'foo@example.com',
    'password': 'foobar'
}


deserialized = UserObjective.deserialize(serialized)


assert deserialized == {
    'password': u'foobar',
    'email': u'foo@example.com',
    'since': datetime.datetime(2001, 9, 11, 0, 0)
}


bad_serialized = {
    'name': 'foobar'
}


try:
    deserialized = UserObjective().deserialize(bad_serialized)

except objective.Invalid as e:

    assert isinstance(e, objective.exc.InvalidChildren)
    assert {(x.node.__name__, x.__class__, x.value) for x in e.children} == {
        ('password', objective.exc.MissingValue, objective.values.Undefined),
        ('email', objective.exc.InvalidValue, 'baz')
    }

Mapping complex structures

import objective


class ProductRequestObjective(objective.BunchMapping):
    @objective.Item()
    class body(objective.Mapping):
        name = objective.Item(objective.Unicode)
        root = objective.Item(objective.Unicode)

        @objective.Item()
        class semantics(objective.List):
            items = objective.Item(objective.Unicode)

    @objective.Item()
    class match(objective.Mapping):
        _id = objective.Item(objective.Field)

Issues, thoughts, ideas

objective is far from complete. There are tons of possible validators I can imagine.

I tried my best (mostly in terms of time) to test.

If you find bugs or you have a great idea, I am totally open to implement it.

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

objective-0.0.8.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

objective-0.0.8-py2.py3-none-any.whl (9.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file objective-0.0.8.tar.gz.

File metadata

  • Download URL: objective-0.0.8.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for objective-0.0.8.tar.gz
Algorithm Hash digest
SHA256 9c733601429f71e17661b5af3a5665170457d02a545b720c5d937c4d6a3b9e44
MD5 15ee7c8ce876373c484379afe34e99de
BLAKE2b-256 dc73231d6532fb011213d4e3a07c651df86ad58b79fa2257d7df459d54b69e2a

See more details on using hashes here.

File details

Details for the file objective-0.0.8-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for objective-0.0.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3380b0deb95d919a0b2a9b4e28b6bd5c681d4f460714e94e3d5e5845ef37065c
MD5 82b56d3419e3465a39b1f09630c6da52
BLAKE2b-256 6c1c634ee7f7df4955ba5eebd13ec87464944c785c0274d72b65f331c8d41e17

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