GithubHelp home page GithubHelp logo

akscram / lollipop Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maximkulkin/lollipop

0.0 2.0 0.0 267 KB

Python data serialization/validation library

License: MIT License

Python 100.00%

lollipop's Introduction

lollipop

License: MIT Build Status Documentation Status PyPI

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

License

MIT licensed. See the bundled LICENSE file for more details.

lollipop's People

Contributors

amitaiporat avatar maximkulkin avatar vovanbo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.