Skip to main content

Python MessagePack module

C library for python 3.10+.

Why:

  • I couldn't negotiate adding type hints to msgpack-python
  • I couldn't negotiate adding Unpacker to ormsgpack
  • There's no stream unpacking in msgspec
  • I couldn't find another MessagePack library
  • msgpack-python interfaces are messy and the library is a bit slow

Installation

pip install amsgpack

Documentation

https://amsgpack.readthedocs.io/

Examples

>>> from amsgpack import packb, unpackb
>>> packb({"compact": True, "schema": 0})
b'\x82\xa7compact\xc3\xa6schema\x00'
>>> unpackb(b'\x82\xa7compact\xc3\xa6schema\x00')
{'compact': True, 'schema': 0}
>>> from amsgpack import FileUnpacker
>>> from io import BytesIO
>>> for data in FileUnpacker(BytesIO(b'\x00\x01\x02')):
...     print(data)
...
0
1
2
>>> from amsgpack import Unpacker
>>> unpacker = Unpacker()
>>> unpacker.feed(b'\x82\xa7compact\xc3\xa6schema\x00')
>>> next(unpacker)
{'compact': True, 'schema': 0}

Ext Type Packing

When encountering unsupported type a default callback is called:

>>> from typing import Any
>>> from amsgpack import Ext, Packer
>>> from array import array
>>>
>>> def default(value: Any) -> Ext:
...     if isinstance(value, array):
...         return Ext(1, value.tobytes())
...     raise ValueError(f"Unserializable object: {value}")
...
>>> packb = Packer(default=default).packb
>>> packb(array('I', [0xBA, 0xDE]))
b'\xd7\x01\xba\x00\x00\x00\xde\x00\x00\x00'

Ext Type Unpacking

By default when encountering Ext type, a conversion to datetime.datetime happens when code == 1, otherwise Ext instance is returned.

>>> from amsgpack import Ext, Unpacker
>>> from array import array
>>>
>>> def ext_hook(ext: Ext):
...     if ext.code == 1:
...         return array("I", ext.data)
...     return ext.default()
...
>>> Unpacker(ext_hook=ext_hook).unpackb(
...     b"\xd7\x01\xba\x00\x00\x00\xde\x00\x00\x00"
... )
array('I', [186, 222])

Benchmark

Benchmark Run amsgpack_benchmark.py and then chart.py to get your values

Metadata

Release files for amsgpack 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for amsgpack 0.5.0
File Size Uploaded
amsgpack-0.5.0.tar.gz 39.2 kB Details

Release files / amsgpack-0.5.0.tar.gz

Download URL amsgpack-0.5.0.tar.gz
Size 39.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7881ed94ebabf51f10725e00dedd7383402603567e4caad9dddfd7034b542e31
BLAKE2b-256 checksum
How to use checksums
39e9030598fd1bbd4d15409d37f178d3aa3d20701777bf1c80a9350e318ba11d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.10.22

Release history Release notifications | RSS feed

0.5.1

1 release file

This release

0.5.0 This release

1 release file

0.4.0

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page