Skip to main content

Fast functional serializers

Project description

Strainer-2020 is a different take on serialization and validation in python. It utilizes a functional style over classes.

Strainer-2020 officially supports Python 3.6 and newer, and runs great on PyPy.

This is a fork of original Alex Kessinger pystrainer library with improvements that changed operation but not the usage paradigm. I’m trying to keep his commitment to efficiency.


  • Functional

  • Complex Python object serialization

  • Data deserialization into simple Python structures

  • Data validation


  • serialization is done by data serializers defined for fields

  • validators perform data validation only

  • basic field types have simplified interface functions tthat wrap generic field() function

  • datetime/time serialization and deserialization preserves timezone information or lack of it; naive datetimes/times are serialized as naive and then deserialized as naive too

Serialization Example

import datetime
from strainer import (serializer, field, child,
                      formatters, validators,
                      ValidationException, fields)

artist_serializer = serializer(
    field('name', validators=[validators.required()])

album_schema = serializer(
    field('title', validators=[validators.required()]),'release_date', required=True),
    child('artist', serializer=artist_serializer, validators=[validators.required()])

class Artist(object):
    def __init__(self, name): = name

class Album(object):
    def __init__(self, title, release_date, artist):
        self.title = title
        self.release_date = release_date
        self.artist = artist

bowie = Artist(name='David Bowie')
album = Album(
    title='Hunky Dory',
    release_date=datetime.datetime(1971, 12, 17)

Now we can serialize, deserialize, and validate data

>>> album_schema.serialize(album)
{'artist': {'name': 'David Bowie'},
 'release_date': '1971-12-17',
 'title': 'Hunky Dory'}
>>> album_schema.deserialize(album_schema.serialize(album))
{'artist': {'name': 'David Bowie'},
 'release_date':, 12, 17),
 'title': 'Hunky Dory'}
>>> input = album_schema.serialize(album)
>>> del input['artist']
>>> album_schema.deserialize(input)
ValidationException: {'artist': ['This field is required']}

The example has been borrowed from Marshmallow and tweaked.


To install Strainer-2020, simply:

$ pip install strainer-2020

Satisfaction, guaranteed.

Release History

1.4.0 (2020)

  • infrastructure refresh

  • separate operation of validators and serializers

  • change to datetime serialization that preserves timezone information or lack of it

  • drop Python 2 support, limit support to modern Python 3 only


  • refining validators

  • added attr_getter to child, and many


  • Updating docs

  • Making it official


  • Fixing python 3 comptatability issue


  • Removed an errant print statement


  • Fixed a bug with datetime validators


  • Fixed a bug with multiple validation, pointing to the correct index

  • Fixed a bug that applied vlaidation to entire array in multiple instead of elements

  • Added a dict_field, if source is dict, instead of an object

  • Added ability to pass validators to child, and many instances applying validation before moving to sub-element

  • Added tests around catching nested validation errors

  • Added formatters, so things can be formatted on the way out

  • Got rid of encoders, not the domain of this project

  • Everything can be imported from one namespace

  • Changed the API from to_representation/to_internal to serialize/deserialize

0.0.5 (2016-11-29)

  • Fleshed out docs

  • Added datetime validator

  • Increased speed bu reducing loops

0.0.4 (2016-11-23)

  • Add some validators

0.0.1 (2016-11-23)

  • Birth

Project details

Download files

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

Source Distribution

strainer-2020-1.4.0.tar.gz (11.2 kB view hashes)

Uploaded source

Built Distribution

strainer_2020-1.4.0-py3-none-any.whl (12.1 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page