本文整理汇总了Python中struct.unpack_from函数的典型用法代码示例。如果您正苦于以下问题:Python unpack_from函数的具体用法?Python unpack_from怎么用?Python unpack_from使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unpack_from函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_udp_connection
def _get_udp_connection(self, host, port):
# build connection request payload
transaction_id = int(randrange(0, 255))
buff = struct.pack('!q', CONNECTION_ID)
buff += struct.pack('!i', ACTION_CONNECT)
buff += struct.pack('!i', transaction_id)
# send payload and get response
self._socket.sendto(buff, (host, port))
try:
response = self._socket.recv(2048)
except socket.timeout:
# TODO: issue warning here
print "tracker down: %s" % host
return None
if len(response) < 16:
# TODO: issue warning here
print "wrong response length"
return None
# extract response information
resp_action, resp_transaction_id = struct.unpack_from('!ii', response, 0)
if transaction_id != resp_transaction_id:
# TODO: issue warning instead
raise ValueError('Transaction IDs do not match (req=%d resp=%d)' % (transaction_id, resp_transaction_id))
if resp_action == ACTION_ERROR:
error = struct.unpack_from('!s', response, 8)[0]
# TODO: issue warning instead
raise RuntimeError('Unable to setup a connection: %s' % error)
elif resp_action == ACTION_CONNECT:
connection_id = struct.unpack_from('!q', response, 8)[0]
return connection_id
return None
示例2: _parse_hpr_time_series
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]}))
示例3: unpack
def unpack(self, info):
self._tag, = unpack_from('>c', info)
skip = 1
if self._tag in b'BCDFIJSZs':
self._value, = unpack_from('>H', info[skip:])
skip += 2
elif self._tag == b'e':
self._value = unpack_from('>HH', info[skip:])
skip += 4
elif self._tag == b'c':
self._value, = unpack_from('>H', info[skip:])
skip += 2
elif self._tag == b'@':
annotation = RuntimeVisibleAnnotation(self._cf)
skip += annotation.unpack(info[skip:])
self._value = annotation
elif self._tag == b'[':
num_values, = unpack_from('>H', info[skip:])
skip += 2
values = []
for n in range(num_values):
value = ElementValue(self._cf)
skip += value.unpack(info[skip:])
values.append(value)
self._value = values
else:
raise ValueError("Unknown ElementValue tag {}".format(self._tag))
return skip
示例4: script_GetOp
def script_GetOp(bytes):
i = 0
while i < len(bytes):
vch = None
opcode = ord(bytes[i])
i += 1
if opcode >= opcodes.OP_SINGLEBYTE_END:
opcode <<= 8
opcode |= ord(bytes[i])
i += 1
if opcode <= opcodes.OP_PUSHDATA4:
nSize = opcode
if opcode == opcodes.OP_PUSHDATA1:
nSize = ord(bytes[i])
i += 1
elif opcode == opcodes.OP_PUSHDATA2:
(nSize,) = struct.unpack_from('<H', bytes, i)
i += 2
elif opcode == opcodes.OP_PUSHDATA4:
(nSize,) = struct.unpack_from('<I', bytes, i)
i += 4
vch = bytes[i:i+nSize]
i += nSize
yield (opcode, vch, i)
示例5: Run
def Run(self, args):
"""Run."""
# This action might crash the box so we need to flush the transaction log.
self.SyncTransactionLog()
# Do any initialization we need to do.
logging.debug("Querying device %s", args.path)
fd = win32file.CreateFile(
args.path,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
None,
win32file.OPEN_EXISTING,
win32file.FILE_ATTRIBUTE_NORMAL,
None)
data = win32file.DeviceIoControl(fd, INFO_IOCTRL, "", 1024, None)
fmt_string = "QQl"
cr3, _, number_of_runs = struct.unpack_from(fmt_string, data)
result = rdfvalue.MemoryInformation(
cr3=cr3,
device=rdfvalue.PathSpec(
path=args.path,
pathtype=rdfvalue.PathSpec.PathType.MEMORY))
offset = struct.calcsize(fmt_string)
for x in range(number_of_runs):
start, length = struct.unpack_from("QQ", data, x * 16 + offset)
result.runs.Append(offset=start, length=length)
self.SendReply(result)
示例6: load_label_data_set
def load_label_data_set(self, label_data_dir):
logging.info("load label set from {0}.".format(label_data_dir))
with open(label_data_dir, "rb") as binary_file_handle:
label_data_buffer = binary_file_handle.read()
head = struct.unpack_from('>II' , label_data_buffer ,0)
logging.info("head:{0}".format(head))
label_num = head[1]
logging.info("img_num:{0}".format(label_num))
offset = struct.calcsize('>II')
logging.info("offset:{0}".format(offset))
img_num_string='>'+str(label_num)+"B"
logging.info("img_num_string:{0}".format(img_num_string))
all_label_2d_ndarray = struct.unpack_from(img_num_string, label_data_buffer, offset)
all_label_2d_ndarray = np.reshape(all_label_2d_ndarray, [label_num, 1])
logging.info("len(all_label_2d_ndarray):{0}".format(len(all_label_2d_ndarray)))
logging.info("type(all_label_2d_ndarray):{0}".format(type(all_label_2d_ndarray)))
logging.info("all_label_2d_ndarray[0]:{0}".format(all_label_2d_ndarray[0]))
logging.info("type(all_label_2d_ndarray[0]):{0}".format(type(all_label_2d_ndarray[0])))
logging.info("all_label_2d_ndarray[0][0]:{0}".format(all_label_2d_ndarray[0][0]))
logging.info("type(all_label_2d_ndarray[0][0]):{0}".format(type(all_label_2d_ndarray[0][0])))
logging.info("Load label finished.")
return all_label_2d_ndarray
示例7: __init__
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)
示例8: __init__
def __init__(self, data):
self.ident = data[:4]
self.record_size, self.type, self.count, self.encoding = unpack_from(b'>IHHI', data, 4)
self.encoding = {
1252 : 'cp1252',
65001: 'utf-8',
}.get(self.encoding, repr(self.encoding))
rest = list(unpack_from(b'>IIIIIIII', data, 16))
self.num_of_resource_records = rest[2]
self.num_of_non_dummy_resource_records = rest[3]
self.offset_to_href_record = rest[4]
self.unknowns1 = rest[:2]
self.unknowns2 = rest[5]
self.header_length = rest[6]
self.title_length = rest[7]
self.resources = []
self.hrefs = []
if data[48:52] == b'EXTH':
self.exth = EXTHHeader(data[48:])
self.title = data[48 + self.exth.length:][:self.title_length].decode(self.encoding)
self.is_image_container = self.exth[539] == 'application/image'
else:
self.exth = ' No EXTH header present '
self.title = ''
self.is_image_container = False
self.bytes_after_exth = data[self.header_length + self.title_length:]
self.null_bytes_after_exth = len(self.bytes_after_exth) - len(self.bytes_after_exth.replace(b'\0', b''))
示例9: script_GetOp
def script_GetOp(bytes):
i = 0
while i < len(bytes):
vch = None
opcode = ord(bytes[i])
i += 1
if opcode <= opcodes.OP_PUSHDATA4:
nSize = opcode
if opcode == opcodes.OP_PUSHDATA1:
nSize = ord(bytes[i])
i += 1
elif opcode == opcodes.OP_PUSHDATA2:
(nSize,) = struct.unpack_from('<H', bytes, i)
i += 2
elif opcode == opcodes.OP_PUSHDATA4:
(nSize,) = struct.unpack_from('<I', bytes, i)
i += 4
if i+nSize > len(bytes):
vch = "_INVALID_"+bytes[i:]
i = len(bytes)
else:
vch = bytes[i:i+nSize]
i += nSize
yield (opcode, vch, i)
示例10: ReadRecord
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
示例11: _read_tag_data_info
def _read_tag_data_info(dmfile):
tag_array_length = struct.unpack_from('>Q', dmfile.read(8))[0] #DM4 specifies this property as always big endian
format_str = '>' + tag_array_length * 'q' #Big endian signed long
tag_array_types = struct.unpack_from(format_str, dmfile.read(8*tag_array_length))
return (tag_array_length, tag_array_types)
示例12: parseString
def parseString(db, offset):
existence = unpack_from('b', db, offset)[0]
if existence == 0x00:
return ("", offset+1)
elif existence == 0x0b:
# decode ULEB128
length = 0
shift = 0
offset += 1
while True:
val = unpack_from('B', db, offset)[0]
length |= ((val & 0x7F) << shift)
offset += 1
if (val & (1 << 7)) == 0:
break
shift += 7
string = unpack_from(str(length)+'s', db, offset)[0]
offset += length
unic = u''
try:
unic = unicode(string, 'utf-8')
except UnicodeDecodeError:
print "Could not parse UTF-8 string, returning empty string."
return (unic, offset)
示例13: sec_info
def sec_info(self, secnum):
start_offset, flgval = struct.unpack_from('>2L', self.datain, 78+(secnum*8))
if secnum == self.num_sections:
next_offset = len(self.datain)
else:
next_offset, nflgval = struct.unpack_from('>2L', self.datain, 78+((secnum+1)*8))
return start_offset, flgval, next_offset
示例14: parse_resp
def parse_resp(self,buf):
header_offset = 2
avp_offset = 8
nr = 0
ns = 0
# read the header
(cflag,) = struct.unpack_from('!H', buf)
cflag_bin = int2bin(cflag,16)
ptype = cflag_bin[0]
blen = cflag_bin[1]
sbit = cflag_bin[4]
obit = cflag_bin[6]
pbit = cflag_bin[7]
ver = cflag_bin[12:16]
if self.debug:
print "<- l2tp packet dump"
print "<-: l2tp cflag bits : %s|%s|%s|%s|%s|%s" % (ptype, blen, sbit, obit, pbit, ver)
if ver != '0010': #
print '!! Not an valid l2tp packet : discarding'
return None
if blen == '1':
(plen,) = struct.unpack_from('!H', buf, offset=header_offset)
if self.debug:
print "<-: l2tp length : %d" % plen
header_offset += 2
(tid, sid) = struct.unpack_from('!HH', buf, offset=header_offset)
if self.debug:
print "<-: l2tp tunnel_id : %d, session_id : %d" % (tid, sid)
header_offset += 4
if sbit == '1':
(ns, nr) = struct.unpack_from('!HH', buf, offset=header_offset)
if self.debug:
print "<-: l2tp ns : %d, nr : %d" % (ns, nr)
header_offset += 4
avp_offset += 4
if obit == '1':
(offset_size, offset_pad) = struct.unpack_from('!HH', buf, offset=header_offset)
if self.debug:
print "<-: l2tp offset_size : %d, offset_pad : %d" % (offset_size, offset_pad)
header_offset += 4
avp_offset += 4
if ptype == '0': # data packet
# write to pppd
data = buf[header_offset:]
try:
async_buf = self.pppd_sync_to_async(data)
pty._writen(self.pppd_fd, async_buf)
except OSError, se:
if se.args[0] not in (errno.EAGAIN, errno.EINTR):
raise
示例15: _tag_aux
def _tag_aux(fp, tb):
bytes_read = 1
tag = tb & CBOR_TYPE_MASK
tag_aux = tb & CBOR_INFO_BITS
if tag_aux <= 23:
aux = tag_aux
elif tag_aux == CBOR_UINT8_FOLLOWS:
data = fp.read(1)
aux = struct.unpack_from("!B", data, 0)[0]
bytes_read += 1
elif tag_aux == CBOR_UINT16_FOLLOWS:
data = fp.read(2)
aux = struct.unpack_from("!H", data, 0)[0]
bytes_read += 2
elif tag_aux == CBOR_UINT32_FOLLOWS:
data = fp.read(4)
aux = struct.unpack_from("!I", data, 0)[0]
bytes_read += 4
elif tag_aux == CBOR_UINT64_FOLLOWS:
data = fp.read(8)
aux = struct.unpack_from("!Q", data, 0)[0]
bytes_read += 8
else:
assert tag_aux == CBOR_VAR_FOLLOWS, "bogus tag {0:02x}".format(tb)
aux = None
return tag, tag_aux, aux, bytes_read