Data serialization and validation library
Project description
Data serialization and validation library
Features
flexible schema definition API with powerful type combinators
data validation
serialization/deserialization
in-place deserialization
Example
from lollipop.types import Object, String, Date
from lollipop.validators import Length
from collections import namedtuple
from datetime import date
Person = namedtuple('Person', ['name'])
Book = namedtuple('Book', ['title', 'publish_date', 'author'])
PersonType = Object({
'name': String(validate=Length(min=1)),
}, constructor=Person)
BookType = Object({
'title': String(),
'publish_date': Date(),
'author': PersonType,
}, constructor=Book)
harryPotter1 = Book(
title='Harry Potter and the Philosopher\'s Stone',
publish_date=date(1997, 6, 26),
author=Person(name='J. K. Rowling')
)
# Dumping
BookType.dump(harryPotter1)
# => {'title': 'Harry Potter and the Philosopher\'s Stone',
# 'publish_date': '1997-06-26',
# 'author': {'name': 'J. K. Rowling'}}
# Loading
BookType.load({'title': 'Harry Potter and the Philosopher\'s Stone',
'publish_date': '1997-06-26',
'author': {'name': 'J. K. Rowling'}})
# => Book(title='Harry Potter and the Philosopher\'s Stone',
# publish_date=date(1997, 06, 26),
# author=User(name='J. K. Rowling'))
# Partial inplace loading
BookType.load_into(harryPotter1, {'publish_date': '1997-06-27'})
# => Book(title='Harry Potter and the Philosopher\'s Stone',
# publish_date=date(1997, 06, 27),
# author=User(name='J. K. Rowling'))
# Loading list of objects
List(BookType).load([
{'title': 'Harry Potter and the Philosopher\'s Stone',
'publish_date': '1997-06-26',
'author': {'name': 'J. K. Rowling'}},
{'title': 'Harry Potter and the Chamber of Secrets',
'publish_date': '1998-07-02',
'author': {'name': 'J. K. Rowling'}},
])
# => [Book(...), Book(...)]
# Validation
BookType.validate({
'title': 'Harry Potter and the Philosopher\'s Stone',
'author': {'name': ''},
})
# => {'author': {'name': 'Length should be at least 1'},
# 'publish_date': 'Value is required'}
Installation
$ pip install lollipop
Documentation
Documentation is available at http://lollipop.readthedocs.io/ .
Requirements
Python >= 2.6 or <= 3.6
Project Links
Documentation: http://lollipop.readthedocs.io/
License
MIT licensed. See the bundled LICENSE file for more details.
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
lollipop-1.1.4.tar.gz
(71.4 kB
view hashes)
Built Distribution
Close
Hashes for lollipop-1.1.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15a0a25dc6598faf4c5925a0ed6cc8ccff0ab00d2722b01ea61b9e3634a4c723 |
|
MD5 | ee8e3f131e0e417a9c8c5d754ed6a6d9 |
|
BLAKE2b-256 | 1be4c8a654b39402995dbf0aebe1084fd20369f262dd1a5970b15ea3bbad57e7 |