I'm looking for an easy way of packing/unpacking data structures for sending over the network:
on client just before sending:
a = ((1,2),(11,22,),(111,222))
message = pack(a)
and then on server:
a = unpack(message)
Is there a lib开发者_高级运维rary that could do pack/unpack magic? Thanks in advance
Looks like JSON might fit the bill. It's simple, and it's in the Python standard library.
It might not be too happy about the tuples, though:
>>> import json
>>> a = ((1,2),(11,22,),(111,222))
>>> print a
((1, 2), (11, 22), (111, 222))
>>> message = json.dumps(a)
>>> message
'[[1, 2], [11, 22], [111, 222]]'
>>> b = json.loads(message)
>>> b
[[1, 2], [11, 22], [111, 222]]
Whether or not that's a problem is for you to decide.
See pickle - Python object serialization:
The
pickle
module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”.
ast.literal_eval()
preserves tuples:
>>> a = ((1,2),(11,22,),(111,222))
>>> s = repr(a)
>>> import ast
>>> ast.literal_eval(s)
((1, 2), (11, 22), (111, 222))
精彩评论