From caecc0098eeb631bb0aeca1fdba397ddd96ca329 Mon Sep 17 00:00:00 2001 From: Joel Nothman Date: Thu, 6 Dec 2012 23:01:12 +1100 Subject: Change Unpacker example to read from stream --- README.rst | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 8cebc01..8154098 100644 --- a/README.rst +++ b/README.rst @@ -55,15 +55,9 @@ stream. buf.seek(0) - unpacker = msgpack.Unpacker() - while True: - data = buf.read(16) - if not data: - break - unpacker.feed(data) - - for unpacked in unpacker: - print unpacked + unpacker = msgpack.Unpacker(buf) + for unpacked in unpacker: + print unpacked packing/unpacking of custom data type ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- cgit v1.2.1 From c567ad1c5220a9da145d41b4e61f3a411d32ce57 Mon Sep 17 00:00:00 2001 From: Joel Nothman Date: Thu, 6 Dec 2012 23:10:25 +1100 Subject: Describe object_pairs_hook in README --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index 8154098..f3779f1 100644 --- a/README.rst +++ b/README.rst @@ -90,6 +90,9 @@ Also possible to pack/unpack user's data types. Here is an example for packed_dict = msgpack.packb(useful_dict, default=encode_datetime) this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime) +``Unpacker``'s ``object_hook`` callback receives a dict; the +``object_pairs_hook`` callback may instead be used to receive a list of +key-value pairs. INSTALL --------- -- cgit v1.2.1 From de3724c1de8b0320e6cab3736404ab9857b7a951 Mon Sep 17 00:00:00 2001 From: Joel Nothman Date: Thu, 6 Dec 2012 23:34:18 +1100 Subject: README documentation of advanced Unpacker features --- README.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.rst b/README.rst index f3779f1..3f5e823 100644 --- a/README.rst +++ b/README.rst @@ -94,6 +94,36 @@ Also possible to pack/unpack user's data types. Here is an example for ``object_pairs_hook`` callback may instead be used to receive a list of key-value pairs. + +advanced unpacking control +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +As an alternative to iteration, ``Unpacker`` objects provide ``unpack``, +``skip``, ``read_array_header`` and ``read_map_header`` methods. The former two +read an entire message from the stream, respectively deserialising and returning +the result, or ignoring it. The latter two methods return the number of elements +in the upcoming container, so that each element in an array, or key-value pair +in a map, can be unpacked or skipped individually. + +Each of these methods may optionally write the packed data it reads to a +callback function: + +:: + + from io import BytesIO + + def distribute(unpacker, get_worker): + nelems = unpacker.read_map_header() + for i in range(nelems): + # Select a worker for the given key + key = unpacker.unpack() + worker = get_worker(key) + + # Send the value as a packed message to worker + bytestream = BytesIO() + unpacker.skip(bytestream.write) + worker.send(bytestream.getvalue()) + INSTALL --------- You can use ``pip`` or ``easy_install`` to install msgpack:: -- cgit v1.2.1 From 6d4115f64bb180f34161b8a517c9c2165af81deb Mon Sep 17 00:00:00 2001 From: Joel Nothman Date: Thu, 6 Dec 2012 23:36:16 +1100 Subject: Minor grammar fixes --- README.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 3f5e823..9a437eb 100644 --- a/README.rst +++ b/README.rst @@ -35,13 +35,13 @@ To unpack it to list, Use ``use_list`` option. >>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=True) [1, 2, 3] -Read docstring for other options. +Read the docstring for other options. streaming unpacking ^^^^^^^^^^^^^^^^^^^ -``Unpacker`` is "streaming unpacker". It unpacks multiple objects from one +``Unpacker`` is a "streaming unpacker". It unpacks multiple objects from one stream. :: @@ -59,10 +59,11 @@ stream. for unpacked in unpacker: print unpacked + packing/unpacking of custom data type ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Also possible to pack/unpack user's data types. Here is an example for +It is also possible to pack/unpack custom data types. Here is an example for ``datetime.datetime``. :: -- cgit v1.2.1 From 659d0961a319bbaabf994a972b06022c99e4465c Mon Sep 17 00:00:00 2001 From: Joel Nothman Date: Thu, 6 Dec 2012 23:36:46 +1100 Subject: Brief mention of Unpacker.feed in README --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 9a437eb..1f5b1a2 100644 --- a/README.rst +++ b/README.rst @@ -42,7 +42,7 @@ streaming unpacking ^^^^^^^^^^^^^^^^^^^ ``Unpacker`` is a "streaming unpacker". It unpacks multiple objects from one -stream. +stream (or from bytes provided through its ``feed`` method). :: -- cgit v1.2.1 From fc41ed606d06becae4b53c8614b74768ae755cba Mon Sep 17 00:00:00 2001 From: Joel Nothman Date: Thu, 6 Dec 2012 23:44:27 +1100 Subject: Mention CPython and MessagePack in README --- README.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.rst b/README.rst index 1f5b1a2..5264ce9 100644 --- a/README.rst +++ b/README.rst @@ -9,6 +9,13 @@ MessagePack Python Binding .. image:: https://secure.travis-ci.org/msgpack/msgpack-python.png :target: https://travis-ci.org/#!/msgpack/msgpack-python +WHAT IT IS +---------- + +`MessagePack `_ is a fast, compact binary serialization format, suitable for +similar data to JSON. This package provides CPython bindings for reading and +writing MessagePack data. + HOW TO USE ----------- -- cgit v1.2.1