Skip to content

Instantly share code, notes, and snippets.

@Abraxos
Last active July 12, 2016 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abraxos/db76b80e7f712492362f34ef86efa55e to your computer and use it in GitHub Desktop.
Save Abraxos/db76b80e7f712492362f34ef86efa55e to your computer and use it in GitHub Desktop.
Just a reference about the basics of MessagePack in Python
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