Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add length argument to read() #123

Open
GoogleCodeExporter opened this issue Apr 11, 2015 · 3 comments
Open

Add length argument to read() #123

GoogleCodeExporter opened this issue Apr 11, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 30 Mar 2012 at 2:57

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 30 Mar 2012 at 3:11

@GoogleCodeExporter
Copy link
Author

This

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

would definitely be the nicest of all.

Original comment by [email protected] on 30 Mar 2012 at 3:17

@GoogleCodeExporter
Copy link
Author

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

Original comment by [email protected] on 9 Dec 2012 at 4:07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants