kutsurak / python-bitstring

Automatically exported from code.google.com/p/python-bitstring
0 stars 0 forks source link

Add "pad:n" specifier to readlist and pack #130

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If there is currently a way to do this in bitstring, please let me know.

My problem is that when working with some hardware, I encountered a lot of 
"reserved" bits and input that I am not interested in and can ignore.

A simple example is, rrrrrrNNNNNNNNNNDDDDDDDDrrrrrrrF where the r's are 
reserved bits, the N's are a count and the D's are data, and F is a flag.

Currently in readlist, I can describe this with:

    "uintbe:6, uintbe:10, uintbe:8, uintbe:7, uintbe:1"

I don't care about the reserved bits, but readlist returns 5 numbers anyway.  
Of course, I can strip these out afterwards (or read each field one at a time), 
but it requires extra logic to ensure that the right reserved numbers get 
removed.  Since I have 100's of these specs, this is a maintenance problem.

It would be much more convenient to have a spec as follows:

    "pad:6, uintbe:10, uintbe:8, pad:7, uintbe:1"

Then I would only get the three numbers returned I care about, and the reserved 
bits would be ignored automatically.

The same idea would work with "pack".  "pad" elements would be zero-filled.

Would you consider this enhancement?

Original issue reported on code.google.com by edward.s...@gmail.com on 5 Dec 2012 at 2:37

GoogleCodeExporter commented 9 years ago
Hi, thanks for the suggestion.

I can't think of a better way to do this than the one you suggest. Thinking 
back I have quite often read values that I had no intention of ever using so at 
first glance this appears to be quite a nice idea.

It would need changes to readlist, pack, unpack and probably read and object 
creation. I'm a bit nervous about zero filling for object creation (whether 
through pack or otherwise) as I don't like to break 0/1 symmetry whenever 
reasonable, but it might be OK.

I'm putting this as an Accepted enhancement for now, but reserve the right to 
change my mind when I've had a longer think about it!

Cheers.

Original comment by dr.scott...@gmail.com on 5 Dec 2012 at 2:57

GoogleCodeExporter commented 9 years ago
Thanks for the response.

Out of frustration, I enhanced bitstring to support the "pad" keyword.  It 
wasn't that hard, given the structure of your code.

I am not saying that I did it the way you would have (it seemed a little 
hacky), but it seems to work well enough for my purposes (attached).  Consider 
it a starting point.

I didn't worry too much about initializing padding.  If the Reserved bits are 
important, they could be managed in variables so they can be re-packed.  If 
they are not important, setting them to zero should be fine.

Thanks again.

Original comment by edward.s...@gmail.com on 5 Dec 2012 at 5:00

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by dr.scott...@gmail.com on 9 Dec 2012 at 4:05

GoogleCodeExporter commented 9 years ago
Code changes done in r.964.

Original comment by dr.scott...@gmail.com on 24 Feb 2013 at 8:42

GoogleCodeExporter commented 9 years ago
Documentation in r.965

Original comment by dr.scott...@gmail.com on 25 Feb 2013 at 5:51