Skip to main content

MessagePack (de)serializer.

Project description

Build Status Documentation Status

What’s this

MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it’s faster and smaller. This package provides CPython bindings for reading and writing MessagePack data.

Very important notes for existing users

PyPI package name

TL;DR: When upgrading from msgpack-0.4 or earlier, don’t do pip install -U msgpack-python. Do pip uninstall msgpack-python; pip install msgpack instead.

Package name on PyPI was changed to msgpack from 0.5. I upload transitional package (msgpack-python 0.5 which depending on msgpack) for smooth transition from msgpack-python to msgpack.

Sadly, this doesn’t work for upgrade install. After pip install -U msgpack-python, msgpack is removed and import msgpack fail.

Deprecating encoding option

encoding and unicode_errors options are deprecated.

In case of packer, use UTF-8 always. Storing other than UTF-8 is not recommended.

For backward compatibility, you can use use_bin_type=False and pack bytes object into msgpack raw type.

In case of unpacker, there is new raw option. It is True by default for backward compatibility, but it is changed to False in near future. You can use raw=False instead of encoding='utf-8'.

Planned backward incompatible changes

When msgpack 1.0, I planning these breaking changes:

  • packer and unpacker: Remove encoding and unicode_errors option.

  • packer: Change default of use_bin_type option from False to True.

  • unpacker: Change default of raw option from True to False.

  • unpacker: Reduce all max_xxx_len options for typical usage.

  • unpacker: Remove write_bytes option from all methods.

To avoid these breaking changes breaks your application, please:

  • Don’t use deprecated options.

  • Pass use_bin_type and raw options explicitly.

  • If your application handle large (>1MB) data, specify max_xxx_len options too.

Install

$ pip install msgpack

PyPy

msgpack provides a pure Python implementation. PyPy can use this.

Windows

When you can’t use a binary distribution, you need to install Visual Studio or Windows SDK on Windows. Without extension, using pure Python implementation on CPython runs slowly.

For Python 2.7, Microsoft Visual C++ Compiler for Python 2.7 is recommended solution.

For Python 3.5, Microsoft Visual Studio 2015 Community Edition or Express Edition can be used to build extension module.

How to use

One-shot pack & unpack

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

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

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

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

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

You should always specify the use_list keyword argument for backward compatibility. See performance issues relating to use_list option 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), use_bin_type=True))

buf.seek(0)

unpacker = msgpack.Unpacker(buf, raw=False)
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, use_bin_type=True)
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime, raw=False)

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.

Extended types

It is also possible to pack/unpack custom data types using the ext type.

>>> import msgpack
>>> import array
>>> def default(obj):
...     if isinstance(obj, array.array) and obj.typecode == 'd':
...         return msgpack.ExtType(42, obj.tostring())
...     raise TypeError("Unknown type: %r" % (obj,))
...
>>> def ext_hook(code, data):
...     if code == 42:
...         a = array.array('d')
...         a.fromstring(data)
...         return a
...     return ExtType(code, data)
...
>>> data = array.array('d', [1.2, 3.4])
>>> packed = msgpack.packb(data, default=default, use_bin_type=True)
>>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
>>> data == unpacked
True

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 de-serialising 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())

Notes

string and binary type

Early versions of msgpack didn’t distinguish string and binary types (like Python 1). The type for representing both string and binary types was named raw.

For backward compatibility reasons, msgpack-python will still default all strings to byte strings, unless you specify the use_bin_type=True option in the packer. If you do so, it will use a non-standard type called bin to serialize byte arrays, and raw becomes to mean str. If you want to distinguish bin and raw in the unpacker, specify raw=False.

Note that Python 2 defaults to byte-arrays over Unicode strings:

>>> import msgpack
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
['spam', 'eggs']
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
                    raw=False)
['spam', u'eggs']

This is the same code in Python 3 (same behaviour, but Python 3 has a different default):

>>> import msgpack
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
[b'spam', b'eggs']
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
                    raw=False)
[b'spam', 'eggs']

ext type

To use the ext type, pass msgpack.ExtType object to packer.

>>> import msgpack
>>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
>>> msgpack.unpackb(packed)
ExtType(code=42, data='xyzzy')

You can use it with default and ext_hook. See below.

Note about performance

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 option

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.

Development

Test

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

$ make 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-0.5.5.tar.gz (137.3 kB view details)

Uploaded Source

Built Distributions

msgpack-0.5.5-cp36-cp36m-win_amd64.whl (84.8 kB view details)

Uploaded CPython 3.6m Windows x86-64

msgpack-0.5.5-cp36-cp36m-win32.whl (76.5 kB view details)

Uploaded CPython 3.6m Windows x86

msgpack-0.5.5-cp36-cp36m-manylinux1_x86_64.whl (309.8 kB view details)

Uploaded CPython 3.6m

msgpack-0.5.5-cp36-cp36m-manylinux1_i686.whl (295.3 kB view details)

Uploaded CPython 3.6m

msgpack-0.5.5-cp35-cp35m-win_amd64.whl (84.1 kB view details)

Uploaded CPython 3.5m Windows x86-64

msgpack-0.5.5-cp35-cp35m-win32.whl (75.7 kB view details)

Uploaded CPython 3.5m Windows x86

msgpack-0.5.5-cp35-cp35m-manylinux1_x86_64.whl (304.3 kB view details)

Uploaded CPython 3.5m

msgpack-0.5.5-cp35-cp35m-manylinux1_i686.whl (287.2 kB view details)

Uploaded CPython 3.5m

msgpack-0.5.5-cp27-cp27mu-manylinux1_x86_64.whl (290.7 kB view details)

Uploaded CPython 2.7mu

msgpack-0.5.5-cp27-cp27mu-manylinux1_i686.whl (274.6 kB view details)

Uploaded CPython 2.7mu

msgpack-0.5.5-cp27-cp27m-win_amd64.whl (83.0 kB view details)

Uploaded CPython 2.7m Windows x86-64

msgpack-0.5.5-cp27-cp27m-win32.whl (74.8 kB view details)

Uploaded CPython 2.7m Windows x86

msgpack-0.5.5-cp27-cp27m-manylinux1_x86_64.whl (290.7 kB view details)

Uploaded CPython 2.7m

msgpack-0.5.5-cp27-cp27m-manylinux1_i686.whl (274.6 kB view details)

Uploaded CPython 2.7m

File details

Details for the file msgpack-0.5.5.tar.gz.

File metadata

  • Download URL: msgpack-0.5.5.tar.gz
  • Upload date:
  • Size: 137.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for msgpack-0.5.5.tar.gz
Algorithm Hash digest
SHA256 d6913b2f1bc7f4220f85a486769d81e2260e0693e086206485d2af7e6b4ad0b4
MD5 7a780e844c1c11cf5c71a4cd49c92157
BLAKE2b-256 3543980858dc279b4b224d9f93c0a19df0733708cf497b041079242356c329fc

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 19090eaf4495d511c55b99fcd89668334b339787f828e6e0f7e00c7aaeaca16e
MD5 61b36db0d24c29bfd2bd31e2ec37e754
BLAKE2b-256 d0d85b76ca1e2894b3429567b56025e31a28b4cd2cad59b4418f791d9f6e3d81

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 9492ef0b3603a270d82085d9950dcef019c9846384e2b901ce8311cc1bda3e0e
MD5 756ceb06cd55c09081caa04e0924dc4f
BLAKE2b-256 84ad7e1beaa4decca4814fe8a86dd35c0825db215e2c8178638ebba8ac79178c

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ee9de0f7e4cbc31f6c1f8e93235733589d7a53d5f543cb3b846848a5559d8cfd
MD5 45a2b5f766b3b30ef851309b3baa66c8
BLAKE2b-256 c9a85449c57d90c71bf0617f89e57152127ac29f79db43464de2024a7b2a6713

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ae18cbe02a63219a5c948aacef74e61bb6e0f0a305f6ef5a290a1e1ee6d2775e
MD5 8876f638453958ace0d5624c860f96ec
BLAKE2b-256 b068b09b2c583bcc91107b56effdcd959f5515b8f40d5d7af430500ea5a095f3

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 38d63e15518439b82a9427c1eae74ce8d7652845ddcb94f7483c9001df57a137
MD5 f97f91b949773254184e885b0f25e49a
BLAKE2b-256 35d4b3ab7c8fb538ac5c67f2e145027fdab9e87dc37dce9057442e758e1000f9

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 fcabf93bac3e17a7cec68434b311c1e768c8c76556e487a1d4bb3036590802e5
MD5 cdafcd233511f4bdcf06c0007fedcefc
BLAKE2b-256 993f60a460cc0bb19da29c016eee44347513c216533b35730ea8ed249ca3a7f7

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5f6abf06d7d789ba5a36c16e3d4a5ffed973844a3962148579df14e64cd7629c
MD5 afcf73c49013bbdd529efdeea21781dc
BLAKE2b-256 d058481f7d44dba22d40e5f3612954f14f5c5ef692f09148f1d15e55de2df0e4

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 c9dc7b866a4c5d6eb340b8f2e2ba71d38932d98eca88ee823b6d41c6526be8a8
MD5 b95df5551c799a541fa04eff20eed441
BLAKE2b-256 b1926fc58c8883aad986ec871d20d9080074faa3d34712a016b924b8f8d29dc9

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fcfad5f3d6af31d92538309fa19e8db4800b33beadd4c1c3f52138de5280ccb9
MD5 bfc4c913e07ac55041a6a8c57b803339
BLAKE2b-256 bcd85522738fc5cafdfc036f71907589908ee3ecc396b83f73ea64adea0b13b4

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8b9c87d89bd8aa88ab208cfa1edc32d391e843400c822109b82be7d7ab375adc
MD5 5a04352dc7651d49fd968b27d8b4ecd0
BLAKE2b-256 74954e052c17ab48c866c505b095828e42913aba807c6ea0f2089535b3d617e6

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 d7872b3ceb4421c17c42642c55520ddbd0b04e9fb1dcd3391afd5e20560d45e5
MD5 696307d4fa3b407dc688c5312ecab60e
BLAKE2b-256 cb3fa8c07ba932dfd648cab6d9b001894dc14cfb33db8b07266c34a29f6e8333

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 fc83de548a202d114d138f7065258874b2ec970ab0d4b2234b4f3c99d1ba76e2
MD5 4dd29a82b77d8ba2a7184942a6f87940
BLAKE2b-256 a22a7fc640b187556ab6cd8eb2b8923089f830e73f946ef5a6869fa2df1403e9

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e58f50767e3fcd76f3afc1c1e0a4d0b97858f35e144933892e9964a25b2c70ef
MD5 44398fbc73dd08606b5fe2643acd4b3c
BLAKE2b-256 bb15012538cf0a0410428c25fe28528cd4c793721f93236ab50996d39e1d6f5b

See more details on using hashes here.

File details

Details for the file msgpack-0.5.5-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.5-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 2508e307fd9484022ec8a5bd4906f399209899496b6f96cbe9e5df48a6f7326c
MD5 766b664ac891678d85451e8e5ac9c24a
BLAKE2b-256 82182318dcdf44dafb82c054460060af3f999d67dfde414e86b9b07b0c971693

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