File: advanced.rst

package info (click to toggle)
python-msgpack 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 460 kB
  • sloc: python: 2,041; ansic: 1,431; makefile: 190; sh: 36
file content (32 lines) | stat: -rw-r--r-- 785 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Advanced usage
===============

Packer
------

autoreset
~~~~~~~~~

When you used ``autoreset=False`` option of :class:`~msgpack.Packer`, 
``pack()`` method doesn't return packed ``bytes``.

You can use :meth:`~msgpack.Packer.bytes` or :meth:`~msgpack.Packer.getbuffer` to
get packed data.

``bytes()`` returns ``bytes`` object.  ``getbuffer()`` returns some bytes-like
object.  It's concrete type is implement detail and it will be changed in future
versions.

You can reduce temporary bytes object by using ``Unpacker.getbuffer()``.

.. code-block:: python

    packer = Packer(use_bin_type=True, autoreset=False)

    packer.pack([1, 2])
    packer.pack([3, 4])

    with open('data.bin', 'wb') as f:
        f.write(packer.getbuffer())

    packer.reset()  # reset internal buffer