<h1 align="center">
<img src="https://raw.githubusercontent.com/senyai/amsgpack/main/doc/amsgpack.svg" width="300">
</h1>
## Python MessagePack module
C library for python 3.10+.
Why:
* I couldn't negotiate adding type hints to [msgpack-python](https://github.com/msgpack/msgpack-python/pull/552)
* I couldn't negotiate adding Unpacker to [ormsgpack](https://github.com/aviramha/ormsgpack/issues/227)
* There's no stream unpacking in [msgspec](https://github.com/jcrist/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
```Python console
>>> 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}
```
```Python console
>>> from amsgpack import FileUnpacker
>>> from io import BytesIO
>>> for data in FileUnpacker(BytesIO(b'\x00\x01\x02')):
... print(data)
...
0
1
2
```
```Python console
>>> 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:
``` python
>>> 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.
``` python
>>> 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

Run `amsgpack_benchmark.py` and then `chart.py` to get your values
Raw data
{
"_id": null,
"home_page": null,
"name": "amsgpack",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "msgpack, messagepack, serializer, serialization, binary",
"author": null,
"author_email": "Arseniy Terekhin <senyai@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/81/27/563927be579884c9d0e11264dc5ff163acac047329f62d00bdc3bfdbf0eb/amsgpack-0.3.2.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">\n<img src=\"https://raw.githubusercontent.com/senyai/amsgpack/main/doc/amsgpack.svg\" width=\"300\">\n</h1>\n\n## Python MessagePack module\n\nC library for python 3.10+.\n\nWhy:\n * I couldn't negotiate adding type hints to [msgpack-python](https://github.com/msgpack/msgpack-python/pull/552)\n * I couldn't negotiate adding Unpacker to [ormsgpack](https://github.com/aviramha/ormsgpack/issues/227)\n * There's no stream unpacking in [msgspec](https://github.com/jcrist/msgspec)\n * I couldn't find another MessagePack library\n * `msgpack-python` interfaces are messy and the library is a bit slow\n\n\n### Installation\n`pip install amsgpack`\n\n### Documentation\n\nhttps://amsgpack.readthedocs.io/\n\n### Examples\n\n```Python console\n>>> from amsgpack import packb, unpackb\n>>> packb({\"compact\": True, \"schema\": 0})\nb'\\x82\\xa7compact\\xc3\\xa6schema\\x00'\n>>> unpackb(b'\\x82\\xa7compact\\xc3\\xa6schema\\x00')\n{'compact': True, 'schema': 0}\n```\n\n```Python console\n>>> from amsgpack import FileUnpacker\n>>> from io import BytesIO\n>>> for data in FileUnpacker(BytesIO(b'\\x00\\x01\\x02')):\n... print(data)\n...\n0\n1\n2\n```\n\n```Python console\n>>> from amsgpack import Unpacker\n>>> unpacker = Unpacker()\n>>> unpacker.feed(b'\\x82\\xa7compact\\xc3\\xa6schema\\x00')\n>>> next(unpacker)\n{'compact': True, 'schema': 0}\n```\n\n### Ext Type Packing\n\nWhen encountering unsupported type a `default` callback is called:\n\n``` python\n>>> from typing import Any\n>>> from amsgpack import Ext, Packer\n>>> from array import array\n>>>\n>>> def default(value: Any) -> Ext:\n... if isinstance(value, array):\n... return Ext(1, value.tobytes())\n... raise ValueError(f\"Unserializable object: {value}\")\n...\n>>> packb = Packer(default=default).packb\n>>> packb(array('I', [0xBA, 0xDE]))\nb'\\xd7\\x01\\xba\\x00\\x00\\x00\\xde\\x00\\x00\\x00'\n```\n\n### Ext Type Unpacking\n\nBy default when encountering `Ext` type, a conversion to `datetime.datetime`\nhappens when `code == 1`, otherwise `Ext` instance is returned.\n\n``` python\n>>> from amsgpack import Ext, Unpacker\n>>> from array import array\n>>>\n>>> def ext_hook(ext: Ext):\n... if ext.code == 1:\n... return array(\"I\", ext.data)\n... return ext.default()\n...\n>>> Unpacker(ext_hook=ext_hook).unpackb(\n... b\"\\xd7\\x01\\xba\\x00\\x00\\x00\\xde\\x00\\x00\\x00\"\n... )\narray('I', [186, 222])\n```\n\n### Benchmark\n\n\nRun `amsgpack_benchmark.py` and then `chart.py` to get your values\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Another MessagePack library",
"version": "0.3.2",
"project_urls": {
"Bug Reports": "https://github.com/senyai/amsgpack/issues",
"Documentation": "https://amsgpack.readthedocs.io/",
"Source": "https://github.com/senyai/amsgpack"
},
"split_keywords": [
"msgpack",
" messagepack",
" serializer",
" serialization",
" binary"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "8127563927be579884c9d0e11264dc5ff163acac047329f62d00bdc3bfdbf0eb",
"md5": "cfd47689de8b55fa9ae94c34d079e246",
"sha256": "a7e3a25c0f9474e4a9193f23cf538aaca259fe70abacda05e92d905acfdd3b2a"
},
"downloads": -1,
"filename": "amsgpack-0.3.2.tar.gz",
"has_sig": false,
"md5_digest": "cfd47689de8b55fa9ae94c34d079e246",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 37113,
"upload_time": "2025-07-24T08:21:55",
"upload_time_iso_8601": "2025-07-24T08:21:55.509924Z",
"url": "https://files.pythonhosted.org/packages/81/27/563927be579884c9d0e11264dc5ff163acac047329f62d00bdc3bfdbf0eb/amsgpack-0.3.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-24 08:21:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "senyai",
"github_project": "amsgpack",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "amsgpack"
}