本文整理汇总了Python中struct.Struct类的典型用法代码示例。如果您正苦于以下问题:Python Struct类的具体用法?Python Struct怎么用?Python Struct使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Struct类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_records
def write_records(records, format, f):
"""
Write a sequence of iterables to a binary file
"""
record_struct = Struct(format)
for rec in records:
f.write(record_struct.pack(*rec))
示例2: _read_mathp
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
示例3: __init__
def __init__(self, info, prefmt=''):
names, formats = zip(*info)
# Remove empty names
self._names = [n for n in names if n]
Struct.__init__(self, prefmt + ''.join(f for f in formats if f))
示例4: write_records
def write_records(records, format, f):
'''
Write a sequence of tuples to a binary file of structures.
'''
record_struct = Struct(format)
for r in records:
f.write(record_struct.pack(*r))
示例5: StreamSerializer
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))
示例6: read_elements_binary
def read_elements_binary(self, nelements):
self.nElementsRead = nelements
self.nElementsSkip = 0
size = nelements * 12 # 12=3*4 all the elements
elements = zeros(self.nElements*3, dtype='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
elements2 = elements.reshape((nelements, 3))
self.infile.read(8) # end of third (element) block, start of regions (fourth) block
return elements2
示例7: _read_grid
def _read_grid(self, data, n): # 21.8 sec, 18.9
"""(4501,45,1) - the marker for Record 17"""
s = Struct(b(self._endian + 'ii3f3i'))
ntotal = 32
nentries = (len(data) - n) // ntotal
self._increase_card_count('GRID', nentries)
for i in range(nentries):
edata = data[n:n + 32]
out = s.unpack(edata)
(nid, cp, x1, x2, x3, cd, ps, seid) = out
if self.is_debug_file:
self.binary_debug.write(' GRID=%s\n' % str(out))
if cd >= 0 and nid < 10000000:
if ps == 0:
ps = ''
node = GRID(nid, cp, np.array([x1, x2, x3]), cd, ps, seid)
self.nodes[nid] = node
#if nid in self.nodes:
#self.reject_lines.append(str(node))
#else:
self.nodes[nid] = node
#self.add_node(node)
else:
self.log.debug("*nid=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s seid=%s" %
(nid, cp, x1, x2, x3, cd, ps, seid))
n += ntotal
return n
示例8: zmf2dict
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
示例9: array_packing
def array_packing(arrdef, *more_arrdef):
"""pack mulltiple arrays into same str
take care of alignments between arrays
"""
arrtype, arr = arrdef
mystruct = Struct(arrtype)
last_bytes = mystruct.size
cur_size = last_bytes * len(arr)
mybuffer = StringIO()
mybuffer.write(''.join(mystruct.pack(val) for val in arr))
for arrtype, arr in more_arrdef:
mystruct = Struct(arrtype)
cur_bytes = mystruct.size
if cur_bytes > last_bytes:
# align the string
fill_bytes = align2pitch(cur_size, cur_bytes)
mybuffer.write(ALIGN_CHAR * fill_bytes)
cur_bytes += fill_bytes
# write this arr
cur_size = last_bytes * len(arr)
mybuffer.write(''.join(mystruct.pack(val) for val in arr))
# leave notes
last_bytes = cur_bytes
rtn = mybuffer.getvalue()
mybuffer.close()
return rtn
示例10: _read_real_table
def _read_real_table(self, data, result_name, flag, is_cid=False):
if self.debug4():
self.binary_debug.write(' _read_real_table\n')
assert flag in ['node', 'elem'], flag
n = 0
ntotal = 32 # 8 * 4
dt = self.nonlinear_factor
assert self.obj is not None
obj = self.obj
format1 = '2i6f' # 8
nnodes = len(data) // ntotal
assert nnodes > 0
#assert len(data) % ntotal == 0
s = Struct(format1)
for inode in range(nnodes):
edata = data[n:n+ntotal]
out = s.unpack(edata)
(eid_device, grid_type, tx, ty, tz, rx, ry, rz) = out
eid = (eid_device - self.device_code) // 10
if self.debug4():
self.binary_debug.write(' %s=%i; %s\n' % (flag, eid, str(out)))
obj.add(dt, eid, grid_type, tx, ty, tz, rx, ry, rz)
n += ntotal
return n
示例11: _read_complex_eigenvalue_4
def _read_complex_eigenvalue_4(self, data):
"""parses the Complex Eigenvalues Table 4 Data"""
if self.read_mode == 1:
return len(data)
ntotal = 4 * 6
nmodes = len(data) // ntotal
n = 0
#assert self.isubcase != 0, self.isubcase
clama = ComplexEigenvalues(11)
self.eigenvalues[self.Title] = clama
#self.eigenvalues[self.isubcase] = lama
s = Struct(b'ii4f')
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, eigr, eigc, freq, damping) = out # CLAMA
#print('imode=%s order=%s eigr=%s eigc=%s freq=%s damping=%s' %
#(imode, order, eigr, eigc, freq, damping))
clama.addF06Line(out)
n += ntotal
assert n == len(data), 'clama length error'
return n
示例12: write_records
def write_records(records, format, f):
'''
Zapis sekwencji krotek do pliku binarnego ze strukturami
'''
record_struct = Struct(format)
for r in records:
f.write(record_struct.pack(*r))
示例13: write_binary_stl
def write_binary_stl(self, stl_filename):
"""Write an STL binary file."""
infile = open(stl_filename, "wb")
if hasattr(self, 'header'):
self.header.ljust(80, '\0')
infile.write(self.header)
else:
header = '%-80s' % stl_filename
infile.write(pack('80s', header))
#avector = [0., 0., 0.]
#bvector = [0., 0., 0.]
#cvector = [0., 0., 0.]
nelements = self.elements.shape[0]
infile.write(pack('i', nelements))
elements = self.elements
p1 = self.nodes[elements[:, 0], :]
p2 = self.nodes[elements[:, 1], :]
p3 = self.nodes[elements[:, 2], :]
avector = p2 - p1
bvector = p3 - p1
n = cross(avector, bvector)
del avector, bvector
#n /= norm(n, axis=1)
s = Struct('12fH')
for eid, element in enumerate(elements):
data = s.pack(n[eid, 0], n[eid, 1], n[eid, 2],
p1[eid, 0], p1[eid, 1], p1[eid, 2],
p2[eid, 0], p2[eid, 1], p2[eid, 2],
p3[eid, 0], p3[eid, 1], p3[eid, 2], 0)
infile.write(data)
infile.close()
示例14: __init__
def __init__(self, fname):
self.fname = fname
dbg("reading blendomatic data from %s" % fname, 1, push="blendomatic")
fname = file_get_path(fname, write=False)
f = file_open(fname, binary=True, write=False)
buf = f.read(Blendomatic.blendomatic_header.size)
self.header = Blendomatic.blendomatic_header.unpack_from(buf)
blending_mode_count, tile_count = self.header
dbg("%d blending modes, each %d tiles" % (blending_mode_count, tile_count), 2)
blending_mode = Struct(endianness + "I %dB" % (tile_count))
self.blending_modes = list()
for i in range(blending_mode_count):
header_data = f.read(blending_mode.size)
bmode_header = blending_mode.unpack_from(header_data)
new_mode = BlendingMode(i, f, tile_count, bmode_header)
self.blending_modes.append(new_mode)
f.close()
dbg(pop="blendomatic")
示例15: read
def read(self, raw, offset):
self.data = dict()
#bool exists;
unit_header_header_struct0 = Struct(endianness + "?")
pc = unit_header_header_struct0.unpack_from(raw, offset)
offset += unit_header_header_struct0.size
self.data["exists"] = pc[0]
if self.data["exists"] == True:
unit_header_header_struct1 = Struct(endianness + "H")
pc = unit_header_header_struct1.unpack_from(raw, offset)
offset += unit_header_header_struct1.size
self.data["unit_command_count"] = pc[0]
self.data["unit_command"] = list()
for i in range(self.data["unit_command_count"]):
t = UnitCommand()
offset = t.read(raw, offset)
self.data["unit_command"] += [t.data]
return offset