Would using struct objects still be effective if the message structure was changing constantly (i.e., the number of items)?

 

I’ve put together something that handles variable lists (the list will be generated somewhere else obviously in the real world). If someone sees a better or faster way, I’m all ears. Thanks!

 

 

import struct

 

m = ['This is Test #1', -100, 2000, 2,3,4,5,6,'Stop',10]

 

##This is the actual message tuple

message = ()  

 

## Here I iterate through the list and build the message by concatenating a ‘,’ after each element; could this be done with a list comprehension?

for i in m:

    message+=(i,',')

 

format = ''

for i in message:

    if type(i) is int:

        format+='h'

    if type(i) is str:

        format+=str(len(i))+'s'

 

data = struct.pack(format,*message)

 

print data

 


From: james.edward.baker@gmail.com [mailto:james.edward.baker@gmail.com] On Behalf Of Jim Baker
Sent: Sunday, 12. July 2009 14:11
To: Carl
Cc: jython-users@lists.sourceforge.net
Subject: Re: [Jython-users] Jython Sockets and Strings

 

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

On Sun, Jul 12, 2009 at 11:23 AM, Carl <s7plc@ttlc.net> wrote:

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:

 

import struct

m = ['This is Test #1', -100, 2000, 'Stop']

 

format = ''.join([str(len(m[0])),'s','h','h',str(len(m[3])),'s'])

data = struct.pack(format, m[0],m[1],m[2],m[3])

 

for i in data:

    print ord(i),i

 

 

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:

    print type(i)

 

 


From: james.edward.baker@gmail.com [mailto:james.edward.baker@gmail.com] On Behalf Of Jim Baker
Sent: Saturday, 11. July 2009 19:45
To: Carl
Cc: jython-users@lists.sourceforge.net
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 <s7plc@ttlc.net> 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:

 

[‘Test’1,’Test2,100,7,18,’21’]

 

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?

 

Thanks.

 

Carl


------------------------------------------------------------------------------
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
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users




--
Jim Baker
jbaker@zyasoft.com




--
Jim Baker
jbaker@zyasoft.com