def binread_sequence(self): self.skip_blanks_and_comments() seqtype = self.get() elemtype = self.get() if seqtype == '\x12': # 1D little endian sequence shape = struct.unpack('<i', self.read(4)) endianness = 'little' elif seqtype == '\x13': # 1D big endian sequence shape = struct.unpack('>i', self.read(4)) endianness = 'big' elif seqtype == '\x14': # 2D little endian sequence shape = struct.unpack('<ii', self.read(8)) endianness = 'little' elif seqtype == '\x15': # 2D big endian sequence shape = struct.unpack('>ii', self.read(8)) endianness = 'big' else: raise TypeError( 'In binread_sequence, read invalid starting seqtype code char: ' + seqtype) seq = [] nelems = 1 for dim in shape: nelems *= dim if elemtype == '\xFF': # generic sequence for i in xrange(nelems): seq.append(self.binread()) if len(shape) > 1: raise TypeError( 'Reading generic 2D sequence not yet suppored.. Must implement turning this into a list of lists...' ) return seq try: atype = PL_TYPECODE_TO_ARRAYTYPE[elemtype] except KeyError: raise TypeError('Unsuppored array elemtype: ' + elemtype) elemsize = int(atype[-1]) data = self.read(nelems * elemsize) ar = numarray.fromstring(data, atype, shape) if sys.byteorder != endianness: ar.byteswap(True) if self.return_lists: ar = ar.tolist() return ar
def binread_sequence(self): self.skip_blanks_and_comments() seqtype = self.get() elemtype = self.get() if seqtype=='\x12': # 1D little endian sequence shape = struct.unpack('<i',self.read(4)) endianness = 'little' elif seqtype=='\x13': # 1D big endian sequence shape = struct.unpack('>i',self.read(4)) endianness = 'big' elif seqtype=='\x14': # 2D little endian sequence shape = struct.unpack('<ii',self.read(8)) endianness = 'little' elif seqtype=='\x15': # 2D big endian sequence shape = struct.unpack('>ii',self.read(8)) endianness = 'big' else: raise TypeError('In binread_sequence, read invalid starting seqtype code char: '+seqtype) seq = [] nelems = 1 for dim in shape: nelems *= dim if elemtype=='\xFF': # generic sequence for i in xrange(nelems): seq.append(self.binread()) if len(shape)>1: raise TypeError('Reading generic 2D sequence not yet suppored.. Must implement turning this into a list of lists...') return seq try: atype = PL_TYPECODE_TO_ARRAYTYPE[elemtype] except KeyError: raise TypeError('Unsuppored array elemtype: '+elemtype) elemsize = int(atype[-1]) data = self.read(nelems*elemsize) ar = numarray.fromstring(data, atype, shape) if sys.byteorder!=endianness: ar.byteswap(True) if self.return_lists: ar = ar.tolist() return ar
def binread(self): self.skip_blanks_and_comments_and_separators() c = self.get() if c == "\x01": # signed char return self.get() elif c == "\x02": # unsigned char return self.get() elif c == "\x03": # short little endian return struct.unpack('<h', self.read(2))[0] elif c == "\x04": # short big endian return struct.unpack('>h', self.read(2))[0] elif c == "\x05": # unsigned short little endian return struct.unpack('<H', self.read(2))[0] elif c == "\x06": # unsigned short big endian return struct.unpack('>H', self.read(2))[0] elif c == "\x07": # 32 bit int little endian return struct.unpack('<i', self.read(4))[0] elif c == "\x08": # 32 bit int big endian return struct.unpack('>i', self.read(4))[0] elif c == "\x0B": # unsigned int little endian return struct.unpack('<I', self.read(4))[0] elif c == "\x0C": # unsigned int big endian return struct.unpack('>I', self.read(4))[0] elif c == "\x0E": # float little endian return struct.unpack('<f', self.read(4))[0] elif c == "\x0F": # float big endian return struct.unpack('>f', self.read(4))[0] elif c == "\x10": # double little endian return struct.unpack('<d', self.read(8))[0] elif c == "\x11": # double big endian return struct.unpack('>d', self.read(8))[0] elif c in "\x12\x13\x14\x15": # 1D or 2D sequence self.unread(c) return self.binread_sequence() elif c == "\x16": # 64 bit int little endian return struct.unpack('<q', self.read(8))[0] elif c == "\x17": # 64 bit int big endian return struct.unpack('>q', self.read(8))[0] #elif c=="\x16": # DEPRECATED binary pair format (for backward compatibility) # self.unread(c) # return self.binread_pair() elif c == '"': # string self.unread(c) return self.read_string() elif c == '*': # pointer self.unread(c) return self.binread_pointer() elif c == '{': # dictionary self.unread(c) return self.binread_dict() elif c == '(': # tuple self.unread(c) return self.binread_tuple() elif c == '0': # boolean return False elif c == '1': return True elif c in string.ascii_letters: # object name self.unread(c) return self.binread_object() else: raise TypeError("binread read unexpected character " + c)
def binread(self): self.skip_blanks_and_comments_and_separators() c = self.get() if c=="\x01": # signed char return self.get() elif c=="\x02": # unsigned char return self.get() elif c=="\x03": # short little endian return struct.unpack('<h',self.read(2))[0] elif c=="\x04": # short big endian return struct.unpack('>h',self.read(2))[0] elif c=="\x05": # unsigned short little endian return struct.unpack('<H',self.read(2))[0] elif c=="\x06": # unsigned short big endian return struct.unpack('>H',self.read(2))[0] elif c=="\x07": # 32 bit int little endian return struct.unpack('<i',self.read(4))[0] elif c=="\x08": # 32 bit int big endian return struct.unpack('>i',self.read(4))[0] elif c=="\x0B": # unsigned int little endian return struct.unpack('<I',self.read(4))[0] elif c=="\x0C": # unsigned int big endian return struct.unpack('>I',self.read(4))[0] elif c=="\x0E": # float little endian return struct.unpack('<f',self.read(4))[0] elif c=="\x0F": # float big endian return struct.unpack('>f',self.read(4))[0] elif c=="\x10": # double little endian return struct.unpack('<d',self.read(8))[0] elif c=="\x11": # double big endian return struct.unpack('>d',self.read(8))[0] elif c in "\x12\x13\x14\x15": # 1D or 2D sequence self.unread(c) return self.binread_sequence() elif c=="\x16": # 64 bit int little endian return struct.unpack('<q',self.read(8))[0] elif c=="\x17": # 64 bit int big endian return struct.unpack('>q',self.read(8))[0] #elif c=="\x16": # DEPRECATED binary pair format (for backward compatibility) # self.unread(c) # return self.binread_pair() elif c=='"': # string self.unread(c) return self.read_string() elif c=='*': # pointer self.unread(c) return self.binread_pointer() elif c=='{': # dictionary self.unread(c) return self.binread_dict() elif c=='(': # tuple self.unread(c) return self.binread_tuple() elif c=='0': # boolean return False elif c=='1': return True elif c in string.ascii_letters: # object name self.unread(c) return self.binread_object() else: raise TypeError("binread read unexpected character "+c)