1
- ======================
2
- MessagePack for Python
3
- ======================
1
+ # MessagePack for Python
4
2
5
- .. image :: https://travis-ci.org/msgpack/msgpack-python.svg?branch=master
6
- :target: https://travis-ci.org/msgpack/msgpack-python
7
- :alt: Build Status
3
+ [ ![ Build Status] ( https://travis-ci.org/msgpack/msgpack-python.svg?branch=master )] ( https://travis-ci.org/msgpack/msgpack-python )
4
+ [ ![ Documentation Status] ( https://readthedocs.org/projects/msgpack-python/badge/?version=latest )] ( https://msgpack-python.readthedocs.io/en/latest/?badge=latest )
8
5
9
- .. image :: https://readthedocs.org/projects/msgpack-python/badge/?version=latest
10
- :target: https://msgpack-python.readthedocs.io/en/latest/?badge=latest
11
- :alt: Documentation Status
12
-
13
-
14
- What's this
15
- -----------
6
+ ## What's this
16
7
17
8
` MessagePack <https://msgpack.org/> ` _ is an efficient binary serialization format.
18
9
It lets you exchange data among multiple languages like JSON.
19
10
But it's faster and smaller.
20
11
This package provides CPython bindings for reading and writing MessagePack data.
21
12
22
13
23
- Very important notes for existing users
24
- ---------------------------------------
14
+ ## Very important notes for existing users
25
15
26
- PyPI package name
27
- ^^^^^^^^^^^^^^^^^
16
+ ### PyPI package name
28
17
29
18
TL;DR: When upgrading from msgpack-0.4 or earlier, don't do ` pip install -U msgpack-python ` .
30
19
Do ` pip uninstall msgpack-python; pip install msgpack ` instead.
@@ -37,8 +26,7 @@ Sadly, this doesn't work for upgrade install. After `pip install -U msgpack-pyt
37
26
msgpack is removed, and ` import msgpack ` fail.
38
27
39
28
40
- Compatibility with the old format
41
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29
+ ### Compatibility with the old format
42
30
43
31
You can use `` use_bin_type=False `` option to pack `` bytes ``
44
32
object into raw type in the old msgpack spec, instead of bin type in new msgpack spec.
@@ -49,8 +37,7 @@ It unpacks str (raw) type in msgpack into Python bytes.
49
37
See note below for detail.
50
38
51
39
52
- Major breaking changes in msgpack 1.0
53
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40
+ ### Major breaking changes in msgpack 1.0
54
41
55
42
* Python 2
56
43
@@ -75,16 +62,13 @@ Major breaking changes in msgpack 1.0
75
62
which type is not bytes or str.
76
63
77
64
78
- Install
79
- -------
65
+ ## Install
80
66
81
- ::
82
67
83
68
$ pip install msgpack
84
69
85
70
86
- Pure Python implementation
87
- ^^^^^^^^^^^^^^^^^^^^^^^^^^
71
+ ### Pure Python implementation
88
72
89
73
The extension module in msgpack (`` msgpack._cmsgpack `` ) does not support
90
74
Python 2 and PyPy.
@@ -96,26 +80,20 @@ Since the [pip](https://pip.pypa.io/) uses the pure Python implementation,
96
80
Python 2 support will not be dropped in the foreseeable future.
97
81
98
82
99
- Windows
100
- ^^^^^^^
83
+ ### Windows
101
84
102
85
When you can't use a binary distribution, you need to install Visual Studio
103
86
or Windows SDK on Windows.
104
87
Without extension, using pure Python implementation on CPython runs slowly.
105
88
106
89
107
- How to use
108
- ----------
90
+ ## How to use
109
91
110
- .. note ::
92
+ NOTE: In examples below, I use `` raw=False `` and `` use_bin_type=True `` for users
93
+ using msgpack < 1.0. These options are default from msgpack 1.0 so you can omit them.
111
94
112
- In examples below, I use ``raw=False `` and ``use_bin_type=True `` for users
113
- using msgpack < 1.0.
114
- These options are default from msgpack 1.0 so you can omit them.
115
95
116
-
117
- One-shot pack & unpack
118
- ^^^^^^^^^^^^^^^^^^^^^^
96
+ ### One-shot pack & unpack
119
97
120
98
Use `` packb `` for packing and `` unpackb `` for unpacking.
121
99
msgpack provides `` dumps `` and `` loads `` as an alias for compatibility with
@@ -124,35 +102,33 @@ msgpack provides ``dumps`` and ``loads`` as an alias for compatibility with
124
102
`` pack `` and `` dump `` packs to a file-like object.
125
103
`` unpack `` and `` load `` unpacks from a file-like object.
126
104
127
- .. code-block :: pycon
128
-
105
+ ``` pycon
129
106
>>> import msgpack
130
107
>>> msgpack.packb([1, 2, 3], use_bin_type=True)
131
108
'\x93\x01\x02\x03'
132
109
>>> msgpack.unpackb(_, raw=False)
133
110
[1, 2, 3]
111
+ ```
134
112
135
113
`` unpack `` unpacks msgpack's array to Python's list, but can also unpack to tuple:
136
114
137
- .. code-block :: pycon
138
-
115
+ ``` pycon
139
116
>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
140
117
(1, 2, 3)
118
+ ```
141
119
142
120
You should always specify the `` use_list `` keyword argument for backward compatibility.
143
121
See performance issues relating to ` use_list option ` _ below.
144
122
145
123
Read the docstring for other options.
146
124
147
125
148
- Streaming unpacking
149
- ^^^^^^^^^^^^^^^^^^^
126
+ ### Streaming unpacking
150
127
151
128
`` Unpacker `` is a "streaming unpacker". It unpacks multiple objects from one
152
129
stream (or from bytes provided through its `` feed `` method).
153
130
154
- .. code-block :: python
155
-
131
+ ``` py
156
132
import msgpack
157
133
from io import BytesIO
158
134
@@ -165,16 +141,15 @@ stream (or from bytes provided through its ``feed`` method).
165
141
unpacker = msgpack.Unpacker(buf, raw = False )
166
142
for unpacked in unpacker:
167
143
print (unpacked)
144
+ ```
168
145
169
146
170
- Packing/unpacking of custom data type
171
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147
+ ### Packing/unpacking of custom data type
172
148
173
149
It is also possible to pack/unpack custom data types. Here is an example for
174
150
`` datetime.datetime `` .
175
151
176
- .. code-block :: python
177
-
152
+ ``` py
178
153
import datetime
179
154
import msgpack
180
155
@@ -196,19 +171,18 @@ It is also possible to pack/unpack custom data types. Here is an example for
196
171
197
172
packed_dict = msgpack.packb(useful_dict, default = encode_datetime, use_bin_type = True )
198
173
this_dict_again = msgpack.unpackb(packed_dict, object_hook = decode_datetime, raw = False )
174
+ ```
199
175
200
176
`` Unpacker `` 's `` object_hook `` callback receives a dict; the
201
177
`` object_pairs_hook `` callback may instead be used to receive a list of
202
178
key-value pairs.
203
179
204
180
205
- Extended types
206
- ^^^^^^^^^^^^^^
181
+ ### Extended types
207
182
208
183
It is also possible to pack/unpack custom data types using the ** ext** type.
209
184
210
- .. code-block :: pycon
211
-
185
+ ``` pycon
212
186
>>> import msgpack
213
187
>>> import array
214
188
>>> def default(obj):
@@ -228,10 +202,10 @@ It is also possible to pack/unpack custom data types using the **ext** type.
228
202
>>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
229
203
>>> data == unpacked
230
204
True
205
+ ```
231
206
232
207
233
- Advanced unpacking control
234
- ^^^^^^^^^^^^^^^^^^^^^^^^^^
208
+ ### Advanced unpacking control
235
209
236
210
As an alternative to iteration, `` Unpacker `` objects provide `` unpack `` ,
237
211
`` skip `` , `` read_array_header `` and `` read_map_header `` methods. The former two
@@ -243,8 +217,7 @@ in a map, can be unpacked or skipped individually.
243
217
Each of these methods may optionally write the packed data it reads to a
244
218
callback function:
245
219
246
- .. code-block :: python
247
-
220
+ ``` py
248
221
from io import BytesIO
249
222
250
223
def distribute (unpacker , get_worker ):
@@ -258,46 +231,41 @@ callback function:
258
231
bytestream = BytesIO()
259
232
unpacker.skip(bytestream.write)
260
233
worker.send(bytestream.getvalue())
234
+ ```
261
235
236
+ ## Notes
262
237
263
- Notes
264
- -----
265
-
266
- string and binary type
267
- ^^^^^^^^^^^^^^^^^^^^^^
238
+ ### string and binary type
268
239
269
240
Early versions of msgpack didn't distinguish string and binary types.
270
241
The type for representing both string and binary types was named ** raw** .
271
242
272
243
You can pack into and unpack from this old spec using `` use_bin_type=False ``
273
244
and `` raw=True `` options.
274
245
275
- .. code-block :: pycon
276
-
246
+ ``` pycon
277
247
>>> import msgpack
278
248
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=False), raw=True)
279
249
[b'spam', b'eggs']
280
250
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True), raw=False)
281
251
[b'spam', 'eggs']
252
+ ```
282
253
283
-
284
- ext type
285
- ^^^^^^^^
254
+ ### ext type
286
255
287
256
To use the ** ext** type, pass `` msgpack.ExtType `` object to packer.
288
257
289
- .. code-block :: pycon
290
-
258
+ ``` pycon
291
259
>>> import msgpack
292
260
>>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
293
261
>>> msgpack.unpackb(packed)
294
262
ExtType(code=42, data='xyzzy')
263
+ ```
295
264
296
265
You can use it with `` default `` and `` ext_hook `` . See below.
297
266
298
267
299
- Security
300
- ^^^^^^^^
268
+ ### Security
301
269
302
270
To unpacking data received from unreliable source, msgpack provides
303
271
two security options.
@@ -311,8 +279,7 @@ there is a risk of the hashdos.
311
279
If you need to support other types for map keys, use `` strict_map_key=False `` .
312
280
313
281
314
- Performance tips
315
- ^^^^^^^^^^^^^^^^
282
+ ### Performance tips
316
283
317
284
CPython's GC starts when growing allocated object.
318
285
This means unpacking may cause useless GC.
@@ -323,17 +290,13 @@ But tuple is lighter than list.
323
290
You can use `` use_list=False `` while unpacking when performance is important.
324
291
325
292
326
- Development
327
- -----------
293
+ ## Development
328
294
329
- Test
330
- ^^^^
295
+ ### Test
331
296
332
297
MessagePack uses ` pytest ` for testing.
333
298
Run test with following command:
334
299
300
+ ```
335
301
$ make test
336
-
337
-
338
- ..
339
- vim: filetype=rst
302
+ ```
0 commit comments