struct is a very good choice for this use case. You will want to use struct objects for efficiency, so that packing/unpacking format strings can be compiled in advance: http://docs.python.org/library/struct.html#struct-objects
Thanks Jim. Unfortunately, it’s Jython only on one end. The other device is just a microcontroller, and I will just be sending and receiving comma delimited messages with mixed data types. But, I think I’ve figured out how to do it with STRUCT:
m = ['This is Test #1', -100, 2000, 'Stop']
format = ''.join([str(len(m)),'s','h','h',str(len(m)),'s'])
data = struct.pack(format, m,m,m,m)
for i in data:
I haven’t tried it with a live controller yet, but the output certainly looks right.
By iterating through ‘m’, I can also determine the data type and build the format dynamically, so I will end up with a pretty standard module.
for i in m:
From: email@example.com [mailto:firstname.lastname@example.org] On Behalf Of Jim Baker
Sent: Saturday, 11. July 2009 19:45
Subject: Re: [Jython-users] Jython Sockets and Strings
Assuming it's Jython (or Python in general) on both ends, you can use cPickle. Otherwise you might choose PyAMF (good also for Flex clients) or JSON (rather universal coverage at this point).
On Sat, Jul 11, 2009 at 12:24 PM, Carl <email@example.com> wrote:
I’ve written a Jython socket program that grabs incoming messages and writes them to a database. It works great with my test data.
But now, I want to send a message that contains mixed data types. During testing, I had the luxury of encoding and decoding on both ends, but in this case I can’t do that. For instance, I might have this list of data:
What I want to do is concatenate each item in the list into a string for the socket.send. Note that this needs to be a raw stream of bytes, so converting 100 to a string isn’t possible. Obviously I can’t just use a ‘’.join due to the non-string variables, so I looked at the java libraries and it looks like some combination of jarray, java.lang.string, and outputstream should work. Can someone point me in the right direction?
Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize
details at: http://p.sf.net/sfu/Challenge
Jython-users mailing list