|
1 | | -# msgpack-stream |
| 1 | +# msgpack-streams |
2 | 2 |
|
3 | | -Pure Python stream based implementation of msgpack |
| 3 | +Fast stream based implementation of msgpack in pure Python. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install msgpack-streams |
| 9 | +``` |
| 10 | + |
| 11 | +## Benchmarks |
| 12 | + |
| 13 | +Average of 50 iterations each on a 3.77 MB payload, pure Python |
| 14 | +(`MSGPACK_PUREPYTHON=1`). |
| 15 | + |
| 16 | +| Implementation | Operation | Speedup vs msgpack | |
| 17 | +| ------------------------------- | --------- | ------------------ | |
| 18 | +| msgpack-streams `unpack` | decode | 2.83x | |
| 19 | +| msgpack-streams `unpack_stream` | decode | 2.70x | |
| 20 | +| msgpack-streams `pack` | encode | 1.84x | |
| 21 | +| msgpack-streams `pack_stream` | encode | 1.69x | |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +```python |
| 26 | +from msgpack_streams import pack, unpack |
| 27 | + |
| 28 | +data = {"key": "value", "number": 42, "list": [1, 2, 3]} |
| 29 | +packed = pack(data) |
| 30 | +unpacked, excess_data = unpack(packed) |
| 31 | +assert data == unpacked |
| 32 | +assert not excess_data |
| 33 | +``` |
| 34 | + |
| 35 | +The stream based API is also available: |
| 36 | + |
| 37 | +```python |
| 38 | +from msgpack_streams import pack_stream, unpack_stream |
| 39 | +import io |
| 40 | + |
| 41 | +data = {"key": "value", "number": 42, "list": [1, 2, 3]} |
| 42 | + |
| 43 | +with io.BytesIO() as stream: |
| 44 | + pack_stream(stream, data) |
| 45 | + # reset stream position for reading |
| 46 | + stream.seek(0) |
| 47 | + unpacked = unpack_stream(stream) |
| 48 | + |
| 49 | +assert data == unpacked |
| 50 | +``` |
| 51 | + |
| 52 | +## Extensions |
| 53 | + |
| 54 | +### Datetime |
| 55 | + |
| 56 | +Timezone-aware `datetime` objects are natively supported and automatically |
| 57 | +encoded using the |
| 58 | +[msgpack Timestamp extension](https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type) |
| 59 | +(type code `-1`). The timestamp format (32-, 64-, or 96-bit) is chosen |
| 60 | +automatically based on the value's range and precision. Decoded timestamps are |
| 61 | +always returned as UTC `datetime` objects. |
| 62 | + |
| 63 | +```python |
| 64 | +from datetime import datetime, timezone |
| 65 | +from msgpack_streams import pack_stream, unpack_stream |
| 66 | +import io |
| 67 | + |
| 68 | +dt = datetime(2025, 3, 25, 12, 0, 0, tzinfo=timezone.utc) |
| 69 | + |
| 70 | +with io.BytesIO() as stream: |
| 71 | + pack_stream(stream, dt) |
| 72 | + stream.seek(0) |
| 73 | + unpacked = unpack_stream(stream) |
| 74 | + |
| 75 | +assert unpacked == dt |
| 76 | +``` |
| 77 | + |
| 78 | +Naive `datetime` objects (without `tzinfo`) will raise a `ValueError`. |
| 79 | + |
| 80 | +### ExtType |
| 81 | + |
| 82 | +Arbitrary msgpack extension types are supported via the `ExtType` dataclass: |
| 83 | + |
| 84 | +```python |
| 85 | +from msgpack_streams import ExtType, pack_stream, unpack_stream |
| 86 | +import io |
| 87 | + |
| 88 | +obj = ExtType(code=42, data=b"hello") |
| 89 | + |
| 90 | +with io.BytesIO() as stream: |
| 91 | + pack_stream(stream, obj) |
| 92 | + stream.seek(0) |
| 93 | + unpacked = unpack_stream(stream) |
| 94 | + |
| 95 | +assert unpacked == obj |
| 96 | +``` |
| 97 | + |
| 98 | +Use `ext_hook` to pack custom types as extensions, and `ext_hook` to decode them |
| 99 | +back: |
| 100 | + |
| 101 | +```python |
| 102 | +from msgpack_streams import ExtType, pack, unpack |
| 103 | +from fmtspec import decode, encode, types # https://pypi.org/project/fmtspec/ |
| 104 | + |
| 105 | +class Point: |
| 106 | + EXT_CODE = 10 |
| 107 | + |
| 108 | + __fmt__ = { |
| 109 | + "x": types.u32, |
| 110 | + "y": types.u32, |
| 111 | + } |
| 112 | + |
| 113 | + def __init__(self, x: int, y: int): |
| 114 | + self.x, self.y = x, y |
| 115 | + |
| 116 | +def unknown_type_hook(obj): |
| 117 | + if isinstance(obj, Point): |
| 118 | + return ExtType(Point.EXT_CODE, encode(obj)) |
| 119 | + return None # unsupported type → TypeError |
| 120 | + |
| 121 | +def ext_hook(ext): |
| 122 | + if ext.code == Point.EXT_CODE: |
| 123 | + return decode(ext.data, shape=Point) |
| 124 | + return None # unknown → keep as ExtType |
| 125 | + |
| 126 | +pt = Point(1, 2) |
| 127 | +packed = pack(pt, ext_hook=unknown_type_hook) |
| 128 | +result, _ = unpack(packed, ext_hook=ext_hook) |
| 129 | +assert result.x == pt.x and result.y == pt.y |
| 130 | +``` |
| 131 | + |
| 132 | +## API reference |
| 133 | + |
| 134 | +```python |
| 135 | +def pack(obj: object, *, float32: bool = False, ext_hook: Callable[[object], ExtType | None] | None = None) -> bytes: |
| 136 | + ... |
| 137 | +``` |
| 138 | + |
| 139 | +Serialize `obj` to a `bytes` object. Pass `float32=True` to encode `float` |
| 140 | +values as 32-bit instead of the default 64-bit. |
| 141 | + |
| 142 | +Pass `ext_hook` to handle types that are not natively supported. The callback |
| 143 | +receives the unsupported object and should return an `ExtType` to pack in its |
| 144 | +place. If it returns `None` a `TypeError` is raised as normal. |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +```python |
| 149 | +def unpack(data: bytes, *, ext_hook: Callable[[ExtType], object | None] | None = None) -> tuple[object, bytes]: |
| 150 | + ... |
| 151 | +``` |
| 152 | + |
| 153 | +Deserialize the first msgpack object from `data`. Returns `(obj, excess)` where |
| 154 | +`excess` is any unconsumed bytes that followed the object. |
| 155 | + |
| 156 | +Pass `ext_hook` to convert `ExtType` values during decoding. The callback |
| 157 | +receives each `ExtType` and should return the decoded object, or `None` to leave |
| 158 | +it as an `ExtType`. |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +```python |
| 163 | +def pack_stream(stream: BinaryIO, obj: object, *, float32: bool = False, ext_hook: Callable[[object], ExtType | None] | None = None) -> None: |
| 164 | + ... |
| 165 | +``` |
| 166 | + |
| 167 | +Serialize `obj` directly into a binary stream. Pass `float32=True` to encode |
| 168 | +`float` values as 32-bit instead of the default 64-bit. |
| 169 | + |
| 170 | +Pass `ext_hook` to handle types that are not natively supported. The callback |
| 171 | +receives the unsupported object and should return an `ExtType` to pack in its |
| 172 | +place. If it returns `None` a `TypeError` is raised as normal. |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +```python |
| 177 | +def unpack_stream(stream: BinaryIO, *, ext_hook: Callable[[ExtType], object] | None = None) -> object: |
| 178 | + ... |
| 179 | +``` |
| 180 | + |
| 181 | +Deserialize a single msgpack object from a binary stream, advancing the stream |
| 182 | +position past the consumed bytes. |
| 183 | + |
| 184 | +Pass `ext_hook` to convert `ExtType` values during decoding. The callback |
| 185 | +receives each `ExtType` and should return the decoded object, or `None` to leave |
| 186 | +it as an `ExtType`. |
0 commit comments