Last active
July 12, 2016 02:53
-
-
Save Abraxos/db76b80e7f712492362f34ef86efa55e to your computer and use it in GitHub Desktop.
Just a reference about the basics of MessagePack in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from msgpack import packb | |
from msgpack import unpackb | |
from time import time | |
def pack(msg): | |
return packb(msg, use_bin_type=True) | |
def unpack(msg): | |
return unpackb(msg, use_list=False, encoding='utf-8') | |
# Testing lists: | |
mylist = [1, 2, 3, 5, "something", 'c', False] | |
print(mylist) | |
msg = pack(mylist) | |
print(msg) | |
result = unpack(msg) | |
print(result) | |
# Testing dictionaries | |
mydict = { | |
"id": 1, | |
"time": time(), | |
"stuff": "Hello, world!", | |
"more stuff": { | |
"name": "John", | |
"occupation": "Smith" | |
} | |
} | |
print(mydict) | |
msg = pack(mydict) | |
print(msg) | |
result = unpack(msg) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment