def deserialize(self, str): """ unpack serialized message in str into this message instance :param str: byte array of serialized message, ``str`` """ try: end = 0 start = end end += 4 (self.result,) = _struct_i.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%si'%length start = end end += struct.calcsize(pattern) self.resolution = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sf'%length start = end end += struct.calcsize(pattern) self.buffer = struct.unpack(pattern, str[start:end]) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def get_images(filename): bin_file = open(filename,'rb') buf = bin_file.read()#all the file are put into memory bin_file.close()# release the measure of operating system index = 0 magic, num_images, num_rows, num_colums = struct.unpack_from(big_endian+four_bytes, buf,index) index += struct.calcsize(big_endian + four_bytes) # TODO why not multy 4? print num_images images = [] #temp images as tuple for x in range(num_images): im = struct.unpack_from(big_endian + picture_bytes, buf, index) index += struct.calcsize(big_endian + picture_bytes) im = list(im) for i in range(len(im)) : if im[i] >= 1 and im[i] < 64: im[i] = 1 if im[i] >= 64 and im[i] < 128: im[i] = 2 if im[i] >= 128 and im[i] < 192: im[i] = 3 if im[i] >= 192 and im[i] < 256: im[i] = 4 else: im[i] = 0 images.append(im) a = np.array(images) return a
def _parse_hpr_time_series(self, offset, rules): """ Convert the binary data into particle data for the Heading, Pitch, Time Series Data Type """ # Unpack the unpacking rules (hpr_num_name, beam_angle_name, spare_name, hpr_time_names),\ (hpr_num_fmt, beam_angle_fmt, spare_fmt, hpr_time_fmt) = zip(*rules) # First unpack the array length and single length value, no need to unpack spare (hpr_num_data, beam_angle_data) = struct.unpack_from( '<%s%s' % (hpr_num_fmt, beam_angle_fmt), self.raw_data, offset) # Then unpack the array using the retrieved lengths value next_offset = offset + struct.calcsize(hpr_num_fmt) + struct.calcsize(beam_angle_fmt) + \ struct.calcsize(spare_fmt) hpr_time_list_data = struct.unpack_from( '<%s%s' % (hpr_num_data * HPR_TIME_SERIES_ARRAY_SIZE, hpr_time_fmt), self.raw_data, next_offset) # convert to numpy array and reshape the data to a 2d array per IDD spec transformed_hpr_time_data = numpy.array(hpr_time_list_data).reshape( (hpr_num_data, HPR_TIME_SERIES_ARRAY_SIZE)).transpose().tolist() # Add to the collected parameter data self.final_result.extend( ({DataParticleKey.VALUE_ID: hpr_num_name, DataParticleKey.VALUE: hpr_num_data}, {DataParticleKey.VALUE_ID: beam_angle_name, DataParticleKey.VALUE: beam_angle_data}, {DataParticleKey.VALUE_ID: hpr_time_names[HEADING_TIME_SERIES_IDX], DataParticleKey.VALUE: transformed_hpr_time_data[HEADING_TIME_SERIES_IDX]}, {DataParticleKey.VALUE_ID: hpr_time_names[PITCH_TIME_SERIES_IDX], DataParticleKey.VALUE: transformed_hpr_time_data[PITCH_TIME_SERIES_IDX]}, {DataParticleKey.VALUE_ID: hpr_time_names[ROLL_TIME_SERIES_IDX], DataParticleKey.VALUE: transformed_hpr_time_data[ROLL_TIME_SERIES_IDX]}))
def _unpack(self, data): """ Unpack the data blob as a motoboot image and return the list of contained image objects""" num_imgs_fmt = "<L" num_imgs_size = struct.calcsize(num_imgs_fmt) num_imgs, = struct.unpack(num_imgs_fmt, data[:num_imgs_size]) img_info_format = "<24sLL" img_info_size = struct.calcsize(img_info_format) imgs = [ struct.unpack(img_info_format, data[num_imgs_size + i*img_info_size:num_imgs_size + (i+1)*img_info_size]) for i in range(num_imgs) ] magic_format = "<8s" magic_size = struct.calcsize(magic_format) magic, = struct.unpack(magic_format, data[num_imgs_size + NUM_MAX_IMAGES*img_info_size:num_imgs_size + NUM_MAX_IMAGES*img_info_size + magic_size]) if magic != MAGIC: raise BadMagicError img_objs = [] for name, start, end in imgs: start_offset = HEADER_SIZE + start * SECTOR_SIZE end_offset = HEADER_SIZE + (end + 1) * SECTOR_SIZE - 1 img = common.File(trunc_to_null(name), data[start_offset:end_offset+1]) img_objs.append(img) self.unpacked_images = img_objs
def _parse_values(self, offset, rules): """ Convert the binary data into particle data for the given rules """ position = offset # Iterate through the unpacking rules and append the retrieved values with its corresponding # particle name for key, formatter in rules: # Skip over spare values if AdcptMWVSParticleKey.SPARE in key: position += struct.calcsize(formatter) continue value = list(struct.unpack_from('<%s' % formatter, self.raw_data, position)) # Support unpacking single values and lists if len(value) == 1: value = value[0] if AdcptMWVSParticleKey.START_TIME in key: timestamp = ((value[0]*100 + value[1]), value[2], value[3], value[4], value[5], value[6], value[7], 0, 0) log.trace("TIMESTAMP: %s", timestamp) elapsed_seconds = calendar.timegm(timestamp) self.set_internal_timestamp(unix_time=elapsed_seconds) log.trace("DATA: %s:%s @ %s", key, value, position) position += struct.calcsize(formatter) self.final_result.append({DataParticleKey.VALUE_ID: key, DataParticleKey.VALUE: value})
def render(self): """ Returns a tuple of format and data suitable for ``struct.pack``. Each (<offset>, <message>) tuple in ``self.messages`` is `render()`-ed and the output collected int a single format and data list, prefaced with a single integer denoting the size of the message set. """ fmt = ["i"] data = [] total_size = 0 for offset, message in self.messages: offset_format, offset_data = Int64(offset).render() message_format, message_data = message.render() message_size = struct.calcsize(message_format) size_format, size_data = Int32(message_size).render() message_format = "".join([ offset_format, size_format, message_format ]) total_size += struct.calcsize("!" + message_format) fmt.append(message_format) data.extend(offset_data) data.extend(size_data) data.extend(message_data) data.insert(0, total_size) return "".join(fmt), data
def _parse_directional_spectrum(self, offset, rules): """ Convert the binary data into particle data for the Directional Spectrum Data Type """ # Unpack the unpacking rules (num_freq_name, num_dir_name, good_name, dat_name),\ (num_freq_fmt, num_dir_fmt, good_fmt, dat_fmt) = zip(*rules) # First unpack the array lengths and single length values (num_freq_data, num_dir_data, dspec_good_data) = struct.unpack_from( '<%s%s%s' % (num_freq_fmt, num_dir_fmt, good_fmt), self.raw_data, offset) # Then unpack the array using the retrieved lengths values next_offset = offset + struct.calcsize(num_freq_fmt) + struct.calcsize(num_dir_fmt) + \ struct.calcsize(good_fmt) dspec_dat_list_data = struct.unpack_from( '<%s%s' % (num_freq_data * num_dir_data, dat_fmt), self.raw_data, next_offset) # convert to numpy array and reshape the data per IDD spec transformed_dat_data = numpy.array(dspec_dat_list_data).reshape( (num_freq_data, num_dir_data)).tolist() # Add to the collected parameter data self.final_result.extend( ({DataParticleKey.VALUE_ID: num_freq_name, DataParticleKey.VALUE: num_freq_data}, {DataParticleKey.VALUE_ID: num_dir_name, DataParticleKey.VALUE: num_dir_data}, {DataParticleKey.VALUE_ID: good_name, DataParticleKey.VALUE: dspec_good_data}, {DataParticleKey.VALUE_ID: dat_name, DataParticleKey.VALUE: transformed_dat_data}))
def __init__(self,fileID): temp = fileID.read(struct.calcsize(self.binary_format)) data = struct.unpack(self.binary_format,temp) self.meshname = "".join([str(x, "ascii") for x in data[0:64] if x[0] < 127 ]) #Name of Mesh every Char is a String on his own self.framecount = data[64] #Framecount = Number of Animationsteps self.vertexcount = data[65] #Number of Vertices in each Frame self.indexcount = data[66] #Number of Indices in Mesh (Triangles = Indexcount/3) self.diffusecolor = data[67:70] #RGB diffuse color self.specularcolor = data[70:73] #RGB specular color (unused) self.specularpower = data[73] #Specular power (unused) self.opacity = data[74] #Opacity self.properties= data[75] #Property flags self.textures = data[76] #Texture flag if self.properties & 1: #PropertyBit is Mesh Alpha Channel custom Color in Game ? self.customalpha = True else: self.customalpha = False if self.properties & 2: #PropertyBit is Mesh TwoSided ? self.istwosided = True else: self.istwosided = False if self.textures & 1: #PropertyBit is Mesh Textured ? temp = fileID.read(struct.calcsize(self.texname_format)) data = struct.unpack(self.texname_format,temp) self.texturefilename = "".join([str(x, "ascii") for x in data[0:-1] if x[0] < 127 ]) self.hastexture = True else: self.hastexture = False self.texturefilename = None
def _parse_sync_response(self, data): keyspecs = [] nkeys = struct.unpack(">H", data[0: struct.calcsize("H")])[0] offset = struct.calcsize("H") for i in xrange(nkeys): spec = {} width = struct.calcsize("QHHB") spec['cas'], spec['vbucket'], keylen, eventid = struct.unpack(">QHHB", data[offset: offset + width]) offset += width spec['key'] = data[offset: offset + keylen] offset += keylen if eventid == MemcachedConstants.CMD_SYNC_EVENT_PERSISTED: spec['event'] = 'persisted' elif eventid == MemcachedConstants.CMD_SYNC_EVENT_MODIFED: spec['event'] = 'modified' elif eventid == MemcachedConstants.CMD_SYNC_EVENT_DELETED: spec['event'] = 'deleted' elif eventid == MemcachedConstants.CMD_SYNC_EVENT_REPLICATED: spec['event'] = 'replicated' elif eventid == MemcachedConstants.CMD_SYNC_INVALID_KEY: spec['event'] = 'invalid key' elif spec['event'] == MemcachedConstants.CMD_SYNC_INVALID_CAS: spec['event'] = 'invalid cas' else: spec['event'] = eventid keyspecs.append(spec) return keyspecs
def __init__(self, player_response): self.players = [] if player_response != None: pre_header,\ header,\ num_players = struct.unpack('<IBB', player_response) assert header == 0x44 player_response = player_response[pystruct.calcsize('<IBB'):] # unpack all the players in the player response while len(player_response) > 0: index,\ name,\ kills,\ time = struct.unpack('<Bzif', player_response) # don't include 'players' with no name - the steam # browser itself appears to ignore these if len(name) > 0: self.players.append(ServerPlayer(name, kills, time)) # add 1 for null byte in the player name packet_size = pystruct.calcsize('<Bif') + len(name) + 1 player_response = player_response[packet_size:] self.players.sort(reverse=True)
def deserialize_numpy(self, str, numpy): """ unpack serialized message in str into this message instance using numpy for array types :param str: byte array of serialized message, ``str`` :param numpy: numpy python module """ try: if self.c is None: self.c = lab5_msgs.msg.ColorMsg() end = 0 _x = self start = end end += 28 (_x.c.r, _x.c.g, _x.c.b, _x.numVertices) = _struct_3qi.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = "<%sf" % length start = end end += struct.calcsize(pattern) self.x = numpy.frombuffer(str[start:end], dtype=numpy.float32, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = "<%sf" % length start = end end += struct.calcsize(pattern) self.y = numpy.frombuffer(str[start:end], dtype=numpy.float32, count=length) _x = self start = end end += 8 (_x.closed, _x.filled) = _struct_2i.unpack(str[start:end]) return self except struct.error as e: raise genpy.DeserializationError(e) # most likely buffer underfill
def deserialize(self, str): """ unpack serialized message in str into this message instance :param str: byte array of serialized message, ``str`` """ try: if self.c is None: self.c = lab5_msgs.msg.ColorMsg() end = 0 _x = self start = end end += 28 (_x.c.r, _x.c.g, _x.c.b, _x.numVertices) = _struct_3qi.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = "<%sf" % length start = end end += struct.calcsize(pattern) self.x = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = "<%sf" % length start = end end += struct.calcsize(pattern) self.y = struct.unpack(pattern, str[start:end]) _x = self start = end end += 8 (_x.closed, _x.filled) = _struct_2i.unpack(str[start:end]) return self except struct.error as e: raise genpy.DeserializationError(e) # most likely buffer underfill
def deserialize(self, str): """ unpack serialized message in str into this message instance :param str: byte array of serialized message, ``str`` """ try: if self.header is None: self.header = std_msgs.msg.Header() end = 0 _x = self start = end end += 12 (_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs,) = _struct_3I.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) start = end end += length if python3: self.header.frame_id = str[start:end].decode('utf-8') else: self.header.frame_id = str[start:end] start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.acceleration = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.orientation = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.angular = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.magnetic = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.timestamp = struct.unpack(pattern, str[start:end]) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def UnpackDEV_BROADCAST(lparam): # guard for 0 here, otherwise PyMakeBuffer will create a new buffer. if lparam == 0: return None hdr_size = struct.calcsize("iii") hdr_buf = win32gui.PyMakeBuffer(hdr_size, lparam) size, devtype, reserved = struct.unpack("iii", hdr_buf) rest = win32gui.PyMakeBuffer(size-hdr_size, lparam+hdr_size) extra = x = {} if devtype == win32con.DBT_DEVTYP_HANDLE: # 2 handles, a GUID, a LONG and possibly an array following... fmt = "PP16sl" x['handle'], x['hdevnotify'], guid_bytes, x['nameoffset'] = \ struct.unpack(fmt, rest[:struct.calcsize(fmt)]) x['eventguid'] = pywintypes.IID(guid_bytes, True) elif devtype == win32con.DBT_DEVTYP_DEVICEINTERFACE: # guid, null-terminated name x['classguid'] = pywintypes.IID(rest[:16], 1) name = rest[16:] if '\0' in name: name = name.split('\0', 1)[0] x['name'] = name elif devtype == win32con.DBT_DEVTYP_VOLUME: # int mask and flags x['unitmask'], x['flags'] = struct.unpack("II", rest[:struct.calcsize("II")]) else: raise NotImplementedError("unknown device type %d" % (devtype,)) return DEV_BROADCAST_INFO(devtype, **extra)
def ReadRecord(d, offset=0x0): id = d[0] d=d[1:] # Eat id if id == 0xff or id == 0x4: # Normal end of Data return id, None, None sztotal = 1 assert RecPack.has_key(id), "Unknown record ID %i at offset %i" % (id, offset) if RecRepeat.has_key(id): sz = struct.calcsize(RecPack[id]) init=struct.unpack_from(RecRepeat[id][1], d) szinit=struct.calcsize(RecRepeat[id][1]) d=d[szinit:] sztotal += szinit res=[] for i in range(0, RecRepeat[id][0]): res.append(struct.unpack_from(RecPack[id], d)) d=d[sz:] sztotal += sz elif type(RecPack[id]) == str: sz = struct.calcsize(RecPack[id]) res = struct.unpack_from(RecPack[id], d) sztotal += sz elif type(RecPack[id]) == int: # 12-bit field array # A padding byte 0xFF may be present sz = RecPack[id] - 1 res = ReadPacked12Bit(d[:sz]) sztotal += sz return id, sztotal, res
def shake_hand(self, size=0, action="receive"): hi_msg_len = struct.calcsize(self.hi_format) ack_msg_len = struct.calcsize(self.ack_format) if action == "send": self.send(self.hi_msg) txt = self.receive(hi_msg_len) out = struct.unpack(self.hi_format, txt)[0] if out != "HI": raise ShakeHandError("Fail to get HI from guest.") size_s = struct.pack("q", size) self.send(size_s) txt = self.receive(ack_msg_len) ack_str = struct.unpack(self.ack_format, txt)[0] if ack_str != self.ack_msg: raise "Guest did not ACK the file size message." return size elif action == "receive": txt = self.receive(hi_msg_len) hi_str = struct.unpack(self.hi_format, txt)[0] if hi_str != self.hi_msg: raise ShakeHandError("Fail to get HI from guest.") self.send(txt) size = self.receive(8) print("xxxx size = %s" % size) if size: size = struct.unpack("q", size)[0] txt = struct.pack(self.ack_format, self.ack_msg) self.send(txt) return size
def PrintRecords(labels, s4, fmtHead, fmtTail="", printHex=True, printNorm=True): fmt = fmtHead szHead = struct.calcsize(fmtHead) szTail = struct.calcsize(fmtTail) printableHead = string.join([x for x in fmtHead if fmtmap.has_key(x)],"") printableTail = string.join([x for x in fmtTail if fmtmap.has_key(x)],"") if fmtTail != "": gap = len(s4[0]) - (struct.calcsize(fmtHead) + struct.calcsize(fmtTail)) fmt = fmtHead + ("x"*gap) + fmtTail labels = ["LINE"] + labels[:len(printableHead)] + labels[len(labels)-len(printableTail):] PrintMultiLineLabels(labels,6) sys.stdout.write(6*" ") PrintByteLabels(fmt, len(s4)) for i in range(0, len(s4)): if printNorm: sys.stdout.write("%5i:%s\n" % (i, StructToString(s4[i], fmt, 6))) if printHex: sys.stdout.write("\33[0m") sys.stdout.write(" %s\n" % (StructToString(s4[i], fmt, 6, color=False, hexonly=True))) if not ((i+1) % 40) or (i == len(s4) - 1): PrintMultiLineLabels(labels,6) sys.stdout.write(6*" ") PrintByteLabels(fmt, len(s4)) #HexPrintMod(s4[i][:szHead].tostring() + s4[i][len(s4[i]) - szTail:].tostring(), szHead + szTail) PrintByteStats(s4, fmt)
def _callfunc(self, fid, argsfmt, args, retfmt): """Call a function on the device by id, directly passing argument/return format parameters.""" # Make a list out of the arguments if they are none #print('calling function No. {}, args({}) {}, returning {}'.format(fid, argsfmt, args, retfmt)) if not (isinstance(args, tuple) or isinstance(args, list)): args = [args] with self._ser as s: # Send the encoded data cmd = b'\\#' + escape(struct.pack(">HHH", self.node_id, fid, struct.calcsize(argsfmt)) + struct.pack(argsfmt, *args)) s.write(cmd) # payload length (clen,) = struct.unpack(">H", s.read(2)) # payload data cbytes = s.read(clen) if clen != struct.calcsize(retfmt): # CAUTION! This error is thrown not because the user supplied a wrong value but because the device answered in an unexpected manner. # FIXME raise an error here or let the whole operation just fail in the following struct.unpack? raise AttributeError("Device response format problem: Length mismatch: {} != {}".format(clen, struct.calcsize(retfmt))) rv = struct.unpack(retfmt, cbytes) # Try to interpret the return value in a useful manner if len(rv) == 0: return None elif len(rv) == 1: return rv[0] else: return list(rv)
def unpack(self, data): n = struct.unpack('!I', data[:struct.calcsize('!I')])[0] items = struct.unpack('!%d%c' % (n, self.format_char),\ data[struct.calcsize('!I'):]) self.vals = [v for v in items] format_str = '!I%d%c' % (len(self.vals), self.format_char) self.struct = struct.Struct(format_str)
def __init__(self, idx, buf): self.inode_no = idx sb = buffer(bytearray(buf)) sz = 0 fmt = "<2H5I2H3I" (self.i_mode, self.i_uid, self.i_size, self.i_atime, self.i_ctime, self.i_mtime, self.i_dtime, self.i_gid, self.i_links_count, self.i_blocks, self.i_flags, self.i_osd1) = struct.unpack_from(fmt, sb, sz) sz += struct.calcsize(fmt) fmt = "<15I" self.i_block = struct.unpack_from(fmt, sb, sz) sz += struct.calcsize(fmt) fmt = "<4I12s" (self.i_gneration, self.i_file_acl, self.i_dir_acl, self.i_faddr, self.i_osd2) = struct.unpack_from(fmt, sb, sz)
def old_obs_callback(self, data, sender=None): print "Received deprecated observation messages. Please update your Piksi." if (sender is not None and (self.relay ^ (sender == 0))): return hdr_fmt = "<dH" hdr_size = struct.calcsize(hdr_fmt) tow, wn = struct.unpack("<dH", data[:hdr_size]) self.gps_tow = tow self.gps_week = wn self.t = datetime.datetime(1980, 1, 6) + \ datetime.timedelta(weeks=self.gps_week) + \ datetime.timedelta(seconds=self.gps_tow) # Observation message format # double P; /**< Pseudorange (m) */ # double L; /**< Carrier-phase (cycles) */ # float snr; /**< Signal-to-Noise ratio */ # u8 prn; /**< Satellite number. */ obs_fmt = '<ddfB' obs_size = struct.calcsize(obs_fmt) n_obs = (len(data) - hdr_size) / obs_size obs_data = data[hdr_size:] self.obs = {} for i in range(n_obs): P, L, snr, prn = struct.unpack(obs_fmt, obs_data[:obs_size]) obs_data = obs_data[obs_size:] self.obs[prn] = (P, L, snr) self.update_obs() self.rinex_save()
def deserialize(self, str): """ unpack serialized message in str into this message instance :param str: byte array of serialized message, ``str`` """ try: if self.timestamp is None: self.timestamp = genpy.Time() end = 0 _x = self start = end end += 8 (_x.timestamp.secs, _x.timestamp.nsecs,) = _struct_2I.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.state = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.covariance = struct.unpack(pattern, str[start:end]) self.timestamp.canon() return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def deserialize_numpy(self, str, numpy): """ unpack serialized message in str into this message instance using numpy for array types :param str: byte array of serialized message, ``str`` :param numpy: numpy python module """ try: if self.workspace_region_shape is None: self.workspace_region_shape = arm_navigation_msgs.msg.Shape() if self.workspace_region_pose is None: self.workspace_region_pose = geometry_msgs.msg.PoseStamped() end = 0 start = end end += 1 (self.workspace_region_shape.type,) = _struct_b.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.workspace_region_shape.dimensions = numpy.frombuffer(str[start:end], dtype=numpy.float64, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%si'%length start = end end += struct.calcsize(pattern) self.workspace_region_shape.triangles = numpy.frombuffer(str[start:end], dtype=numpy.int32, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) self.workspace_region_shape.vertices = [] for i in range(0, length): val1 = geometry_msgs.msg.Point() _x = val1 start = end end += 24 (_x.x, _x.y, _x.z,) = _struct_3d.unpack(str[start:end]) self.workspace_region_shape.vertices.append(val1) _x = self start = end end += 12 (_x.workspace_region_pose.header.seq, _x.workspace_region_pose.header.stamp.secs, _x.workspace_region_pose.header.stamp.nsecs,) = _struct_3I.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) start = end end += length if python3: self.workspace_region_pose.header.frame_id = str[start:end].decode('utf-8') else: self.workspace_region_pose.header.frame_id = str[start:end] _x = self start = end end += 56 (_x.workspace_region_pose.pose.position.x, _x.workspace_region_pose.pose.position.y, _x.workspace_region_pose.pose.position.z, _x.workspace_region_pose.pose.orientation.x, _x.workspace_region_pose.pose.orientation.y, _x.workspace_region_pose.pose.orientation.z, _x.workspace_region_pose.pose.orientation.w,) = _struct_7d.unpack(str[start:end]) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def deserialize_numpy(self, str, numpy): """ unpack serialized message in str into this message instance using numpy for array types :param str: byte array of serialized message, ``str`` :param numpy: numpy python module """ try: if self.timestamp is None: self.timestamp = genpy.Time() end = 0 _x = self start = end end += 8 (_x.timestamp.secs, _x.timestamp.nsecs,) = _struct_2I.unpack(str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.state = numpy.frombuffer(str[start:end], dtype=numpy.float64, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) self.covariance = numpy.frombuffer(str[start:end], dtype=numpy.float64, count=length) self.timestamp.canon() return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def deserialize_numpy(self, str, numpy): """ unpack serialized message in str into this message instance using numpy for array types :param str: byte array of serialized message, ``str`` :param numpy: numpy python module """ try: end = 0 start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%si'%length start = end end += struct.calcsize(pattern) self.handles = numpy.frombuffer(str[start:end], dtype=numpy.int32, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) start = end end += length if python3: self.setModes = str[start:end].decode('utf-8') else: self.setModes = str[start:end] start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sf'%length start = end end += struct.calcsize(pattern) self.values = numpy.frombuffer(str[start:end], dtype=numpy.float32, count=length) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def deserialize(self, str): """ unpack serialized message in str into this message instance :param str: byte array of serialized message, ``str`` """ try: end = 0 start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%si'%length start = end end += struct.calcsize(pattern) self.handles = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) start = end end += length if python3: self.setModes = str[start:end].decode('utf-8') else: self.setModes = str[start:end] start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sf'%length start = end end += struct.calcsize(pattern) self.values = struct.unpack(pattern, str[start:end]) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def writeData(data, datafile, dataformat=defformat): # Check given format string is valid try: bytesperpix = struct.calcsize(dataformat) except: raise ValueError, "Supplied data format " + str(dataformat) + " is invalid" # end try # Get the size in bytes for the given data format itemsize = struct.calcsize(dataformat) writestr = "" for i in range(0, len(data)): try: packeditem = struct.pack(dataformat, data[i]) except: datafile.close() raise IOError, "Could not pack " + str(dataitem) + " into " + str(bytesperpix) + " bytes. Reason: " + str(sys.exc_info()[1]) # end try if ((i % 1000 != 999) and (i != len(data) - 1)): writestr = writestr + packeditem else: writestr = writestr + packeditem try: datafile.write(writestr) except: datafile.close() raise IOError, "Failed to write to data file. Reason: " + str(sys.exc_info()[1]) # end try writestr = ""
def __init__(self, varlist): """ construct a SharedMemoryReader, given a variable list (names from ALMemory). Since this is done in async style, there is a deferred for completion of the init called self.openDeferred which can be used, i.e. self.openDeferred.addCallback(on_shm_inited) """ if not self.isMMapAvailable(): raise Exception("Don't initialize me if there is no MMAP!") self._shm_proxy = shm_proxy = burst.getBurstMemProxy(deferred=True) self._var_names = varlist self._fd = None self._buf = None self._unpack = 'f' * len(self._var_names) start_offset = BURST_SHARED_MEMORY_VARIABLES_START_OFFSET self._unpack_start = start_offset self._unpack_end = start_offset + struct.calcsize(self._unpack) self._sonar_unpack = 'f'*US_ELEMENTS_NUM self._sonar_start = 0 self._sonar_end = struct.calcsize(self._sonar_unpack) + self._sonar_start self.vars = dict((k, 0.0) for k in self._var_names) # TODO - ugly (next year) self.vars[US_DISTANCES_VARNAME] = [0.0] * US_ELEMENTS_NUM print "SharedMemory: asked burstmem to map %s variables" % len(self._var_names) # set all the names of the variables we want mapped - we don't block # or anything since we expect the first few frames to be eventless, # but this is definitely a TODO self.openDeferred = Deferred() shm_proxy.clearMappedVariables().addCallback(self._complete_init).addErrback(log.err)
def main(infile, outfile): # Read the cloop head pream, uncomp_buf_size, total_blocks = \ unpack(CLOOP_HEAD_FMT, infile.read(calcsize(CLOOP_HEAD_FMT))) comp_buf_size = uncomp_buf_size + uncomp_buf_size // 1000 + 12 + 4 total_offsets = total_blocks + 1 # Read the block offsets offsets_fmt = ('!%dQ' % total_offsets).encode('ascii') offsets = unpack(offsets_fmt, infile.read(calcsize(offsets_fmt))) # Pipe the blocks through the decompressor and onto the output block_modulo = total_blocks // 10 for i in range(total_blocks): size = offsets[i + 1] - offsets[i] if size < 0 or size > comp_buf_size: raise SystemExit( "Size %d for block %d (offset %d) wrong, corrupt data!" % (size, i, offsets[i]) ) comp_buf = infile.read(size) if len(comp_buf) != size: raise SystemExit("Error read %d data bytes" % size) uncomp_buf = zlib.decompress(comp_buf) outfile.write(uncomp_buf)
def load_mnist(im_path, lb_path): # loading images binfile = open(im_path, 'rb') buf = binfile.read() index = 0 magic,numImages,numRows,numColumns = \ struct.unpack_from('>IIII' , buf , index) index += struct.calcsize('>IIII') if magic!=2051: raise NameError('MNIST TRAIN-IMAGE INCCORECT!') ims = np.zeros([numImages, numRows*numColumns]) for i in range(numImages): ims[i,:] = struct.unpack_from('>784B', buf, index) index += struct.calcsize('>784B'); # loading labels binfile = open(lb_path, 'rb') buf = binfile.read() index = 0 magic,numLabels = struct.unpack_from( '>II', buf, index ) index += struct.calcsize('>II') if magic!=2049: raise NameError('MNIST TRAIN-LABEL INCORRECT!') lbs = np.zeros(numLabels) lbs[:] = struct.unpack_from( '>'+ str(numLabels) +'B', buf, index ) return [ims, numRows, numColumns, lbs]
def deserialize(self, str): """ unpack serialized message in str into this message instance :param str: byte array of serialized message, ``str`` """ try: if self.poses is None: self.poses = None end = 0 start = end end += 4 (length, ) = _struct_I.unpack(str[start:end]) self.poses = [] for i in range(0, length): val1 = geometry_msgs.msg.PoseStamped() _v6 = val1.header start = end end += 4 (_v6.seq, ) = _struct_I.unpack(str[start:end]) _v7 = _v6.stamp _x = _v7 start = end end += 8 ( _x.secs, _x.nsecs, ) = _struct_2I.unpack(str[start:end]) start = end end += 4 (length, ) = _struct_I.unpack(str[start:end]) start = end end += length if python3: _v6.frame_id = str[start:end].decode('utf-8') else: _v6.frame_id = str[start:end] _v8 = val1.pose _v9 = _v8.position _x = _v9 start = end end += 24 ( _x.x, _x.y, _x.z, ) = _struct_3d.unpack(str[start:end]) _v10 = _v8.orientation _x = _v10 start = end end += 32 ( _x.x, _x.y, _x.z, _x.w, ) = _struct_4d.unpack(str[start:end]) self.poses.append(val1) start = end end += 4 (length, ) = _struct_I.unpack(str[start:end]) pattern = '<%sh' % length start = end end += struct.calcsize(pattern) self.sizes = struct.unpack(pattern, str[start:end]) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
############################################################################ MAX_IDENT_LENGTH = 50 FRAME_TYPE_PROTOCOL_VERSION = 0 FRAME_TYPE_INCOMPATIBLE_PROTOCOL = 1 FRAME_TYPE_IDENTIFICATION = 2 FRAME_TYPE_MESSAGE = 3 FRAME_TYPE_PING = 4 FRAME_TYPE_P0NG = 5 FRAME_TYPE_TYPE = "B" FRAME_TYPE_SIZE = 1 # 1 byte FRAME_FORMAT_PROTOCOL_VERSION = "!I" FRAME_FORMAT_PROTOCOL_VERSION_SIZE = struct.calcsize(FRAME_FORMAT_PROTOCOL_VERSION) FRAME_FORMAT_MESSAGE = "!16sII" FRAME_FORMAT_MESSAGE_SIZE = struct.calcsize(FRAME_FORMAT_MESSAGE) MIN_FRAME_SIZE = 1 # just the type field INFINITE_TTL = 0xffffffff ENCODING = "utf-8" ############################################################################# ############################################################################# class Messaging(object): def __init__(self, identifier, domain, packeter, queues_storage=None): """
def deserialize(self, str): """ unpack serialized message in str into this message instance :param str: byte array of serialized message, ``str`` """ try: if self.mod_paths is None: self.mod_paths = None end = 0 start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) self.mod_paths = [] for i in range(0, length): val1 = ramp_msgs.msg.Path() start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) val1.points = [] for i in range(0, length): val2 = ramp_msgs.msg.KnotPoint() _v6 = val2.motionState start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v6.positions = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v6.velocities = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v6.accelerations = struct.unpack(pattern, str[start:end]) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v6.jerks = struct.unpack(pattern, str[start:end]) start = end end += 8 (_v6.time,) = _get_struct_d().unpack(str[start:end]) start = end end += 4 (val2.stopTime,) = _get_struct_I().unpack(str[start:end]) val1.points.append(val2) self.mod_paths.append(val1) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def deserialize_numpy(self, str, numpy): """ unpack serialized message in str into this message instance using numpy for array types :param str: byte array of serialized message, ``str`` :param numpy: numpy python module """ try: if self.poses is None: self.poses = None end = 0 start = end end += 4 (length, ) = _struct_I.unpack(str[start:end]) self.poses = [] for i in range(0, length): val1 = geometry_msgs.msg.PoseStamped() _v16 = val1.header start = end end += 4 (_v16.seq, ) = _struct_I.unpack(str[start:end]) _v17 = _v16.stamp _x = _v17 start = end end += 8 ( _x.secs, _x.nsecs, ) = _struct_2I.unpack(str[start:end]) start = end end += 4 (length, ) = _struct_I.unpack(str[start:end]) start = end end += length if python3: _v16.frame_id = str[start:end].decode('utf-8') else: _v16.frame_id = str[start:end] _v18 = val1.pose _v19 = _v18.position _x = _v19 start = end end += 24 ( _x.x, _x.y, _x.z, ) = _struct_3d.unpack(str[start:end]) _v20 = _v18.orientation _x = _v20 start = end end += 32 ( _x.x, _x.y, _x.z, _x.w, ) = _struct_4d.unpack(str[start:end]) self.poses.append(val1) start = end end += 4 (length, ) = _struct_I.unpack(str[start:end]) pattern = '<%sh' % length start = end end += struct.calcsize(pattern) self.sizes = numpy.frombuffer(str[start:end], dtype=numpy.int16, count=length) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
def read_field_data(self): endian = '>' nfields = self.master_header['nfields'] PROJ_LATLON = 0 PROJ_LAMBERT_CONF = 3 PROJ_FLAT = 8 PROJ_POLAR_RADAR = 9 PROJ_OBLIQUE_STEREO = 12 PROJ_RHI_RADAR = 13 I = self.data_types['ui32'] data = {} self.field_names = [] for i in range(nfields): proj_type = self.field_headers[i]['proj_type'] offset = self.field_headers[i]['field_data_offset'] nx = self.field_headers[i]['nx'] ny = self.field_headers[i]['ny'] nz = self.field_headers[i]['nz'] field_name = self.field_headers[i]['field_name'] encoding_type = self.field_headers[i]['encoding_type'] data_element_nbytes = self.field_headers[i]['data_element_nbytes'] transform_type = self.field_headers[i]['transform_type'] scaling_type = self.field_headers[i]['scaling_type'] scale = self.field_headers[i]['scale'] bias = self.field_headers[i]['bias'] self.mdvfile.seek(offset) vlevel_names = ['vlevel_offsets', 'vlevel_nbytes'] vlevel_types = [str(nz) + I] * 2 vlevel = self.read_mdv_bytes(vlevel_names, vlevel_types, endian) vlevel_nbytes = struct.calcsize("".join(vlevel_types)) vdata = np.ones((nz, ny, nx), dtype=np.float) for j in range(len(vlevel['vlevel_nbytes'])): voffset = offset + vlevel['vlevel_offsets'][j] + vlevel_nbytes self.mdvfile.seek(voffset) compression = self.get_compression_info() uncompressed_bytes = self.decompress_data(compression) sdata = np.array( self.scale_data(uncompressed_bytes, encoding_type, data_element_nbytes, transform_type, scaling_type, scale, bias)) vdata[j, :, :] = sdata.reshape((ny, nx)) if proj_type == PROJ_FLAT: vdata[j, :, :] = vdata[j, ::-1, :] elif proj_type == PROJ_POLAR_RADAR: vdata[j, :, :] = vdata[j, :, :] data[field_name] = vdata self.field_names.append(field_name) return data
def deserialize_numpy(self, str, numpy): """ unpack serialized message in str into this message instance using numpy for array types :param str: byte array of serialized message, ``str`` :param numpy: numpy python module """ try: if self.mod_paths is None: self.mod_paths = None end = 0 start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) self.mod_paths = [] for i in range(0, length): val1 = ramp_msgs.msg.Path() start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) val1.points = [] for i in range(0, length): val2 = ramp_msgs.msg.KnotPoint() _v8 = val2.motionState start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v8.positions = numpy.frombuffer(str[start:end], dtype=numpy.float64, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v8.velocities = numpy.frombuffer(str[start:end], dtype=numpy.float64, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v8.accelerations = numpy.frombuffer(str[start:end], dtype=numpy.float64, count=length) start = end end += 4 (length,) = _struct_I.unpack(str[start:end]) pattern = '<%sd'%length start = end end += struct.calcsize(pattern) _v8.jerks = numpy.frombuffer(str[start:end], dtype=numpy.float64, count=length) start = end end += 8 (_v8.time,) = _get_struct_d().unpack(str[start:end]) start = end end += 4 (val2.stopTime,) = _get_struct_I().unpack(str[start:end]) val1.points.append(val2) self.mod_paths.append(val1) return self except struct.error as e: raise genpy.DeserializationError(e) #most likely buffer underfill
from collections import defaultdict from collections import namedtuple from chipsec.logger import * from chipsec.file import * import chipsec.hal.acpi_tables import chipsec.hal.uefi class AcpiRuntimeError (RuntimeError): pass # ACPI Table Header Format ACPI_TABLE_HEADER_FORMAT = '=4sIBB6s8sI4sI' ACPI_TABLE_HEADER_SIZE = struct.calcsize(ACPI_TABLE_HEADER_FORMAT) # 36 assert( 36 == ACPI_TABLE_HEADER_SIZE ) class ACPI_TABLE_HEADER( namedtuple('ACPI_TABLE_HEADER', 'Signature Length Revision Checksum OEMID OEMTableID OEMRevision CreatorID CreatorRevision') ): __slots__ = () def __str__(self): return """ Table Header ------------------------------------------------------------------ Signature : %s Length : 0x%08X Revision : 0x%02X Checksum : 0x%02X OEM ID : %s OEM Table ID : %s OEM Revision : 0x%08X Creator ID : %s
def acquire(self, when): old_owner = self._owner() if when is self.IF_UNOWNED and old_owner != XNone: raise AlreadyOwned set_clipboard_data(self.clipboard, "VERSION") # Having acquired the selection, we have to announce our existence # (ICCCM 2.8, still). The details here probably don't matter too # much; I've never heard of an app that cares about these messages, # and metacity actually gets the format wrong in several ways (no # MANAGER or owner_window atoms). But might as well get it as right # as possible. # To announce our existence, we need: # -- the timestamp we arrived at # -- the manager selection atom # -- the window that registered the selection # Of course, because Gtk is doing so much magic for us, we have to do # some weird tricks to get at these. # Ask ourselves when we acquired the selection: contents = wait_for_contents(self.clipboard, "TIMESTAMP") ts_data = selectiondata_get_data(contents) #data is a timestamp, X11 datatype is Time which is CARD32, #(which is 64 bits on 64-bit systems!) Lsize = calcsize("@L") if len(ts_data)==Lsize: ts_num = unpack("@L", ts_data[:Lsize])[0] else: ts_num = 0 #CurrentTime log.warn("invalid data for 'TIMESTAMP': %s", ([hex(ord(x)) for x in ts_data])) # Calculate the X atom for this selection: selection_xatom = get_xatom(self.atom) # Ask X what window we used: self._xwindow = X11Window.XGetSelectionOwner(self.atom) root = self.clipboard.get_display().get_default_screen().get_root_window() xid = get_xwindow(root) X11Window.sendClientMessage(xid, xid, False, StructureNotifyMask, "MANAGER", ts_num, selection_xatom, self._xwindow) if old_owner != XNone and when is self.FORCE: # Block in a recursive mainloop until the previous owner has # cleared out. try: with xsync: window = get_pywindow(self.clipboard, old_owner) window.set_events(window.get_events() | STRUCTURE_MASK) log("got window") except XError: log("Previous owner is already gone, not blocking") else: log("Waiting for previous owner to exit...") add_event_receiver(window, self) gtk_main() log("...they did.") window = get_pywindow(self.clipboard, self._xwindow) window.set_title("Xpra-ManagerSelection")
def arch_bits(): return struct.calcsize("P")*8
MULTICAST_ADDRESS = "239.255.42.99" # IANA, local network PORT_COMMAND = 1510 PORT_DATA = 1511 # Default multicast group SOCKET_BUFSIZE = 0x100000 ### ### NatNet packet format ### ### # sPacket struct (PacketClient.cpp:65) # - iMessage (unsigned short), # - nDataBytes (unsigned short), # - union of possible payloads (MAX_PAYLOADSIZE bytes) PACKET_HEADER_FORMAT = "=2H" PACKET_FORMAT = PACKET_HEADER_FORMAT + ("%dB" % MAX_PAYLOADSIZE) MAX_PACKETSIZE = struct.calcsize(PACKET_FORMAT) # sender payload struct (PacketClient.cpp:57) # - szName (string MAX_NAMELENGTH), # - Version (4 unsigned chars), # - NatNetVersion (4 unsigned chars) SENDER_FORMAT = "=" + ("%ds" % MAX_NAMELENGTH) + "4B4B" SenderData = namedtuple("SenderData", "appname version natnet_version") # rigid body payload (PacketClient.cpp:586) # - id (int, 32 bits) # - x,y,z (3 floats, 3x32 bits) # - qx,qy,qz,qw (4 floats, 4x32 bits) RIGIDBODY_FORMAT = "=i3f4f" # RigidBody: # id is an integer
import weakref import pickle import operator import io import math import struct import sys import warnings import array from array import _array_reconstructor as array_reconstructor try: # Try to determine availability of long long independently # of the array module under test struct.calcsize('@q') have_long_long = True except struct.error: have_long_long = False sizeof_wchar = array.array('u').itemsize class ArraySubclass(array.array): pass class ArraySubclassWithKwargs(array.array): def __init__(self, typecode, newarg=None): array.array.__init__(self) typecodes = "ubBhHiIlLfd"
def GetSize(cls): """Calculate the size of the struct.""" format_str = "".join([x[0] for x in cls._fields]) return struct.calcsize(format_str)