kutsurak / python-bitstring

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

Add "uint16" etc. parameters to Bits constructor #138

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Right now, to store a 16 bit uint, I need to do:

   Bits(uint=foo, length=16)

Would it be perhaps sensible to also allow:

   Bits(uint16=foo)

instead?

This allows for more compact creation of Bits values. Of course, this should 
work for at least 8, 16, 32 and perhaps also 64, and work for uint, int, 
uintbe, uintle, etc. But perhaps it's easy enough to just catch **kwargs and 
then make this work for any combination automatically?

While writing this, I realize that the "pack" function offers something 
similar, perhaps that can be reused? Or I could just use pack instead, which 
also gives me a pretty concise syntax:

    pack('uint16', foo)

Original issue reported on code.google.com by matthijskooijman@gmail.com on 7 Nov 2013 at 3:43

GoogleCodeExporter commented 9 years ago
You can already use

    pack('uint:16', foo)

and you can also do

    Bits('uint:16=100')

but that only works if you know the value in advance.

I'm not keen on multiplying up the number of parameters as I don't think it 
adds much except lots of extra code, documentation and tests...

Original comment by dr.scott...@gmail.com on 7 Nov 2013 at 3:59