Welcome, guest | Sign In | My Account | Store | Cart
import struct

class BinaryReaderEOFException(Exception):
    def __init__(self):
        pass
    def __str__(self):
        return 'Not enough bytes in file to satisfy read request'

class BinaryReader:
    # Map well-known type names into struct format characters.
    typeNames = {
        'int8'   :'b',
        'uint8'  :'B',
        'int16'  :'h',
        'uint16' :'H',
        'int32'  :'i',
        'uint32' :'I',
        'int64'  :'q',
        'uint64' :'Q',
        'float'  :'f',
        'double' :'d',
        'char'   :'s'}

    def __init__(self, fileName):
        self.file = open(fileName, 'rb')
        
    def read(self, typeName):
        typeFormat = BinaryReader.typeNames[typeName.lower()]
        typeSize = struct.calcsize(typeFormat)
        value = self.file.read(typeSize)
        if typeSize != len(value):
            raise BinaryReaderEOFException
        return struct.unpack(typeFormat, value)[0]
    
    def __del__(self):
        self.file.close()

Diff to Previous Revision

--- revision 1 2011-03-14 23:59:46
+++ revision 2 2011-03-15 00:11:10
@@ -1,10 +1,10 @@
+import struct
+
 class BinaryReaderEOFException(Exception):
     def __init__(self):
         pass
-        
     def __str__(self):
         return 'Not enough bytes in file to satisfy read request'
-
 
 class BinaryReader:
     # Map well-known type names into struct format characters.

History