MessagePack serializer for efficient binary data exchange among multiple languages
npx @tessl/cli install tessl/pypi-msgpack@1.1.0MessagePack is an efficient binary serialization format that enables data exchange among multiple languages like JSON but is faster and smaller. This Python implementation provides CPython bindings for high performance and a pure Python fallback for PyPy compatibility.
pip install msgpackimport msgpackCommon usage pattern:
from msgpack import packb, unpackbFor streaming applications:
from msgpack import Packer, Unpackerimport msgpack
# Pack Python objects to binary data
data = {'name': 'Alice', 'age': 30, 'scores': [95, 87, 92]}
packed = msgpack.packb(data)
# Unpack binary data back to Python objects
unpacked = msgpack.unpackb(packed)
print(unpacked) # {'name': 'Alice', 'age': 30, 'scores': [95, 87, 92]}
# File operations
with open('data.msgpack', 'wb') as f:
msgpack.pack(data, f)
with open('data.msgpack', 'rb') as f:
loaded_data = msgpack.unpack(f)MessagePack Python provides two implementation paths:
msgpack._cmsgpack)msgpack.fallback)The library automatically selects the best available implementation unless overridden via the MSGPACK_PUREPYTHON environment variable.
Core convenience functions for one-shot packing and unpacking operations, providing simple interfaces for the most common serialization tasks.
def packb(o, **kwargs): ...
def unpackb(packed, **kwargs): ...
def pack(o, stream, **kwargs): ...
def unpack(stream, **kwargs): ...Advanced classes for efficient streaming serialization and deserialization, enabling processing of large datasets and continuous data streams.
class Packer:
def __init__(self, **kwargs): ...
def pack(self, obj): ...
class Unpacker:
def __init__(self, file_like=None, **kwargs): ...
def feed(self, data): ...
def unpack(self): ...Specialized types for handling extension data and timestamps, enabling custom serialization of complex data structures.
class ExtType:
def __init__(self, code: int, data: bytes): ...
class Timestamp:
def __init__(self, seconds: int, nanoseconds: int = 0): ...Comprehensive exception hierarchy for robust error handling during serialization and deserialization operations.
class UnpackException(Exception): ...
class FormatError(ValueError, UnpackException): ...
class ExtraData(UnpackValueError): ...