Skip to main content

MessagePack (de)serializer.

Project description

Author:

INADA Naoki

Version:
0.3.0
Date:
2012-12-07
https://secure.travis-ci.org/msgpack/msgpack-python.png

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.

NOTE for msgpack 0.2.x users

The msgpack 0.3 have some incompatible changes.

The default value of use_list keyword argument is True from 0.3.x. You should pass the argument explicitly for backward compatibility.

Unpacker.unpack() and some unpack methods now raises OutOfData instead of StopIteration. StopIteration is used for iterator protocol only.

HOW TO USE

one-shot pack & unpack

Use packb for packing and unpackb for unpacking. msgpack provides dumps and loads as alias for compatibility with json and pickle.

pack and dump packs to file-like object. unpack and load unpacks from file-like object.

>>> import msgpack
>>> msgpack.packb([1, 2, 3])
'\x93\x01\x02\x03'
>>> msgpack.unpackb(_)
[1, 2, 3]

unpack unpacks msgpack’s array to Python’s list, but can unpack to tuple:

>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False)
(1, 2, 3)

You should always pass the use_list keyword argument. See performance issues relating to use_list below.

Read the docstring for other options.

streaming unpacking

Unpacker is a “streaming unpacker”. It unpacks multiple objects from one stream (or from bytes provided through its feed method).

import msgpack
from io import BytesIO

buf = BytesIO()
for i in range(100):
   buf.write(msgpack.packb(range(i)))

buf.seek(0)

unpacker = msgpack.Unpacker(buf)
for unpacked in unpacker:
    print unpacked

packing/unpacking of custom data type

It is also possible to pack/unpack custom data types. Here is an example for datetime.datetime.

import datetime

import msgpack

useful_dict = {
    "id": 1,
    "created": datetime.datetime.now(),
}

def decode_datetime(obj):
    if b'__datetime__' in obj:
        obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
    return obj

def encode_datetime(obj):
    if isinstance(obj, datetime.datetime):
        return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
    return obj


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.

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.

Warning: these methods raise StopIteration when called at the end of the stream. Unless caught, this may silently break an iteration.

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:

$ easy_install msgpack-python
  or
$ pip install msgpack-python

Windows

msgpack provides some binary distribution for Windows. You can install msgpack without compiler with them.

When you can’t use binary distribution, you need to install Visual Studio or Windows SDK on Windows. (NOTE: Visual C++ Express 2010 doesn’t support amd64. Windows SDK is recommanded way to build amd64 msgpack without any fee.)

PERFORMANCE NOTE

GC

CPython’s GC starts when growing allocated object. This means unpacking may cause useless GC. You can use gc.disable() when unpacking large message.

use_list

List is the default sequence type of Python. But tuple is lighter than list. You can use use_list=False while unpacking when performance is important.

Python’s dict can’t use list as key and MessagePack allows array for key of mapping. use_list=False allows unpacking such message. Another way to unpacking such object is using object_pairs_hook.

TEST

MessagePack uses nosetest for testing. Run test with following command:

$ nosetests test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msgpack-python-0.3.0.tar.gz (97.9 kB view details)

Uploaded Source

Built Distributions

msgpack_python-0.3.0-py3.3-win-amd64.egg (61.7 kB view details)

Uploaded Source

msgpack_python-0.3.0-py3.3-win32.egg (58.9 kB view details)

Uploaded Source

msgpack_python-0.3.0-py2.7-win-amd64.egg (61.4 kB view details)

Uploaded Source

msgpack_python-0.3.0-py2.7-win32.egg (57.4 kB view details)

Uploaded Source

msgpack-python-0.3.0.win-amd64-py3.3.exe (279.9 kB view details)

Uploaded Source

msgpack-python-0.3.0.win-amd64-py2.7.exe (281.7 kB view details)

Uploaded Source

msgpack-python-0.3.0.win32-py3.3.exe (245.9 kB view details)

Uploaded Source

msgpack-python-0.3.0.win32-py2.7.exe (250.1 kB view details)

Uploaded Source

File details

Details for the file msgpack-python-0.3.0.tar.gz.

File metadata

File hashes

Hashes for msgpack-python-0.3.0.tar.gz
Algorithm Hash digest
SHA256 fb6ed5144a9fe2b58d46f8f276417d48dd57f9eea2a94e0f51c6b04e7044f9b3
MD5 10dec96c90992b0f6e38bdf0cc5a8e79
BLAKE2b-256 eda608df2ee416908f13276bd570d1bb0fb65d7eedb8654797a9d6177897a9f5

See more details on using hashes here.

File details

Details for the file msgpack_python-0.3.0-py3.3-win-amd64.egg.

File metadata

File hashes

Hashes for msgpack_python-0.3.0-py3.3-win-amd64.egg
Algorithm Hash digest
SHA256 12782516d1a38e74dd78dc6e6b626d9ae41aaee2a8d2b851bf7435e2e9ae8256
MD5 9bcfca39afa8219d12048f97a5d2a2df
BLAKE2b-256 228abe7be9bb1f16a872754d7b476ab30a50bb19164b77b37763a3d5b064f147

See more details on using hashes here.

File details

Details for the file msgpack_python-0.3.0-py3.3-win32.egg.

File metadata

File hashes

Hashes for msgpack_python-0.3.0-py3.3-win32.egg
Algorithm Hash digest
SHA256 f609a7e667946da00743c0b42e90fdcf0bb15c8410ccc2cee08b681d1effae8f
MD5 b7032156929c5f424e583ec97a11521b
BLAKE2b-256 02e9ccb5d61ad21bf295b2d7addfdddec163192ad379da65e31b00bd3c3064f6

See more details on using hashes here.

File details

Details for the file msgpack_python-0.3.0-py2.7-win-amd64.egg.

File metadata

File hashes

Hashes for msgpack_python-0.3.0-py2.7-win-amd64.egg
Algorithm Hash digest
SHA256 330a7975d7171d6244f9414d78a67c085dd27ccba20e09defc9b425189b43e6e
MD5 b2015ad4316ddbd8688693542769413d
BLAKE2b-256 51eaeb9be756de26e9d74b2e7d2ffa8c00bcbff212fd919930c1c91a242c4baf

See more details on using hashes here.

File details

Details for the file msgpack_python-0.3.0-py2.7-win32.egg.

File metadata

File hashes

Hashes for msgpack_python-0.3.0-py2.7-win32.egg
Algorithm Hash digest
SHA256 7244a58eb526bb275d3fe7fa9ceb7b3dbe921f937eddfb7de4866eb5fe2cd6b2
MD5 d9fee3a6bb8aec510dbec5a55fbc3d16
BLAKE2b-256 4579f8a34ff6705a397424d7e9271cf862b065c9c85007b8c63effa5a1c214cb

See more details on using hashes here.

File details

Details for the file msgpack-python-0.3.0.win-amd64-py3.3.exe.

File metadata

File hashes

Hashes for msgpack-python-0.3.0.win-amd64-py3.3.exe
Algorithm Hash digest
SHA256 72a5db9d7a5eef63ae91ca33c80b5ce9d9c2d743e3d4eba233406da188f5e59e
MD5 327350979a89422556a94902fa982885
BLAKE2b-256 9acb23aa6f69468107999a42fc691ae1356180051009c072ec39d584b2056f9b

See more details on using hashes here.

File details

Details for the file msgpack-python-0.3.0.win-amd64-py2.7.exe.

File metadata

File hashes

Hashes for msgpack-python-0.3.0.win-amd64-py2.7.exe
Algorithm Hash digest
SHA256 b4f76350c5ac6e168c1a8a497635d1548b877a17bcf84571f178ce9b0d50b6fa
MD5 d482b1169ac997c1d34b817d3c85ccbd
BLAKE2b-256 fa82186a1d5e9c733182caaf0f165f329b2747982aab74b72eef53037e01d336

See more details on using hashes here.

File details

Details for the file msgpack-python-0.3.0.win32-py3.3.exe.

File metadata

File hashes

Hashes for msgpack-python-0.3.0.win32-py3.3.exe
Algorithm Hash digest
SHA256 71bffbdf11a48da36e342cda7f8eed1e4441da15fd4aad5cf51cc418eba9c3e6
MD5 7df5d4b72621f5c5f5a40177f953fa94
BLAKE2b-256 5d2c6fdb88234cd16f7b8ad26175755c813e00b58c1a2dc9163223665069ad65

See more details on using hashes here.

File details

Details for the file msgpack-python-0.3.0.win32-py2.7.exe.

File metadata

File hashes

Hashes for msgpack-python-0.3.0.win32-py2.7.exe
Algorithm Hash digest
SHA256 e46b07ab7d6ed5f2ce9834f8eb8ed91fea57991b77733f6c5599db2a23b0e775
MD5 25d5b77a7dcb6f4ee655a6f5d88313b6
BLAKE2b-256 ee0da54105c63fb2393720b65daf3ce7c79743faa41c54a19fa0096b25959ca4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page