• Stars
    star
    253
  • Rank 160,776 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A portable, lightweight MessagePack serializer and deserializer written in pure Python, compatible with Python 2, Python 3, CPython, PyPy / msgpack.org[Python]

u-msgpack-python Tests Status Docs Status GitHub release License

u-msgpack-python is a lightweight MessagePack serializer and deserializer module written in pure Python, compatible with Python 2 and 3, as well CPython and PyPy implementations of Python. u-msgpack-python is fully compliant with the latest MessagePack specification. In particular, it supports the new binary, UTF-8 string, application-defined ext, and timestamp types.

u-msgpack-python is currently distributed as a package on PyPI and as a single file module.

Installation

With pip:

$ pip install u-msgpack-python

With easy_install:

$ easy_install u-msgpack-python

or simply drop umsgpack.py into your project!

$ wget https://raw.github.com/vsergeev/u-msgpack-python/master/umsgpack/__init__.py -O umsgpack.py

Examples

Basic Example:

>>> import umsgpack
>>> umsgpack.packb({u"compact": True, u"schema": 0})
b'\x82\xa7compact\xc3\xa6schema\x00'
>>> umsgpack.unpackb(_)
{u'compact': True, u'schema': 0}
>>> 

A more complicated example:

>>> umsgpack.packb([1, True, False, 0xffffffff, {u"foo": b"\x80\x01\x02", \
...                 u"bar": [1,2,3, {u"a": [1,2,3,{}]}]}, -1, 2.12345])
b'\x97\x01\xc3\xc2\xce\xff\xff\xff\xff\x82\xa3foo\xc4\x03\x80\x01\
\x02\xa3bar\x94\x01\x02\x03\x81\xa1a\x94\x01\x02\x03\x80\xff\xcb\
@\x00\xfc\xd3Z\x85\x87\x94'
>>> umsgpack.unpackb(_)
[1, True, False, 4294967295, {u'foo': b'\x80\x01\x02', \
 u'bar': [1, 2, 3, {u'a': [1, 2, 3, {}]}]}, -1, 2.12345]
>>> 

Streaming serialization with file-like objects:

>>> f = open('test.bin', 'wb')
>>> umsgpack.pack({u"compact": True, u"schema": 0}, f)
>>> umsgpack.pack([1,2,3], f)
>>> f.close()
>>> 
>>> f = open('test.bin', 'rb')
>>> umsgpack.unpack(f)
{u'compact': True, u'schema': 0}
>>> umsgpack.unpack(f)
[1, 2, 3]
>>> f.close()
>>> 

Serializing and deserializing a raw Ext type:

>>> # Create an Ext object with type 5 and data b"\x01\x02\x03"
... foo = umsgpack.Ext(5, b"\x01\x02\x03")
>>> umsgpack.packb({u"stuff": foo, u"awesome": True})
b'\x82\xa5stuff\xc7\x03\x05\x01\x02\x03\xa7awesome\xc3'
>>> 
>>> bar = umsgpack.unpackb(_)
>>> print(bar['stuff'])
Ext Object (Type: 5, Data: 0x01 0x02 0x03)
>>> bar['stuff'].type
5
>>> bar['stuff'].data
b'\x01\x02\x03'
>>> 

Serializing and deserializing application-defined types with ext_serializable():

>>> @umsgpack.ext_serializable(0x50)
... class Point(collections.namedtuple('Point', ['x', 'y'])):
...     def packb(self):
...         return struct.pack(">ii", self.x, self.y)
...     @staticmethod
...     def unpackb(data):
...         return Point(*struct.unpack(">ii", data))
... 
>>> umsgpack.packb(Point(1, 2))
b'\xd7P\x00\x00\x00\x01\x00\x00\x00\x02'
>>> umsgpack.unpackb(_)
Point(x=1, y=2)
>>> 

Serializing and deserializing application-defined types with Ext handlers:

>>> umsgpack.packb([complex(1,2), decimal.Decimal("0.31")],
...  ext_handlers = {
...   complex: lambda obj: umsgpack.Ext(0x30, struct.pack("ff", obj.real, obj.imag)),
...   decimal.Decimal: lambda obj: umsgpack.Ext(0x40, str(obj).encode()),
... })
b'\x92\xd70\x00\x00\x80?\x00\x00\x00@\xd6@0.31'
>>> umsgpack.unpackb(_,
...  ext_handlers = {
...   0x30: lambda ext: complex(*struct.unpack("ff", ext.data)),
...   0x40: lambda ext: decimal.Decimal(ext.data.decode()),
... })
[(1+2j), Decimal('0.31')]
>>> 

Python standard library style names dump, dumps, load, loads are also available:

>>> umsgpack.dumps({u"compact": True, u"schema": 0})
b'\x82\xa7compact\xc3\xa6schema\x00'
>>> umsgpack.loads(_)
{u'compact': True, u'schema': 0}
>>> 
>>> f = open('test.bin', 'wb')
>>> umsgpack.dump({u"compact": True, u"schema": 0}, f)
>>> f.close()
>>> 
>>> f = open('test.bin', 'rb')
>>> umsgpack.load(f)
{u'compact': True, u'schema': 0}
>>> 

Documentation

Documentation is hosted at https://u-msgpack-python.readthedocs.io.

To build documentation locally with Sphinx, run:

cd docs
make html

Sphinx will produce the HTML documentation in docs/_build/html/.

Run make help to see other output targets (LaTeX, man, text, etc.).

Testing

The included unit tests may be run with test_umsgpack.py, under your favorite interpreter.

$ python2 test_umsgpack.py
$ python3 test_umsgpack.py
$ pypy test_umsgpack.py
$ pypy3 test_umsgpack.py

Alternatively, you can use tox or detox to test multiple Python versions at once.

$ pip install tox
$ tox

License

u-msgpack-python is MIT licensed. See the included LICENSE file for more details.

More Repositories

1

c-periphery

A C library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
C
727
star
2

luaradio

A lightweight, embeddable software-defined radio framework built on LuaJIT
Lua
604
star
3

python-periphery

A pure Python 2/3 library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
Python
529
star
4

briefsky

A free weather frontend to a variety of weather providers
TypeScript
243
star
5

apfcp

x86 Assembly Primer for C Programmers
Assembly
211
star
6

lua-periphery

A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
C
181
star
7

btckeygenie

A standalone Bitcoin keypair/address generator, written in Go.
Go
104
star
8

ntgbtminer

A no thrills getblocktemplate Bitcoin miner, written in Python.
Python
83
star
9

vavrdisasm

vAVRdisasm is an 8-bit Atmel AVR disassembler.
C
74
star
10

audioprism

A spectrogram tool for PulseAudio and WAV files, written in C++11.
C++
63
star
11

tinytaptunnel

a point-to-point layer 2 tap interface tunnel over UDP/IP with HMAC-SHA256 authentication, written in Go.
Go
57
star
12

ssterm

A simple console-based serial port terminal, written in Python.
Python
52
star
13

libGIS

libGIS is a collection of utility functions to create, read and write Atmel Generic, Intel HEX8, and Motorola S-Record formatted files.
C
50
star
14

0xtrades.info

A real-time trade viewer for the 0x protocol.
TypeScript
36
star
15

snake.ts

A simple snake implementation in TypeScript and blessed.
TypeScript
22
star
16

3d-gears

3D Parametric Gears made with Fusion 360
18
star
17

arm-bmw-sw

ARM Bare Metal Widget (arm-bmw) software
C
18
star
18

mbed-cmsis

Building your own CMSIS Code for the mbed.
C
17
star
19

vpicdisasm

vPICdisasm is a Microchip PIC disassembler for Baseline, Mid-Range, and Mid-Range Enhanced 8-bit PIC cores.
C
16
star
20

arm-bmw-hw

ARM Bare Metal Widget (arm-bmw) hardware
Eagle
15
star
21

cmsis-templates

CMSIS v3.20 Bootstrapping Templates for GNU ARM Tools
C
14
star
22

v8cpu

v8cpu is a simple multi-cycle von Neumann architecture 8-bit CPU in under 500 lines of Verilog.
Verilog
14
star
23

teatimer

A simple kitchen timer implemented in digital logic on a Lattice MachXO2 CPLD.
Eagle
14
star
24

gardend-lua

A modular, discrete-time control daemon for a hydroponic garden, written in Lua.
Lua
12
star
25

radio-decoders

Python
11
star
26

evolve110

Rule 110 on the Ethereum blockchain
JavaScript
9
star
27

libGISdotnet

libGIS ported to .NET.
C#
8
star
28

wireless-triac

Wireless ZigBee/XBee Controlled TRIAC (Sep 2009)
C
8
star
29

zigradio

A lightweight software-defined radio framework built with Zig
Zig
7
star
30

rigexpert-tool

Dump, plot, and convert impedance sweeps from a RigExpert antenna analyzer.
Python
6
star
31

qrd

Simple QR Code decoder for educational purposes
Python
6
star
32

minifortune

A minimal fortune-mod clone, written in C.
C
6
star
33

basic-makefiles

TeX
6
star
34

3d-simple-iso-thread

A simple, single-file modeled ISO thread module for OpenSCAD
OpenSCAD
4
star
35

3d-holster-claw

A 3D printable holster claw accessory made with Fusion 360
3
star
36

wireless-power-meter

Wireless ZigBee/XBee V-I Power Meter (Sep 2009)
Python
2
star
37

3d-mini-turntable

A miniature motorized turntable for small objects made with OpenSCAD
OpenSCAD
2
star
38

3d-fiber-enclosure

A 3D printable fiber optic project enclosure made with Fusion 360
1
star
39

3d-webcam-mount

3D Printable Webcam Mount made with Fusion 360
1
star
40

3d-jewelry-box

A 3D printable jewelry box made with OpenSCAD
OpenSCAD
1
star
41

3d-flosser-holder

A 3D printable flosser holder made with OpenSCAD
OpenSCAD
1
star
42

template110

Rule 110 implemented with C++11 templates and std::array.
C++
1
star
43

wclock

LED Word Clock (Mar 2012)
Eagle
1
star
44

yatumblr-backup

yet another tumblr backup script
Python
1
star