@@ -9,6 +9,13 @@ MessagePack Python Binding
99.. image :: https://secure.travis-ci.org/msgpack/msgpack-python.png
1010 :target: https://travis-ci.org/#!/msgpack/msgpack-python
1111
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+
1219HOW TO USE
1320-----------
1421
@@ -35,14 +42,14 @@ To unpack it to list, Use ``use_list`` option.
3542 >>> msgpack.unpackb(b ' \x93\x01\x02\x03 ' , use_list = True )
3643 [1, 2, 3]
3744
38- Read docstring for other options.
45+ Read the docstring for other options.
3946
4047
4148streaming unpacking
4249^^^^^^^^^^^^^^^^^^^
4350
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) .
4653
4754::
4855
@@ -55,20 +62,15 @@ stream.
5562
5663 buf.seek(0)
5764
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
6468
65- for unpacked in unpacker:
66- print unpacked
6769
6870packing/unpacking of custom data type
6971^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7072
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
7274``datetime.datetime ``.
7375
7476::
@@ -96,6 +98,39 @@ Also possible to pack/unpack user's data types. Here is an example for
9698 packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
9799 this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)
98100
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())
99134
100135INSTALL
101136---------
0 commit comments