Skip to main content

objective

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.

Metadata

Release files for objective 0.0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for objective 0.0.13
File Interpreter ABI Platform
objective-0.0.13-py2.py3-none-any.whl Python 3, Python 2 none any Details

Release files / objective-0.0.13-py2.py3-none-any.whl

Download URL objective-0.0.13-py2.py3-none-any.whl
Size 10.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6cbad75e6ccdb6a4cb187cd24caba3932e1adcd7719c8679ad166c378aadd775
BLAKE2b-256 checksum
How to use checksums
b6bcc45a0aeef506a76704d6027a93506cf894f290b4b0d4749f2567ec8ae5b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

Release history Release notifications | RSS feed

This release

0.0.13 This release

1 release file

0.0.11

2 release files

0.0.10

1 release file

0.0.9

1 release file

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page