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.7.tar.gz
(7.6 kB
view details)
Built Distribution
File details
Details for the file objective-0.0.7.tar.gz
.
File metadata
- Download URL: objective-0.0.7.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3f668f285c7e17569a3bf058a3b01b1c7b5cb108a985582517a67aa28a81327 |
|
MD5 | 192df1aaeb3dc76c6ef7e738e252d30c |
|
BLAKE2b-256 | 4a18775862baec862ff5bc562dd67e04912eaeb9fbb83cb789745d377143fd89 |
File details
Details for the file objective-0.0.7-py2.py3-none-any.whl
.
File metadata
- Download URL: objective-0.0.7-py2.py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec7d92a416ff45a9ea3f052da138a1ce71a041439327da2290bc75ef0b60ebb9 |
|
MD5 | e3dae7238dd01d0dbf031bf941cb3b0d |
|
BLAKE2b-256 | dac70ea5079d3674ffe476d9d4deedd17af19445459eea867f81c800aba6ab59 |