Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neilisaac/lazycontract
Python library to define declarative contracts for serialization and deserialization
https://github.com/neilisaac/lazycontract
Last synced: 3 months ago
JSON representation
Python library to define declarative contracts for serialization and deserialization
- Host: GitHub
- URL: https://github.com/neilisaac/lazycontract
- Owner: neilisaac
- License: mit
- Created: 2015-06-25T23:34:31.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-28T18:54:28.000Z (about 6 years ago)
- Last Synced: 2024-07-20T16:44:16.976Z (4 months ago)
- Language: Python
- Size: 32.2 KB
- Stars: 7
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-models - lazycontract - Python library to define declarative contracts for serialization and deserialization. (Model, Schema)
README
lazycontract
============### Status
[![TravisCI](https://travis-ci.org/neilisaac/lazycontract.svg)](https://travis-ci.org/neilisaac/lazycontract)
[![Code Climate](https://codeclimate.com/github/neilisaac/lazycontract/badges/gpa.svg)](https://codeclimate.com/github/neilisaac/lazycontract)Fork the [GitHub project](https://github.com/neilisaac/lazycontract) to contribute!
### Get it
Available on [PyPI](https://pypi.python.org/pypi/lazycontract):
```
pip install lazycontract
```### Overview
Base classes:
`StrictContract` only allows the defined attributes
`LazyContract` ignores undefined attributes
`DynamicContract` deserializes undefined attributes, but does not serialize them
### Example
```python
import json
import lazycontract# custom property
class SloppyCSVProperty(lazycontract.LazyProperty):
_type = list
def deserialize(self, obj):
return obj.split(",") if isinstance(obj, (str, unicode)) else objdef serialize(self, obj):
return ",".join(str(item) for item in obj)# custom contract
class SloppyDocument(lazycontract.LazyContract):
title = lazycontract.StringProperty(default="new document")
header = SloppyCSVProperty()
body = lazycontract.ListProperty(SloppyCSVProperty())# serialization
test1 = SloppyDocument(title="hello world", header=["first", "second"], body=[])
test1.body.append([1, 2])
test1.body.append([3, 4])print json.dumps(test1.to_dict()) # {"body": ["1,2", "3,4"], "header": "first,second", "title": "hello world"}
# deserialization
data = json.loads('{"body": ["1,2", "3,4"], "header": "first,second", "title": "hello world"}')
test2 = SloppyDocument(data)print test2.title # hello world
print test2.header # [u'first', u'second']
print test2.body # [[u'1', u'2'], [u'3', u'4']]print type(test2) #
print type(test2.header) #
print type(test2.header[0]) #```
See `lazycontract/test_properties.py` for more examples of property usage.
### Goals and inspiration
* Provide a simple means to express data contracts in code.
* Blatantly inspired by [jsonobject](https://github.com/dimagi/jsonobject)'s API
* Attribute serialization, de-serialization and validation should be trivial to understand and implement
* Errors in the data or contract should be easy to debug
* Don't impose a wire format or encoder/decoder### TODO
* lazy deserialization
* timestamp properties