msgpack as a pickler
Project description
msgpickle
msgpickle is a library that enhances msgpack for Python, providing a more extensive pickling mechanism.
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)
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.
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)
## 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file msgpickle-1.0.1.tar.gz.
File metadata
- Download URL: msgpickle-1.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
685753d8f1899d3a736b00f8d742c9a38e8767de40e1429a39feffabad8025ad
|
|
| MD5 |
c4a55725ebf9662e540b3eda1d9b1a96
|
|
| BLAKE2b-256 |
e1caad4d833a2d90e862031b0cecc1ecbb3327dfda78b7a7a7a6137ae3879710
|
File details
Details for the file msgpickle-1.0.1-py3-none-any.whl.
File metadata
- Download URL: msgpickle-1.0.1-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41820a23a8d54d20751f47006fdff00a0e771b309a516ad507b192a11aa0e3fa
|
|
| MD5 |
abc05d0bcd19f7aed2cb2792e9a599ff
|
|
| BLAKE2b-256 |
f1c7eaf96ab3bcf60f1bd4a39b326e76a6a030cb16f2e480be9e8d86e44e3f54
|