mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-11-06 12:30:58 +00:00
Merge branch 'master' of github.com:msgpack/msgpack-python
This commit is contained in:
commit
a1577a8838
1 changed files with 47 additions and 12 deletions
57
README.rst
57
README.rst
|
|
@ -9,6 +9,13 @@ MessagePack Python Binding
|
||||||
.. image:: https://secure.travis-ci.org/msgpack/msgpack-python.png
|
.. image:: https://secure.travis-ci.org/msgpack/msgpack-python.png
|
||||||
:target: https://travis-ci.org/#!/msgpack/msgpack-python
|
:target: https://travis-ci.org/#!/msgpack/msgpack-python
|
||||||
|
|
||||||
|
WHAT IT IS
|
||||||
|
----------
|
||||||
|
|
||||||
|
`MessagePack <http://msgpack.org/>`_ 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
|
HOW TO USE
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
|
@ -35,14 +42,14 @@ To unpack it to list, Use ``use_list`` option.
|
||||||
>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=True)
|
>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=True)
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
|
|
||||||
Read docstring for other options.
|
Read the docstring for other options.
|
||||||
|
|
||||||
|
|
||||||
streaming unpacking
|
streaming unpacking
|
||||||
^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
``Unpacker`` is "streaming unpacker". It unpacks multiple objects from one
|
``Unpacker`` is a "streaming unpacker". It unpacks multiple objects from one
|
||||||
stream.
|
stream (or from bytes provided through its ``feed`` method).
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
|
@ -55,20 +62,15 @@ stream.
|
||||||
|
|
||||||
buf.seek(0)
|
buf.seek(0)
|
||||||
|
|
||||||
unpacker = msgpack.Unpacker()
|
unpacker = msgpack.Unpacker(buf)
|
||||||
while True:
|
|
||||||
data = buf.read(16)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
unpacker.feed(data)
|
|
||||||
|
|
||||||
for unpacked in unpacker:
|
for unpacked in unpacker:
|
||||||
print unpacked
|
print unpacked
|
||||||
|
|
||||||
|
|
||||||
packing/unpacking of custom data type
|
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``.
|
``datetime.datetime``.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
@ -96,6 +98,39 @@ Also possible to pack/unpack user's data types. Here is an example for
|
||||||
packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
|
packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
|
||||||
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_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.
|
||||||
|
|
||||||
|
|
||||||
|
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
|
INSTALL
|
||||||
---------
|
---------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue