本文整理汇总了Python中struct.Struct.unpack方法的典型用法代码示例。如果您正苦于以下问题:Python Struct.unpack方法的具体用法?Python Struct.unpack怎么用?Python Struct.unpack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类struct.Struct
的用法示例。
在下文中一共展示了Struct.unpack方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getDepthData
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def getDepthData(self):
encoded = self.meta['model']['depth_map']
# Decode
encoded += '=' * (len(encoded) % 4)
encoded = encoded.replace('-', '+').replace('_', '/')
data = encoded.decode('base64').decode('zip') # base64 encoded
# Read header
hsize = ord(data[0]) # header size in bytes
fmt = Struct('< x 3H B') # little endian, padding byte, 3x unsigned short int, unsigned char
n_planes, width, height, offset = fmt.unpack(data[:hsize])
# Read plane labels
n = width * height
fmt = Struct('%dB' % n)
lbls = fmt.unpack(data[offset:offset+fmt.size])
offset += fmt.size
# Read planes
fmt = Struct('< 4f') # little endian, 4 signed floats
planes = []
for i in xrange(n_planes):
unpacked = fmt.unpack(data[offset:offset+fmt.size])
planes.append((unpacked[:3], unpacked[3]))
offset += fmt.size
self.depthdata = (width, height), lbls, planes
return self.depthdata
示例2: read_image_optional_header
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def read_image_optional_header(cls, file):
format = Struct('<HBBIIIIIIIIIHHHHHHIIIIHHIIIIII')
directory_format = Struct('<II')
directory = []
partial_header = format.unpack(file.read(format.size))
directory = [cls._IMAGE_DATA_DIRECTORY(*directory_format.unpack(file.read(directory_format.size))) for i in range(16)]
return cls._IMAGE_OPTIONAL_HEADER(*(partial_header + (directory,)))
示例3: _read_mathp
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _read_mathp(self, data, n):
"""MATHP(4506,45,374) - Record 11"""
nmaterials = 0
s1 = Struct(b(self._endian + 'i7f3i23fi'))
s2 = Struct(b(self._endian + '8i'))
n2 = n
while n2 < n:
edata = data[n:n+140]
n += 140
out1 = s1.unpack(edata)
(mid, a10, a01, d1, rho, alpha, tref, ge, sf, na, nd, kp,
a20, a11, a02, d2,
a30, a21, a12, a03, d3,
a40, a31, a22, a13, a04, d4,
a50, a41, a32, a23, a14, a05, d5,
continue_flag) = out1
data_in = [out1]
if continue_flag:
edata = data[n:n+32] # 7*4
n += 32
out2 = s2.unpack(edata)
(tab1, tab2, tab3, tab4, x1, x2, x3, tab5) = out2
data_in.append(out2)
mat = MATHP.add_op2_data(data_in)
self.add_op2_material(mat)
nmaterials += 1
self.card_count['MATHP'] = nmaterials
return n
示例4: _readPBEAM
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _readPBEAM(self, data, n):
"""
PBEAM(5402,54,262) - the marker for Record 14
.. todo:: add object
"""
s1 = Struct(b(self._endian + '4if'))
s2 = Struct(b(self._endian + '16f'))
s3 = Struct(b(self._endian + '11f'))
ntotal = 1072 # 44+12*84+20
nproperties = (len(data) - n) // ntotal
for i in range(nproperties):
eData = data[n:n+20]
n += 20
dataIn = list(s1.unpack(eData))
self.binary_debug.write(' PBEAM=%s\n' % str(dataIn))
(pid, mid, nsegs, ccf, x) = dataIn
for i in range(12):
eData = data[n:n+64]
n += 64
pack = s2.unpack(eData)
(so, xxb, a, i1, i2, i12, j, nsm, c1, c2,
d1, d2, e1, e2, f1, f2) = pack
dataIn.append(pack)
self.binary_debug.write(' %s\n' % str(pack))
eData = data[n:n+44]
dataIn = list(s3.unpack(eData))
#(k1,k2,s1,s2,nsia,nsib,cwa,cwb,m1a,m2a,m1b,m2b,n1a,n2a,n1b,n2b) = pack
# prop = PBEAM(None, dataIn)
# self.addOp2Property(prop)
#sys.exit('ept-PBEAM')
self.card_count['PBEAM'] = nproperties
return n
示例5: zmf2dict
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def zmf2dict(fn):
"""Función que lee una librería de Zemax (archivo con terminación zmf), y genera un diccionario con las descripciones
de cada componente. La llave es la referencia de cada componente
"""
f=open(fn,"r")
rd={}
head = Struct("<I")
lens = Struct("<100sIIIIIIIdd")
shapes = "?EBPM"
version, = head.unpack(f.read(head.size))
assert version in (1001, )
while True:
li = f.read(lens.size)
if len(li) != lens.size:
if len(li) > 0:
print(f, "additional data", repr(li))
break
li = list(lens.unpack(li))
li[0] = li[0].decode("latin1").strip("\0")
li[3] = shapes[li[3]]
description = f.read(li[7])
assert len(description) == li[7]
description = zmf_obfuscate(description, li[8], li[9])
description = description.decode("latin1")
assert description.startswith("VERS {:06d}\n".format(li[1]))
rd[li[0]]=description
return rd
示例6: _readMATHP
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _readMATHP(self, data, n):
"""MATHP(4506,45,374) - Record 11"""
#print "reading MATHP"
nmaterials = 0
s1 = Struct(b(self._endian + 'i7f3i23fi'))
s2 = Struct(b(self._endian + '8i'))
n2 = n
while n2 < n:
eData = data[n:n+140]
n += 140
out1 = s1.unpack(eData)
(mid, a10, a01, d1, rho, alpha, tref, ge, sf, na, nd, kp,
a20, a11, a02, d2,
a30, a21, a12, a03, d3,
a40, a31, a22, a13, a04, d4,
a50, a41, a32, a23, a14, a05, d5,
continueFlag) = out1
dataIn = [out1]
if continueFlag:
eData = data[n:n+32] # 7*4
n += 32
out2 = s2.unpack(eData)
(tab1, tab2, tab3, tab4, x1, x2, x3, tab5) = out2
data.append(out2)
self.addOp2Material(MATHP(None, dataIn))
nmaterials += 1
self.card_count['MATHP'] = nmaterials
return n
示例7: _read_pcomp
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _read_pcomp(self, data, n):
"""
PCOMP(2706,27,287) - the marker for Record 22
"""
nproperties = 0
s1 = Struct(b(self._endian + "2i3fi2f"))
s2 = Struct(b(self._endian + "i2fi"))
ndata = len(data)
while n < (ndata - 32):
out = s1.unpack(data[n : n + 32])
(pid, nlayers, z0, nsm, sb, ft, Tref, ge) = out
if self.binary_debug:
self.log.debug("PCOMP pid=%s nlayers=%s z0=%s nsm=%s sb=%s ft=%s Tref=%s ge=%s" % tuple(out))
assert isinstance(nlayers, int), out
n += 32
mids = []
T = []
thetas = []
souts = []
# None, 'SYM', 'MEM', 'BEND', 'SMEAR', 'SMCORE', 'NO'
is_symmetrical = "NO"
if nlayers < 0:
is_symmetrical = "SYM"
nlayers = abs(nlayers)
assert nlayers > 0, out
assert 0 < nlayers < 100, "pid=%s nlayers=%s z0=%s nms=%s sb=%s ft=%s Tref=%s ge=%s" % (
pid,
nlayers,
z0,
nsm,
sb,
ft,
Tref,
ge,
)
for ilayer in range(nlayers):
(mid, t, theta, sout) = s2.unpack(data[n : n + 16])
mids.append(mid)
T.append(t)
thetas.append(theta)
souts.append(sout)
if self.is_debug_file:
self.binary_debug.write(" mid=%s t=%s theta=%s sout=%s" % (mid, t, theta, sout))
n += 16
data_in = [pid, z0, nsm, sb, ft, Tref, ge, is_symmetrical, mids, T, thetas, souts]
prop = PCOMP.add_op2_data(data_in)
self._add_op2_property(prop)
nproperties += 1
self.card_count["PCOMP"] = nproperties
return n
示例8: test_unpack_get_stats
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def test_unpack_get_stats(self):
"""
tc -s class show dev lo classid 1:1
"""
data1 = "\x24\x00\x00\x00\x2a\x00\x01\x03\x22\x08\xdc\x4d\x00\x00\x00\x00"
data2 = "\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
nlmsghdr = Struct("IHHII")
tcmsg = Struct("BxxxiIII")
nlmsghdr.unpack(data1)
tcmsg.unpack(data2)
示例9: _readPCOMP
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _readPCOMP(self, data, n):
"""
PCOMP(2706,27,287) - the marker for Record 22
"""
nproperties = 0
n2 = n
s1 = Struct(b"2i3fi2f")
s2 = Struct(b"i2fi")
while n2 < n: # len(data) >= 32: # 8*4 - dynamic
# print("len(data) = %s" % len(data))
# print(self.print_block(data[0:200]))
isSymmetrical = "NO"
eData = data[n : n + 32]
out = s1.unpack(eData)
self.binary_debug.write(" PCOMP=%s\n" % str(out))
(pid, nLayers, z0, nsm, sb, ft, Tref, ge) = out
eData = data[n : n + 16 * (nLayers)]
Mid = []
T = []
Theta = []
Sout = []
if nLayers < 0:
isSymmetrical = "YES"
nLayers = abs(nLayers)
# print("nLayers = ",nLayers)
assert 0 < nLayers < 100, "pid=%s nLayers=%s z0=%s nms=%s sb=%s ft=%s Tref=%s ge=%s" % (
pid,
nLayers,
z0,
nsm,
sb,
ft,
Tref,
ge,
)
idata = 0
for ilayer in range(nLayers):
(mid, t, theta, sout) = s2.unpack(eData[idata : idata + 16])
Mid.append(mid)
T.append(t)
Theta.append(theta)
Sout.append(sout)
idata += 16
dataIn = [pid, z0, nsm, sb, ft, Tref, ge, isSymmetrical, Mid, T, Theta, Sout]
# print("PCOMP = %s" % (dataIn))
prop = PCOMP(None, dataIn)
self.addOp2Property(prop)
nproperties += 1
self.card_count["PCOMP"] = nproperties
return n
示例10: unpackls
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def unpackls(data):
items = []
view = buffer(data)
shortformat = Struct('!H')
while view:
namelength = shortformat.unpack(view[:2])[0]
itemlength = namelength + 13
view = view[2:]
itemformat = Struct('!%dsLBQ' % namelength)
item = RemoteFile.unpack(*itemformat.unpack(view[:itemlength]))
items.append(item)
view = view[itemlength:]
return items
示例11: _read_pcomp
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _read_pcomp(self, data, n):
"""
PCOMP(2706,27,287) - the marker for Record 22
"""
nproperties = 0
s1 = Struct(b(self._endian + '2i3fi2f'))
s2 = Struct(b(self._endian + 'i2fi'))
ndata = len(data)
while n < (ndata - 32):
out = s1.unpack(data[n:n+32])
(pid, nlayers, z0, nsm, sb, ft, Tref, ge) = out
if self.binary_debug:
self.log.debug('PCOMP pid=%s nlayers=%s z0=%s nsm=%s sb=%s ft=%s Tref=%s ge=%s' % tuple(out))
assert isinstance(nlayers, int), out
n += 32
Mid = []
T = []
Theta = []
Sout = []
# None, 'SYM', 'MEM', 'BEND', 'SMEAR', 'SMCORE', 'NO'
is_symmetrical = 'NO'
if nlayers < 0:
is_symmetrical = 'SYM'
nlayers = abs(nlayers)
assert nlayers > 0, out
assert 0 < nlayers < 100, 'pid=%s nlayers=%s z0=%s nms=%s sb=%s ft=%s Tref=%s ge=%s' % (
pid, nlayers, z0, nsm, sb, ft, Tref, ge)
for ilayer in range(nlayers):
(mid, t, theta, sout) = s2.unpack(data[n:n+16])
Mid.append(mid)
T.append(t)
Theta.append(theta)
Sout.append(sout)
if self.is_debug_file:
self.binary_debug.write(' mid=%s t=%s theta=%s sout=%s' % (mid, t, theta, sout))
n += 16
data_in = [
pid, z0, nsm, sb, ft, Tref, ge,
is_symmetrical, Mid, T, Theta, Sout]
prop = PCOMP.add_op2_data(data_in)
self._add_op2_property(prop)
nproperties += 1
self.card_count['PCOMP'] = nproperties
return n
示例12: _read_tf
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _read_tf(self, data, n):
nfields = (len(data) - n) // 4
# subtract of the header (sid, nid, component, b0, b1, b2)
# divide by 5 (nid1, component1, a0, a1, a2)
#nrows = (nfields - 6) // 5
#print('n=%s nrows=%s' % (n, nrows))
#print(self.show_data(data))
#nid1, component1, a0, a1, a2
ndata = len(data)
struct1 = Struct(b'3i3f')
struct2 = Struct(b'2i3f')
while n < ndata:
n2 = n + 24 # 20=4*6
sid, nid, component, b0, b1, b2 = struct1.unpack(data[n:n2])
if self.is_debug_file:
self.binary_debug.write('TF header -> %s\n' % ([sid, nid, component, b0, b1, b2]))
nids = []
components = []
a = []
irow = 0
while 1:
n3 = n2 + 20 # 20=4*5
nid1, component1, a0, a1, a2 = struct2.unpack(data[n2:n3])
if self.is_debug_file:
self.binary_debug.write(' i=%s -> %s\n' % (
irow, [nid1, component1, a0, a1, a2]))
if nid1 == -1 and component1 == -1:
break
assert nid1 > -1
assert component1 > -1
nids.append(nid1)
components.append(component1)
a.append([a0, a1, a2])
n2 = n3
irow += 1
tf = TF(sid, nid, component, b0, b1, b2, nids, components, a)
#if self.is_debug_file:
#self.binary_debug.write('%s\n' % str(tf))
self._add_tf_object(tf)
self._increase_card_count('TF')
n = n3
return n
示例13: _read_buckling_eigenvalue_4
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def _read_buckling_eigenvalue_4(self, data):
# BLAMA - Buckling eigenvalue summary table
# CLAMA - Complex eigenvalue summary table
# LAMA - Normal modes eigenvalue summary table
if self.read_mode == 1:
return len(data)
msg = '_read_buckling_eigenvalue_4'
#return self._not_implemented_or_skip(data, msg) # TODO: implement buckling eigenvalues
ntotal = 4 * 7
nModes = len(data) // ntotal
n = 0
#assert self.isubcase != 0, self.isubcase
blama = BucklingEigenvalues(11)
self.eigenvalues[self.Title] = blama
#self.eigenvalues[self.isubcase] = lama
s = Struct(b'ii5f')
for i in range(nModes):
edata = data[n:n+ntotal]
out = s.unpack(edata)
if self.debug4():
self.binary_debug.write(' eigenvalue%s - %s\n' % (i, str(out)))
(iMode, order, eigen, omega, freq, mass, stiff) = out # BLAMA??
#(modeNum, extractOrder, eigenvalue, radian, cycle, genM, genK) = line # LAMA
#(rootNum, extractOrder, eigr, eigi, cycle, damping) = data # CLAMA
blama.addF06Line(out)
n += ntotal
return n
示例14: StreamSerializer
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
class StreamSerializer(object):
"""Helper to pass python objects over streams."""
length_format = '!i'
def __init__(self):
self.length_struct = Struct(self.length_format)
self.length = calcsize(self.length_format)
@staticmethod
def encode(obj):
return pickle.dumps(obj)
@staticmethod
def decode(message):
return pickle.loads(message)
def encode_with_length(self, obj):
"""Encode object and prepend length to message."""
message = self.encode(obj)
return self.length_struct.pack(len(message)) + message
def decode_from_stream(self, fd, timeout=5):
"""Read object from given stream and return it."""
rlist, _, _ = select([fd], [], [], timeout)
if not rlist:
raise RuntimeError("Can't read object from {0!r}.".format(fd))
message_length = self.length_struct.unpack(os.read(fd, self.length))[0]
assert message_length > 0, 'wrong message length provided'
return self.decode(os.read(fd, message_length))
示例15: read_elements_binary
# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import unpack [as 别名]
def read_elements_binary(self, nelements):
self.nElementsRead = nelements
self.nElementsSkip = 0
#print "starting read_elements"
#print self.infile.tell(), 'elements'
#isBuffered = True
size = nelements * 12 # 12=3*4 all the elements
elements = zeros(self.nElements*3, 'int32')
n = 0
s = Struct(b'>3000i')
while size > 12000: # 4k is 1000 elements
data = self.infile.read(4 * 3000)
nodes = s.unpack(data)
elements[n : n + 3000] = nodes
size -= 4 * 3000
n += 3000
assert size >= 0, 'size=%s' % size
if size > 0:
data = self.infile.read(size)
Format = b'>%ii' % (size // 4)
nodes = unpack(Format, data)
elements[n:] = nodes
#if isBuffered:
#pass
#else:
#raise RuntimeError('unBuffered')
elements2 = elements.reshape((nelements, 3))
self.infile.read(8) # end of third (element) block, start of regions (fourth) block
#print "finished read_elements"
return elements2