views:

250

answers:

4

Hi, everyone

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 library that could do pack/unpack magic? Thanks in advance

+7  A: 

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.

Thomas
Thanks. Looks like what I needed.
facha
+1: JSON or even YAML is definitely a way to go. YAML will keep tuple a tuple without serializing it to list, but it is not a standard package. For something that should be human readable (like configuration files)., I would definitely go for YAML.
van
@van No, YAML can't keep a tuple as a tuple without using unsafe tags. Using unsafe tags is a horrible idea for network communication, for the same reason pickle is. There are few exceptions. And, really, if you're going to use language-specific and unsafe tags in YAML, what does YAML have over using pickle?
Devin Jeanpierre
@Devin: as I mentioned: human readable. You are right about the usage of unsafe tags, and one should decide on the trade-off.
van
+3  A: 

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”.

John Kugelman
A word of warning: if you don't trust the sender, don't use pickle.
cobbal
pickle is almost always wrong for network communication, for the reason cobbal states. From the pickle documentation: `Warning: The pickle module is not intended to be secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.` Given that in client-server communication it is pretty much impossible to know that the client is what you think it is, software wise, this answer is simply wrong. (there are special exceptions, like if only one person can connect and you know who he is, that sort of thing).
Devin Jeanpierre
A: 

In your example, I'm not sure why you even need JSON:

>>> a = ((1,2),(11,22,),(111,222))
>>> print str(a)
((1, 2), (11, 22), (111, 222))
>>> b = eval(str(a))
>>> b
((1, 2), (11, 22), (111, 222))
Casey
Because `eval()` is almost always a bad idea.
Xavier Ho
using `eval` on data received over a network is a security and maintenance nightmare.
J.F. Sebastian
A: 

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))
J.F. Sebastian