This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A simple library for converting to and from Python objects

Project Description

Cornflake is a serialization library inspired by Django REST Framework.

Usage

First we create a serializer and a class to test it with:

from cornflake import fields
from cornflake import serializers
from cornflake.exceptions import ValidationError
from cornflake.validators import not_empty, not_in_future

class PatientSerializer(serializers.Serializer):
    first_name = fields.StringField(validators=[not_empty()])
    last_name = fields.StringField(validators=[not_empty()])
    birth_date = fields.DateField(validators=[not_in_future()])
    death_date = fields.DateField(required=False)

    def validate_first_name(self, value):
        if value.upper() == 'TEST':
            raise ValidationError('No test patients please.')

        return value

    def validate(self, data):
        if data['death_date'] is not None and data['death_date'] < data['birth_date']:
            raise ValidationError({'death_date': "Can't be before birth date."})

        return data

    def create(self, validated_data):
        return Patient(**validated_data)

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            setattr(instance, key, value)

        return instance

class Patient(object):
    def __init__(self, first_name, last_name, birth_date, death_date):
        self.first_name = first_name
        self.last_name = last_name
        self.birth_date = birth_date
        self.death_date = death_date

Now we can create a patient:

>>> serializer = PatientSerializer(data={'first_name': 'John', 'last_name': 'Smith', 'birth_date': '2001-02-03'})
>>> serializer.is_valid()
True
>>> patient = serializer.save()

Update the patient with new data:

>>> serializer = PatientSerializer(patient, data={'first_name': 'John', 'last_name': 'Smith', 'birth_date': '2001-02-03', 'death_date': '2016-01-01'})
>>> serializer.is_valid()
True
>>> patient = serializer.save()

Serialize the patient to use in an API response:

>>> serializer = PatientSerializer(patient)
>>> serializer.data
{'birth_date': '2001-02-03', 'first_name': u'John', 'last_name': u'Smith', 'death_date': '2016-01-01'}

Deserialize, validate and report errors:

>>> serializer = PatientSerializer(data={'first_name': 'TEST', 'last_name': 'Smith', 'birth_date': '2001-02-03'})
>>> serializer.is_valid()
False
>>> serializer.errors
{'first_name': ['No test patients please.']}

Tests

Run tests with the tox command:

pip install tox
tox
Release History

Release History

This version
History Node

0.2.2

History Node

0.2.1

History Node

0.2.0

History Node

0.1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
cornflake-0.2.2.tar.gz (19.0 kB) Copy SHA256 Checksum SHA256 Source Aug 23, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting