Re: Byte oriented data types in python

2009-01-25 Thread John Machin
On Jan 26, 10:53 am, "Martin v. Löwis" 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-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

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis 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 comp

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

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis 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 >> programmin

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

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis 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 = struct.unpack(fmtstr

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[

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis 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 is 1 for 1

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 b

Re: Byte oriented data types in python

2009-01-25 Thread John Machin
On Jan 26, 2:28 am, Ravi wrote: > On Jan 25, 12:52 am, "Martin v. Löwis" 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

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

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis 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 dat

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 wh

Re: Byte oriented data types in python

2009-01-25 Thread Stephen Hansen
On Sun, Jan 25, 2009 at 7:27 AM, 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. Noo... struct really IS the choice; that is the explicit purpose of the

Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, 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. I don't know what you mean by "returns an expanded string of the data". I do know that stru

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 nat

Re: Byte oriented data types in python

2009-01-25 Thread Ravi
On Jan 25, 12:52 am, "Martin v. Löwis" 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, use the

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) -- http://mail.python.org

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

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, a

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

Byte oriented data types in python

2009-01-24 Thread Ravi
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. -- http://mail.python