Skip to main content

A de/serialization library for python

Project description

PSerialization

Python library for serializing & deserializing python objects.

Out of the box support for "basic" object de/serialization, that is objects that hold all of their state in __ dict __ and have a trivial __ new __.

For more complicated types like datetime.datetime, users of this library can supply custom middleware to handle de/serializing those types.

Useful for sending python objects to a system that may only be expecting to handle primitive types, as well as reconstructing python objects from systems that lack type information. I personally use this for editing/loading configuration files stored as json, and for loading objects from nosql dbs like MongoDB.

https://github.com/SteffenCucos/PSerialization

'Basic' Object Example

from pserialize.serializer import Serializer
from pserialize.deserializer import Deserializer

serializer = Serializer()
deserializer = Deserializer()

class Shoe():
	def __init__(self, size: int, condition: str, brand: str):
		self.size = size
		self.condition = condition
		self.brand = brand

if __name__ == "__main__":
	shoes = [Shoe(11, "Good", "Nike"), Shoe(12, "Bad", "Geox")]
	
	# Serialize a python object into primitives
	serialized = serializer.serialize(shoes)
	
	assert serialized == [
		{ "size": 11, "condition": "Good", "brand": "Nike" },
		{ "size": 12, "condition": "Bad", "brand": "Geox" }
	]
	
	# Build back the object representation just from primitives
	deserialized = deserializer.deserialize(serialized, Shoe)
	
	assert deserialized == shoes

Middleware Example

from datetime import datetime
from pserialize.serializer import Serializer
from pserialize.deserializer import Deserializer

def serialize_datetime(date: datetime):
	return repr(date)

def deserialize_date(value: object):
	assert type(value) is str

	arg_str = value.split("(")[1]
	arg_str = arg_str.replace(")", "")
	args = arg_str.strip(" ").split(",")
	args = [int(arg) for arg in args]

	return datetime(*args)

serializer = Serializer(middleware={datetime: serialize_datetime})
deserializer = Deserializer(middleware={datetime: deserialize_datetime})

if __name__ == "__main__":
	date = datetime(2022, 7, 25, 11, 3, 44, 21000)

	# Serialized using the custom function
	serialized = serializer.serialize(date)
	assert serialized == "datetime.datetime(2022, 7, 25, 11, 3, 44, 21000)"

	# Deserialized back into the correct type
	deserialized = deserializer.deserialize(serialized, datetime)
	assert deserialized == date

Update PyPi Package

py -m build
py -m twine upload --repository pypi dist/*

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

pserialization-0.0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pserialization-0.0.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pserialization-0.0.2.tar.gz.

File metadata

  • Download URL: pserialization-0.0.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.0

File hashes

Hashes for pserialization-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a9dc806c8123482db3596a2d4b6cb02b141ad83b172c85ff8df1ab5ba7058d6c
MD5 b0d7adac78d4c5c475edc4882cef0c85
BLAKE2b-256 9e9d7b3e538e93b484a122e83110bf2a214f5cab28f10fc9155a00b8189ec57b

See more details on using hashes here.

File details

Details for the file pserialization-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pserialization-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8024e2d7e5853612113d3e04a4063ac17887127ff701d4e48234da06c42f3109
MD5 8cd99a4332c2779d760f607e18af527d
BLAKE2b-256 4f11c4da331d67c3b3f89ab5494a08894769cf2e0ac60521afcb7ae29f821e9f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page