@@ -9,6 +9,13 @@ MessagePack Python Binding
9
9
.. image :: https://secure.travis-ci.org/msgpack/msgpack-python.png
10
10
:target: https://travis-ci.org/#!/msgpack/msgpack-python
11
11
12
+ WHAT IT IS
13
+ ----------
14
+
15
+ `MessagePack <http://msgpack.org/ >`_ is a fast, compact binary serialization format, suitable for
16
+ similar data to JSON. This package provides CPython bindings for reading and
17
+ writing MessagePack data.
18
+
12
19
HOW TO USE
13
20
-----------
14
21
@@ -35,14 +42,14 @@ To unpack it to list, Use ``use_list`` option.
35
42
>>> msgpack.unpackb(b ' \x93\x01\x02\x03 ' , use_list = True )
36
43
[1, 2, 3]
37
44
38
- Read docstring for other options.
45
+ Read the docstring for other options.
39
46
40
47
41
48
streaming unpacking
42
49
^^^^^^^^^^^^^^^^^^^
43
50
44
- ``Unpacker `` is "streaming unpacker". It unpacks multiple objects from one
45
- stream.
51
+ ``Unpacker `` is a "streaming unpacker". It unpacks multiple objects from one
52
+ stream (or from bytes provided through its `` feed `` method) .
46
53
47
54
::
48
55
@@ -55,20 +62,15 @@ stream.
55
62
56
63
buf.seek(0)
57
64
58
- unpacker = msgpack.Unpacker()
59
- while True:
60
- data = buf.read(16)
61
- if not data:
62
- break
63
- unpacker.feed(data)
65
+ unpacker = msgpack.Unpacker(buf)
66
+ for unpacked in unpacker:
67
+ print unpacked
64
68
65
- for unpacked in unpacker:
66
- print unpacked
67
69
68
70
packing/unpacking of custom data type
69
71
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70
72
71
- Also possible to pack/unpack user's data types. Here is an example for
73
+ It is also possible to pack/unpack custom data types. Here is an example for
72
74
``datetime.datetime ``.
73
75
74
76
::
@@ -96,6 +98,39 @@ Also possible to pack/unpack user's data types. Here is an example for
96
98
packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
97
99
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)
98
100
101
+ ``Unpacker ``'s ``object_hook `` callback receives a dict; the
102
+ ``object_pairs_hook `` callback may instead be used to receive a list of
103
+ key-value pairs.
104
+
105
+
106
+ advanced unpacking control
107
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
108
+
109
+ As an alternative to iteration, ``Unpacker `` objects provide ``unpack ``,
110
+ ``skip ``, ``read_array_header `` and ``read_map_header `` methods. The former two
111
+ read an entire message from the stream, respectively deserialising and returning
112
+ the result, or ignoring it. The latter two methods return the number of elements
113
+ in the upcoming container, so that each element in an array, or key-value pair
114
+ in a map, can be unpacked or skipped individually.
115
+
116
+ Each of these methods may optionally write the packed data it reads to a
117
+ callback function:
118
+
119
+ ::
120
+
121
+ from io import BytesIO
122
+
123
+ def distribute(unpacker, get_worker):
124
+ nelems = unpacker.read_map_header()
125
+ for i in range(nelems):
126
+ # Select a worker for the given key
127
+ key = unpacker.unpack()
128
+ worker = get_worker(key)
129
+
130
+ # Send the value as a packed message to worker
131
+ bytestream = BytesIO()
132
+ unpacker.skip(bytestream.write)
133
+ worker.send(bytestream.getvalue())
99
134
100
135
INSTALL
101
136
---------
0 commit comments