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
>
> 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
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
> 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
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
>> 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
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
> 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[
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
> 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
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
>> 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
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
>>> 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
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
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
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
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
> 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
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.
-
> 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
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
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
23 matches
Mail list logo