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.
Features
Functional
Complex Python object serialization
Data deserialization into simple Python structures
Data validation
Changes
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()]),
fields.date('release_date', required=True),
child('artist', serializer=artist_serializer, validators=[validators.required()])
)
class Artist(object):
def __init__(self, name):
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(
artist=bowie,
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': datetime.date(1971, 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.
Installation
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
1.0.1
refining validators
added attr_getter to child, and many
1.0.0
Updating docs
Making it official
0.0.9
Fixing python 3 comptatability issue
0.0.8
Removed an errant print statement
0.0.7
Fixed a bug with datetime validators
0.0.6
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
Metadata
Release files for strainer-2020 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| strainer-2020-1.4.0.tar.gz | 11.2 kB | Without provenance | Details for strainer-2020-1.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| strainer_2020-1.4.0-py3-none-any.whl | Python 3 | none | any | Details for strainer_2020-1.4.0-py3-none-any.whl |
Total release size: 23.4 kB
Release files / strainer-2020-1.4.0.tar.gz
| Download URL | strainer-2020-1.4.0.tar.gz |
|---|---|
| Size | 11.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
423c5bc60c2a67ef0cc16cb14a4adac1649f6ea727238a61ab664876d3d7600b
|
|
BLAKE2b-256 checksum How to use checksums |
8b6317cdca5c742612441dc243628439efa46e760c15880a5cd160e1064a3ed4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.6.13
|
Release files / strainer_2020-1.4.0-py3-none-any.whl
| Download URL | strainer_2020-1.4.0-py3-none-any.whl |
|---|---|
| Size | 12.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56fcdf87b0f0719e2f7f4e202f8937ce4efe3a96fff63c8127f584b431d7deda
|
|
BLAKE2b-256 checksum How to use checksums |
e6b4adbaae855f6ce429791cc3841bb6fed7135556b66691f0955824e24d733a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.6.13
|
Security for strainer-2020 1.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.