METADATA 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. Metadata-Version: 2.1
  2. Name: msgpack
  3. Version: 0.6.1
  4. Summary: MessagePack (de)serializer.
  5. Home-page: https://msgpack.org/
  6. Author: INADA Naoki
  7. Author-email: songofacandy@gmail.com
  8. License: Apache 2.0
  9. Project-URL: Source, https://github.com/msgpack/msgpack-python
  10. Project-URL: Documentation, https://msgpack-python.readthedocs.io/
  11. Project-URL: Tracker, https://github.com/msgpack/msgpack-python/issues
  12. Platform: UNKNOWN
  13. Classifier: Programming Language :: Python :: 2
  14. Classifier: Programming Language :: Python :: 2.7
  15. Classifier: Programming Language :: Python :: 3
  16. Classifier: Programming Language :: Python :: 3.5
  17. Classifier: Programming Language :: Python :: 3.6
  18. Classifier: Programming Language :: Python :: 3.7
  19. Classifier: Programming Language :: Python :: Implementation :: CPython
  20. Classifier: Programming Language :: Python :: Implementation :: PyPy
  21. Classifier: Intended Audience :: Developers
  22. Classifier: License :: OSI Approved :: Apache Software License
  23. Description-Content-Type: text/x-rst
  24. ======================
  25. MessagePack for Python
  26. ======================
  27. .. image:: https://travis-ci.org/msgpack/msgpack-python.svg?branch=master
  28. :target: https://travis-ci.org/msgpack/msgpack-python
  29. :alt: Build Status
  30. .. image:: https://readthedocs.org/projects/msgpack-python/badge/?version=latest
  31. :target: https://msgpack-python.readthedocs.io/en/latest/?badge=latest
  32. :alt: Documentation Status
  33. What's this
  34. -----------
  35. `MessagePack <https://msgpack.org/>`_ is an efficient binary serialization format.
  36. It lets you exchange data among multiple languages like JSON.
  37. But it's faster and smaller.
  38. This package provides CPython bindings for reading and writing MessagePack data.
  39. Very important notes for existing users
  40. ---------------------------------------
  41. PyPI package name
  42. ^^^^^^^^^^^^^^^^^
  43. TL;DR: When upgrading from msgpack-0.4 or earlier, don't do `pip install -U msgpack-python`.
  44. Do `pip uninstall msgpack-python; pip install msgpack` instead.
  45. Package name on PyPI was changed to msgpack from 0.5.
  46. I upload transitional package (msgpack-python 0.5 which depending on msgpack)
  47. for smooth transition from msgpack-python to msgpack.
  48. Sadly, this doesn't work for upgrade install. After `pip install -U msgpack-python`,
  49. msgpack is removed and `import msgpack` fail.
  50. Deprecating encoding option
  51. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  52. encoding and unicode_errors options are deprecated.
  53. In case of packer, use UTF-8 always. Storing other than UTF-8 is not recommended.
  54. For backward compatibility, you can use ``use_bin_type=False`` and pack ``bytes``
  55. object into msgpack raw type.
  56. In case of unpacker, there is new ``raw`` option. It is ``True`` by default
  57. for backward compatibility, but it is changed to ``False`` in near future.
  58. You can use ``raw=False`` instead of ``encoding='utf-8'``.
  59. Planned backward incompatible changes
  60. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  61. When msgpack 1.0, I planning these breaking changes:
  62. * packer and unpacker: Remove ``encoding`` and ``unicode_errors`` option.
  63. * packer: Change default of ``use_bin_type`` option from False to True.
  64. * unpacker: Change default of ``raw`` option from True to False.
  65. * unpacker: Reduce all ``max_xxx_len`` options for typical usage.
  66. * unpacker: Remove ``write_bytes`` option from all methods.
  67. To avoid these breaking changes breaks your application, please:
  68. * Don't use deprecated options.
  69. * Pass ``use_bin_type`` and ``raw`` options explicitly.
  70. * If your application handle large (>1MB) data, specify ``max_xxx_len`` options too.
  71. Install
  72. -------
  73. ::
  74. $ pip install msgpack
  75. PyPy
  76. ^^^^
  77. msgpack provides a pure Python implementation. PyPy can use this.
  78. Windows
  79. ^^^^^^^
  80. When you can't use a binary distribution, you need to install Visual Studio
  81. or Windows SDK on Windows.
  82. Without extension, using pure Python implementation on CPython runs slowly.
  83. For Python 2.7, `Microsoft Visual C++ Compiler for Python 2.7 <https://www.microsoft.com/en-us/download/details.aspx?id=44266>`_
  84. is recommended solution.
  85. For Python 3.5, `Microsoft Visual Studio 2015 <https://www.visualstudio.com/en-us/products/vs-2015-product-editions.aspx>`_
  86. Community Edition or Express Edition can be used to build extension module.
  87. How to use
  88. ----------
  89. One-shot pack & unpack
  90. ^^^^^^^^^^^^^^^^^^^^^^
  91. Use ``packb`` for packing and ``unpackb`` for unpacking.
  92. msgpack provides ``dumps`` and ``loads`` as an alias for compatibility with
  93. ``json`` and ``pickle``.
  94. ``pack`` and ``dump`` packs to a file-like object.
  95. ``unpack`` and ``load`` unpacks from a file-like object.
  96. .. code-block:: pycon
  97. >>> import msgpack
  98. >>> msgpack.packb([1, 2, 3], use_bin_type=True)
  99. '\x93\x01\x02\x03'
  100. >>> msgpack.unpackb(_, raw=False)
  101. [1, 2, 3]
  102. ``unpack`` unpacks msgpack's array to Python's list, but can also unpack to tuple:
  103. .. code-block:: pycon
  104. >>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
  105. (1, 2, 3)
  106. You should always specify the ``use_list`` keyword argument for backward compatibility.
  107. See performance issues relating to `use_list option`_ below.
  108. Read the docstring for other options.
  109. Streaming unpacking
  110. ^^^^^^^^^^^^^^^^^^^
  111. ``Unpacker`` is a "streaming unpacker". It unpacks multiple objects from one
  112. stream (or from bytes provided through its ``feed`` method).
  113. .. code-block:: python
  114. import msgpack
  115. from io import BytesIO
  116. buf = BytesIO()
  117. for i in range(100):
  118. buf.write(msgpack.packb(i, use_bin_type=True))
  119. buf.seek(0)
  120. unpacker = msgpack.Unpacker(buf, raw=False)
  121. for unpacked in unpacker:
  122. print(unpacked)
  123. Packing/unpacking of custom data type
  124. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  125. It is also possible to pack/unpack custom data types. Here is an example for
  126. ``datetime.datetime``.
  127. .. code-block:: python
  128. import datetime
  129. import msgpack
  130. useful_dict = {
  131. "id": 1,
  132. "created": datetime.datetime.now(),
  133. }
  134. def decode_datetime(obj):
  135. if b'__datetime__' in obj:
  136. obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
  137. return obj
  138. def encode_datetime(obj):
  139. if isinstance(obj, datetime.datetime):
  140. return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
  141. return obj
  142. packed_dict = msgpack.packb(useful_dict, default=encode_datetime, use_bin_type=True)
  143. this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime, raw=False)
  144. ``Unpacker``'s ``object_hook`` callback receives a dict; the
  145. ``object_pairs_hook`` callback may instead be used to receive a list of
  146. key-value pairs.
  147. Extended types
  148. ^^^^^^^^^^^^^^
  149. It is also possible to pack/unpack custom data types using the **ext** type.
  150. .. code-block:: pycon
  151. >>> import msgpack
  152. >>> import array
  153. >>> def default(obj):
  154. ... if isinstance(obj, array.array) and obj.typecode == 'd':
  155. ... return msgpack.ExtType(42, obj.tostring())
  156. ... raise TypeError("Unknown type: %r" % (obj,))
  157. ...
  158. >>> def ext_hook(code, data):
  159. ... if code == 42:
  160. ... a = array.array('d')
  161. ... a.fromstring(data)
  162. ... return a
  163. ... return ExtType(code, data)
  164. ...
  165. >>> data = array.array('d', [1.2, 3.4])
  166. >>> packed = msgpack.packb(data, default=default, use_bin_type=True)
  167. >>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
  168. >>> data == unpacked
  169. True
  170. Advanced unpacking control
  171. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  172. As an alternative to iteration, ``Unpacker`` objects provide ``unpack``,
  173. ``skip``, ``read_array_header`` and ``read_map_header`` methods. The former two
  174. read an entire message from the stream, respectively de-serialising and returning
  175. the result, or ignoring it. The latter two methods return the number of elements
  176. in the upcoming container, so that each element in an array, or key-value pair
  177. in a map, can be unpacked or skipped individually.
  178. Each of these methods may optionally write the packed data it reads to a
  179. callback function:
  180. .. code-block:: python
  181. from io import BytesIO
  182. def distribute(unpacker, get_worker):
  183. nelems = unpacker.read_map_header()
  184. for i in range(nelems):
  185. # Select a worker for the given key
  186. key = unpacker.unpack()
  187. worker = get_worker(key)
  188. # Send the value as a packed message to worker
  189. bytestream = BytesIO()
  190. unpacker.skip(bytestream.write)
  191. worker.send(bytestream.getvalue())
  192. Notes
  193. -----
  194. string and binary type
  195. ^^^^^^^^^^^^^^^^^^^^^^
  196. Early versions of msgpack didn't distinguish string and binary types (like Python 1).
  197. The type for representing both string and binary types was named **raw**.
  198. For backward compatibility reasons, msgpack-python will still default all
  199. strings to byte strings, unless you specify the ``use_bin_type=True`` option in
  200. the packer. If you do so, it will use a non-standard type called **bin** to
  201. serialize byte arrays, and **raw** becomes to mean **str**. If you want to
  202. distinguish **bin** and **raw** in the unpacker, specify ``raw=False``.
  203. Note that Python 2 defaults to byte-arrays over Unicode strings:
  204. .. code-block:: pycon
  205. >>> import msgpack
  206. >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
  207. ['spam', 'eggs']
  208. >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
  209. raw=False)
  210. ['spam', u'eggs']
  211. This is the same code in Python 3 (same behaviour, but Python 3 has a
  212. different default):
  213. .. code-block:: pycon
  214. >>> import msgpack
  215. >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
  216. [b'spam', b'eggs']
  217. >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
  218. raw=False)
  219. [b'spam', 'eggs']
  220. ext type
  221. ^^^^^^^^
  222. To use the **ext** type, pass ``msgpack.ExtType`` object to packer.
  223. .. code-block:: pycon
  224. >>> import msgpack
  225. >>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
  226. >>> msgpack.unpackb(packed)
  227. ExtType(code=42, data='xyzzy')
  228. You can use it with ``default`` and ``ext_hook``. See below.
  229. Note about performance
  230. ----------------------
  231. GC
  232. ^^
  233. CPython's GC starts when growing allocated object.
  234. This means unpacking may cause useless GC.
  235. You can use ``gc.disable()`` when unpacking large message.
  236. use_list option
  237. ^^^^^^^^^^^^^^^
  238. List is the default sequence type of Python.
  239. But tuple is lighter than list.
  240. You can use ``use_list=False`` while unpacking when performance is important.
  241. Python's dict can't use list as key and MessagePack allows array for key of mapping.
  242. ``use_list=False`` allows unpacking such message.
  243. Another way to unpacking such object is using ``object_pairs_hook``.
  244. Development
  245. -----------
  246. Test
  247. ^^^^
  248. MessagePack uses `pytest` for testing.
  249. Run test with following command:
  250. $ make test
  251. ..
  252. vim: filetype=rst