Writing more efficient code

John Machin sjmachin at lexicon.net
Tue Jan 2 03:06:01 EST 2007


On 2/01/2007 2:23 PM, gonzlobo wrote:
> Thanks to John, Paul & Jon for their responses. This list is great for 
> info.

Hi gonzlobo,

Please dont use private e-mail; post to the Python mailing-list / 
newsgroup, so that everybody can see what the eventual outcome is.

Please also answer the question that I asked: in effect, what you have 
showed looks like a hex dump of a binary fiie -- are you saying that the
data is actually transmitted over the serial bus in hex with spaces in 
between?

> 
> Jon,
> OCaml might be the best option, but my braincell is only good for 1
> language at a time. Hopefully python doesn't replace English.  :^)
> 
> Paul,
> Thanks for the snippets. It looks pretty complicated, but worth
> looking into. I'm all for reducing line of code (hopefully not for
> readability).
> 
> John M,
> You're right, my original example contained non-sensical data (I made
> it up for example's sake).

It wasn't me who wrote that, but I agree.

> 
> Here's a snippet of real data (it's a 10Mb/s serial bus, so there's
> *alot* of data).
> 0000007a 06 0321 80 00 34 d1 01 0b 3f f7 01 6b
> 0000007b 26 0311 00 00 00 00 1a bd 00 00 00 00
> 0000007c 06 0321 80 00 a0 04 81 eb 20 05 81 1b
> 0000007d 16 0614 00 00 00 00 00 00 00 00 00 00
> 0000007e 06 0321 80 00 20 00 01 07 a0 43 01 9b
> 0000007f 06 0301 80 00 a0 b9 82 2b 3f d6 02 ab
> 00000080 06 0321 80 00 bf d4 01 5b a3 f0 01 db
> 00000081 06 0301 80 00 31 9c 02 0b bf d7 02 15
> 00000082 0f 0416 01 01 00 00 20 20 20 20 20 20
> 00000083 06 0301 80 00 bf ff 02 6b bf f3 82 eb
> 00000084 0f 0416 02 01 00 00 20 20 20 20 20 20
> 00000085 06 0301 80 00 bf ed 82 1b a0 07 02 07
> 00000086 06 0321 00 00 00 00 01 af 00 00 00 00
> 00000087 26 0311 80 00 e0 ce 02 30 80 07 82 86
> 00000088 06 0301 80 00 a0 4a 02 9b 3f df 02 5b
> 00000089 06 0301 80 00 80 00 02 ce 80 00 02 b3
> 0000008a 06 0301 80 00 00 00 02 5f e0 00 02 89
> 0000008b 16 0614 00 fe 31 00 00 00 00 00 00 00
> 0000008c 43 03a1 01 00 80 00 02 5d 80 0b 06 5d
> 0000008d 06 0301 80 00 60 a1 92 c1 e0 a1 8a 21
> 0000008e 4f 0450 01 10 00 00 80 00 37 00 00 00
> 
> line = 0000007a 06 0321 80 00 34 d1 01 0b 3f f7 01 6b
          0....v....1....v....2....v....3....v....4....v

> Label 321 actually contains:
> 
>    time = line[:8]
>    PID = line[12:16]
>    d2 = line[17:19]
>    d3 = line[20:22]
>    d4 = line[23:25]
>    d5 = line[26:28]
>    d6 = line[29:31]
>    d7 = line[32:34]
>    d8 = line[35:37]
>    d9 = line[38:40]
>    d10 = line[41:43]
>    d11 = line[44:46]

That's not Python 101, it's PYBOL :-) I haven't had to resort to the 
0....v....1 etc caper for a very long time.

Consider doing this:

ints = [int(x, 16) for x in line.split()]
time = ints[0]
d = ints[1:]
pid = d[1]
# what is the undescribed field in ints[1]?


> 
> d2 + d3 = Pitch Angle (* 0.01)

I asked you before what you mean by "combine" ... now I'll ask what you 
mean by "d2 + d3"

Do you mean this:
     pitch_angle = (d[3] * 256 + d[2]) * 0.01
?


> d4 + d5 = Roll Angle (* 0.05)
> d6 + d8 = Magnetic Heading (* 0.05)

What happened to d7?

> d9 + d10 = Pressure Altitude (* 1.0)
> d11 = various flags


> 
> My code is python 101 (uses lots of nested if's), so using
> dictionaries would be very helpful.

Here's my Python 102 ... we can help you get to 201 level with a bit 
more disclosure from you on the specifics ...

| >>> line = "0000007a 06 0321 80 00 34 d1 01 0b 3f f7 01 6b"
| >>> ints = [int(x, 16) for x in line.split()]
| >>> ints
| [122, 6, 801, 128, 0, 52, 209, 1, 11, 63, 247, 1, 107]
| >>> time = ints[0]
| >>> time
| 122
| >>> d = ints[1:]
| >>> d
| [6, 801, 128, 0, 52, 209, 1, 11, 63, 247, 1, 107]
| >>> unknown = d[0]
| >>> unknown
| 6
| >>> pid = d[1]
| >>> pid
| 801
| >>> hex(pid)
| '0x321'
| >>> pitch_angle = (d[3] * 256 + d[2]) * 0.01
| >>> pitch_angle
| 1.28
| >>>

Call me crazy, but I'm highly suspicious of 0x8000 becoming a pitch 
angle of 1.28 degrees ;-) Especially since other lines in your sample 
with pid == 0x321 have (mostly) d2d3 == 0x8000 also, except for one with 
0x000 -- I'm not an aeronautical engineer, but I would have expected 
other values for pitch angle.

HTH,
John



More information about the Python-list mailing list