Skip to main content

msgpack as a pickler

Project description

msgpickle

msgpickle is a library that enhances msgpack for Python, providing extensive pickling mechanism, including lambda support.

Description

Allows for the serialization and deserialization of complex Python objects using msgpack, with support for custom serialization methods for any object type, even those that cannot be modified directly.

Installation

pip install msgpickle

Usage

import msgpickle

import datetime

# Example object
obj = datetime.datetime.now()

# serialize the object
serialized = msgpickle.dumps(obj)

# deserialize the object
deserialized = msgpickle.loads(serialized)

print(deserialized)

Support lambdas, cloud-functions across server farms

Using the cloud_function_serializer, you can serilize lambdas, as long as the python version is the same.

serializer = msgpickle.MsgPickle(use_default=False)
serializer.register(*msgpickle.cloud_function_serializer)

Because use_defaults is false, that pickler won't serialize anything except basic objects and lambdas

Custom Serialization and Deserialization

# Custom serialization function
def datetime_pack(obj):
    if isinstance(obj, datetime):
        return obj.isoformat()
    return obj

# Custom deserialization function
def datetime_unpack(obj):
    try:
        return datetime.fromisoformat(obj)
    except TypeError:
        return obj

# Register the custom functions for datetime
msgpickle.register('datetime.datetime', datetime_pack, datetime_unpack)

You may register "None" as either the pack or unpack function. This will use the default instead for that class.

Strict mode

Just like msgpack, you can specify "strict=True" on dump and/or load. This will not use any "default handlers" for object, but will continue to use explicit registered handlers.

Disable, or rename object-oriented from_pack and to_pack to prevent a malicious payload from executing those functions.

pickler = MsgPickle(use_default=False, use_oo=None)

Easily create a truly safe pickler, even for complex objects. Disabling the defaults and oo hooks is sufficient for security. Strict mode, at that point, will just prevent msgpack from serializing tuples as lists.

Custom object signatures

Normally, msgpickle pickles objects as a mapping with 3 keys: ".", "#" and "d". It then attempts to deserialize these using your registered handlers, etc.

However, you may want to customize the object signatures. This can be done by overriding pickler.CLASS pickler.MODULE and pickler.DATA

Compression signatures and enumeration

Normally, msgpickle pickles class and module information as strings.

This can be overridden by objects as a mapping with 3 keys: ".", "#" and "d". It then attempts to deserialize these using your registered handlers, etc.

However, you may want to customize the object signatures. This can be done by overriding pickler.CLASS pickler.MODULE and pickler.DATA values.

Likewise, you may not want to serialize class names and module names.

You can enable enumeration using pickler.use_enumeration([keys]). Without an explicit list of class paths, this will build an enumeration of all registered classes, and use that as your pickled values. Needless to say, it's better to have an explicit list. Changing this list or the order of the list will result in being unable to deserialize.

This will significantly compress the output.

Advanced Usage

For more complex projects requiring different serialization/deserialization strategies, you can create instances of MsgPickle with custom serializers for specific types.

Here’s how to instantiate a MsgPickle object, localized to a module.

from msgpickle import MsgPickle

msgpickle = MsgPickle()

msgpickle.register("my.class", packer, unpacker)

serialized_data = msgpickle.dumps(obj)
deserialized_data = msgpickle.loads(dat)

print(deserialized_data)

Alternately, you can create to_pack() and from_pack() functions in your class, which will be used instead.

License

This project is licensed under the MIT License.

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

msgpickle-1.4.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

msgpickle-1.4.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file msgpickle-1.4.1.tar.gz.

File metadata

  • Download URL: msgpickle-1.4.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for msgpickle-1.4.1.tar.gz
Algorithm Hash digest
SHA256 2c69538810d59fd7fb1b8610dbef48fbc41d1315a714aadc2969e753f61c2f25
MD5 edb7f0a640fb755a3db193f5a44841fc
BLAKE2b-256 361f7b0b3a424548b4678f1321bf057747e13b436e3f875ae202e22696b27b30

See more details on using hashes here.

File details

Details for the file msgpickle-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: msgpickle-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for msgpickle-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d58df8a7f90d585b2bd46129b499c9ffbaccc08b6267b8658e5e294962a9f1ae
MD5 db398f93f75cd44cb150e9bdcebcd5e5
BLAKE2b-256 87a9d19dabbf9945c944605cabe0e0b2f7053eecb635ab32c908b993e10a2a81

See more details on using hashes here.

Supported by

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