ANN: binario - simple work with binary files

Rustom Mody rustompmody at gmail.com
Mon Aug 25 23:45:32 EDT 2014


On Tuesday, August 26, 2014 6:58:42 AM UTC+5:30, Tim Roberts wrote:
> bwatas wrote:
> >binario is the Python package that lets an application read/write primitive data types from an underlying input/output file as binary data.
> >Package on PyPI: https://pypi.python.org/pypi/binario
> >Package on GitHub: https://github.com/asaskevich/binario
> >Docs: http://binarios-docs.readthedocs.org/en/latest/
> >Package still in Alpha, and I need some help with testing, new features and docs :)

> I hope you will accept constructive criticism.

> The documentation says it lets an application read/write binary data from
> an "underlying input/output stream".  That's not really accurate.  "Stream"
> implies something very general, but your constructors will only accept
> filenames.  Your code ONLY works with files.  If I have a stream of data in
> memory, or an already open file, your code can't be used.

> This is why packages like "struct" (which is basically a more compact
> version of what you are doing) read from a string or a buffer, and leave
> the file-like behavior to things that already know how to behave like
> files.

> Compare your sample code:

>   >>> import binario
>   >>> r = binario.Reader("file.dat")
>   >>> r.read_short()
>   2014
>   >>> r.read_bool()
>   True
>   >>> r.read_float()
>   3.1415
>   >>> r.read_string()
>   "Hello, world!"
>   >>> r.read(5)
>   b'\x80\x14\n\xff\x00'

> To the equivalent code with struct:

>   import struct

>   dscrp = "H?fs5B"

>   f = open('file.dat')
>   stuff = struct.unpack( dscrp, f.read() )

>   print stuff

> In both cases, you have to KNOW the format of the data beforehand.  If you
> do a read_short where you happen to have written a float, disaster ensues.

> I don't really see that you've added very much.

I thought much the same.
However notice your f.read(). Its type is string.

What if file.dat is a 1GB wav file?



More information about the Python-list mailing list