kutsurak / python-bitstring

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

Add length argument to read() #123

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm not sure if it will clobber the API, but I think it would be nice if read() 
would accept an additional argument to specify the length.

When you parse binary files, you often have your data prefixed with the size of 
it. So you need to read out the size first, and then the data.

Currently you would do:
    datasize = s.read('int:12')
    data = value.read(8*datasize).bytes

or 

    data = value.read('bytes:{}'.fomrat(datasize))

It would be nice if you could do:

    data = value.read('bytes', datasize))

Cheers,
  Volker

Original issue reported on code.google.com by volker.m...@gmail.com on 30 Mar 2012 at 2:57

GoogleCodeExporter commented 9 years ago
Hi,

I'm not sure that I see what the additional parameter would gain you. As you 
say, instead of 

    datasize = s.read('int:12')
    data = s.read('bytes', datasize)

you can use

    data = s.read(8*datasize).bytes

or

    data = s.read('bytes:{}'.format(datasize))

so I don't think you get much extra (you still need to read the datasize 
separately), and I don't like to add things the the API if I can avoid it.

On the other hand, what I think would be nice is to allow something like this:

    datasize, data = s.readlist('int:12=a, bytes:a')

which would allow the whole thing to happen in one step.

Original comment by dr.scott...@gmail.com on 30 Mar 2012 at 3:11

GoogleCodeExporter commented 9 years ago
This

  datasize, data = s.readlist('int:12=a, bytes:a')

would definitely be the nicest of all.

Original comment by volker.m...@gmail.com on 30 Mar 2012 at 3:17

GoogleCodeExporter commented 9 years ago
See also issue 131 for a slightly different usage, for example 
s.readlist('int:12=a, a*uint:8')

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