declaraive de/serialization of python structures
Project description
Objective is a de/serialization tool with following features:
declarative definition of objectives.
reuse definitions by inheritance.
optional validation.
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
Release history Release notifications | RSS feed
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.6.tar.gz
(7.6 kB
view hashes)
Built Distribution
Close
Hashes for objective-0.0.6-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daf82e5f25fcddcdd483fb8c7507ebdb63bcb4a0eb5d6ba64f3afe513f73c97b |
|
MD5 | 736ca5ae19202494d77f72b89a133589 |
|
BLAKE2b-256 | 97f1229322378d159bea50ee054e7fff9e04d5f846439efe2753f12e4e3f820b |