A Library to enable preconversion of any Python type into one that is easily serializable
Project description
Preconvert
A Library to enable preconversion of any Python type into one that is easily serializable.
Preconvert provides a way to define conversions from more complex objects and datastructures into the base types before serialization happens. This happens in a way that is independant of the eventual form of serialization, allowing you to easily then serialize into multiple formats.
Why?
Have you ever tried to json.dumps
a data structure, only to be surprised when your DataClass throws an exception, or your namedtuple outputs as a list?
Preconvert was created to solve this problem across common serialization formats.
Design goals:
- Enable conversion from complex to simple types independant of desired output format
- Provide built in conversion for common types that are not universally supported (dataclasses, namedtuple, etc...)
- Provide a way to build custom preconverters or override built-in preconverters
- Ability to build preconverters that are dependent on the destination format
- Easy utilization from existing projects
How do I use this?
- Download
pip3 install preconvert
- Utilize
If your project uses one of our built-in supported serializers (json, msgpak, bson) you can simply replace your existing serializer import with a preconvert one:
from preconvert.outputs import json
OR
from preconvert.outputs import simplejson as json
OR
from preconvert.outputs import msgpack
OR
from preconvert.outputs import bson
If not you can inject preconvert before usage of any other serializers, often by setting a default
or on_onknown
parameter:
import preconvert
import my_serializer
my_serializer.dumps(default=preconvert.unserializable)
How do I extend this?
Want to add preconversion to your own custom types? For OOP projects, one easy way to do this is to add a __native_types__
method to your object:
class MyCustomClass(object):
def __init__(self, first_name, children=()):
self.first_name = first_name
self.children = children
def __native_types__(self)
return {'first': self.first_name, 'children': children}
For other entities, such as objects you do not control, you can register a new preconverter using the preconverter.converter
decorator:
import preconverter
@preconverter.converter(SomeFrameworkObject)
def convert_framework_object(instance):
return {'name': instance.name}
You can also, optionally, specify preconversions per an intended serialization format:
import preconverter
@preconverter.json(SomeFrameworkObject)
def convert_framework_object(instance):
return {'json': {'name': instance.name}}
@preconverter.msgpack(SomeFrameworkObject)
def convert_framework_object(instance):
return ['name', instance.name]
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
Built Distribution
Hashes for preconvert-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2607f8cb3c697f818074266ac12657546ed49f46c8282ba77c71eb52aea02a0d |
|
MD5 | 7b0e0f8a33c4c19f3283d5cf856be6cf |
|
BLAKE2b-256 | cab598c9da427f542677f24d26410b27873422e635e41b94d85b723ff8ab0b15 |