Re: Byte oriented data types in python

2009-01-25 Thread Ravi
Take a look at the struct and ctypes modules. struct is really not the choice. it returns an expanded string of the data and this means larger latency over bluetooth. ctypes is basically for the interface with libraries written in C (this I read from the python docs) --

Re: Byte oriented data types in python

2009-01-25 Thread Ravi
On Jan 25, 12:52 am, Martin v. Löwis mar...@v.loewis.de wrote: packet_type (1 byte unsigned) || packet_length (1 byte unsigned) || packet_data(variable) How to construct these using python data types, as int and float have no limits and their sizes are not well defined. In Python 2.x,

Re: Byte oriented data types in python

2009-01-25 Thread Steve Holden
Ravi wrote: Take a look at the struct and ctypes modules. struct is really not the choice. it returns an expanded string of the data and this means larger latency over bluetooth. If you read the module documentation more carefully you will see that it converts between the various native

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Ravi ra.ravi@gmail.com wrote: Take a look at the struct and ctypes modules. struct is really not the choice. it returns an expanded string of the data and this means larger latency over bluetooth. I don't know what you mean by returns an expanded string of the data. I do

Re: Byte oriented data types in python

2009-01-25 Thread Stephen Hansen
On Sun, Jan 25, 2009 at 7:27 AM, Ravi ra.ravi@gmail.com wrote: Take a look at the struct and ctypes modules. struct is really not the choice. it returns an expanded string of the data and this means larger latency over bluetooth. Noo... struct really IS the choice; that is the

Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
Take a look at the struct and ctypes modules. struct is really not the choice. it returns an expanded string of the data and this means larger latency over bluetooth. I don't know what you mean by returns an expanded string of the data. I do know that struct does exactly what you

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote: Take a look at the struct and ctypes modules. struct is really not the choice. it returns an expanded string of the data and this means larger latency over bluetooth. I don't know what you mean by returns an expanded string of the

Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
I disagree. He has a format (type, length, value), with the value being variable-sized. How do you do that in the struct module? You construct a format string for the value portion based on the type/length header. Can you kindly provide example code on how to do this? I don't see how

Re: Byte oriented data types in python

2009-01-25 Thread John Machin
On Jan 26, 2:28 am, Ravi ra.ravi@gmail.com wrote: On Jan 25, 12:52 am, Martin v. Löwis mar...@v.loewis.de wrote: packet_type (1 byte unsigned) || packet_length (1 byte unsigned) || packet_data(variable) How to construct these using python data types, as int and float have no

Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
Looks to me like there was already a reasonable way of getting a bytes object containing a variable number of zero bytes. Any particular reason why bytes(n) was given this specialised meaning? I think it was because bytes() was originally mutable, and you need a way to create a buffer of n

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote: You construct a format string for the value portion based on the type/length header. Can you kindly provide example code on how to do this? OK, something like this to handle received data where there is an initial 8-bit type field that

Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
dtype = ord(rawdata[0]) dcount = struct.unpack(!H,rawdata[1:3]) if dtype == 1: fmtstr = ! + H*dcount elif dtype == 2: fmtstr = ! + f*dcount rlen = struct.calcsize(fmtstr) data = struct.unpack(fmtstr,rawdata[3:3+rlen]) leftover = rawdata[3+rlen:]

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote: dtype = ord(rawdata[0]) dcount = struct.unpack(!H,rawdata[1:3]) if dtype == 1: fmtstr = ! + H*dcount elif dtype == 2: fmtstr = ! + f*dcount rlen = struct.calcsize(fmtstr) data =

Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
Unfortunately, that does not work in the example. We have a message type (an integer), and a variable-length string. So how do you compute the struct format for that? I'm confused. Are you asking for an introductory tutorial on programming in Python? Perhaps. I honestly do not know how to

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote: Unfortunately, that does not work in the example. We have a message type (an integer), and a variable-length string. So how do you compute the struct format for that? I'm confused. Are you asking for an introductory tutorial on

Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
It deals with variable sized fields just fine: dtype = 18 dlength = 32 format = !BB%ds % dlength rawdata = struct.pack(format, (dtype,dlength,data)) I wouldn't call this just fine, though - it involves a % operator to even compute the format string. IMO, it is *much* better not to use

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote: It deals with variable sized fields just fine: dtype = 18 dlength = 32 format = !BB%ds % dlength rawdata = struct.pack(format, (dtype,dlength,data)) I wouldn't call this just fine, though - it involves a % operator to even

Re: Byte oriented data types in python

2009-01-25 Thread Stephen Hansen
However, as you keep claiming that the struct module is what should be used, I must be missing something about the struct module. You seem to be focusing overly on the C Struct part of the description of what the struct module does, instead of the part where it says, packed binary data and

Re: Byte oriented data types in python

2009-01-25 Thread John Machin
On Jan 26, 10:53 am, Martin v. Löwis mar...@v.loewis.de wrote: It deals with variable sized fields just fine: dtype = 18 dlength = 32 format = !BB%ds % dlength rawdata = struct.pack(format, (dtype,dlength,data)) I wouldn't call this just fine, though - it involves a % operator to

Re: Byte oriented data types in python

2009-01-24 Thread Stephen Hansen
I have following packet format which I have to send over Bluetooth. packet_type (1 byte unsigned) || packet_length (1 byte unsigned) || packet_data(variable) How to construct these using python data types, as int and float have no limits and their sizes are not well defined. Check out the

Re: Byte oriented data types in python

2009-01-24 Thread Martin v. Löwis
packet_type (1 byte unsigned) || packet_length (1 byte unsigned) || packet_data(variable) How to construct these using python data types, as int and float have no limits and their sizes are not well defined. In Python 2.x, use the regular string type: chr(n) will create a single byte, and

Re: Byte oriented data types in python

2009-01-24 Thread skip
Ravi packet_type (1 byte unsigned) || packet_length (1 byte unsigned) || Ravi packet_data(variable) Ravi How to construct these using python data types, as int and float have Ravi no limits and their sizes are not well defined. Take a look at the struct and ctypes modules. --