""" Wrappers for doing binary IO on file-like objects """ import numpy import struct import sys ## Standard size: ## short is 8 bits ## int and long are 32 bits ## long long is 64 bits class BadMagicNumber( IOError ): pass class BinaryFileReader( object ): """ Wrapper for doing binary reads on any file like object. Currently this is not heavily optimized (it uses the `struct` module to unpack) """ def __init__( self, file, magic = None, is_little_endian = False ): self.is_little_endian = is_little_endian self.file = file if magic is not None: # Attempt to read magic number and chuck endianess bytes = file.read( 4 ) if struct.unpack( ">I", bytes )[0] == magic: pass elif struct.unpack( "