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, list[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.6.tar.gz (6.3 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.6-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pserialization-0.0.6.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for pserialization-0.0.6.tar.gz
Algorithm Hash digest
SHA256 1126616a24da1d384e8bdba8030afdcf8d63b077519e88f780b24e7700048621
MD5 b43ca1c39b2a45967accad566b410837
BLAKE2b-256 94c588a50aab4838130e383f870b4c29273b8508bc12fb1a805e6c0dd4ee817a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pserialization-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for pserialization-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b1fc58be4a11fb99f1044376324201720a317902285b528fe9036e58b265633e
MD5 a492e5ad017b8cf4ec77b2a32bfd77d1
BLAKE2b-256 6928302eab7b708e551d1f7a315b42ca59e8f9a1b700ce5e169e47f8d4c0dd18

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