本文整理汇总了Python中struct.struct_pack函数的典型用法代码示例。如果您正苦于以下问题:Python struct_pack函数的具体用法?Python struct_pack怎么用?Python struct_pack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了struct_pack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setQuestion
def setQuestion(self, qname, qtype=QTYPE_A, qclass=QCLASS_IN):
for label in qname.split('.'):
label = str(label)
l = len(label)
self.buf += struct_pack('b', l)
self.buf += struct_pack(str(l)+'s', label)
self.buf += '\0'
self.buf += self._pack16bit(qtype)
self.buf += self._pack16bit(qclass)
示例2: tick
def tick(self,sample,sample2=None):
if (sample2 == None):
s1 = int(self.sampleScale*sample)
s1 = clip_value(s1,-self.sampleScale,self.sampleScale)
self.wavFile.writeframes(struct_pack(self.packFormat,s1))
return sample
else:
s1 = int(self.sampleScale*sample)
s1 = clip_value(s1,-self.sampleScale,self.sampleScale)
s2 = int(self.sampleScale*sample2)
s2 = clip_value(s2,-self.sampleScale,self.sampleScale)
self.wavFile.writeframes(struct_pack(self.packFormat,s1))
self.wavFile.writeframes(struct_pack(self.packFormat,s2))
return (sample,sample2)
示例3: encode_time
def encode_time(value):
if value.microsecond > 0:
val = (
value.microsecond |
value.second << 20 |
value.minute << 26 |
value.hour << 32
)
return DYN_COL_TIME, struct_pack('Q', val)[:6]
else:
val = (
value.second |
value.minute << 6 |
value.hour << 12
)
return DYN_COL_TIME, struct_pack('I', val)[:3]
示例4: pack_test
def pack_test(self, string, buffsize):
""" packs wireless request data for sending it to the kernel """
buffsize = buffsize - len(string)
buff = array('c', string+'\0'*buffsize)
caddr_t, length = buff.buffer_info()
s = struct_pack('Pii', caddr_t, length, 1)
return buff, s
示例5: bool_to_bitarray
def bool_to_bitarray(value):
"""
Converts a numpy boolean array to a bit array (a string of bits in
a bytes object).
Parameters
----------
value : numpy bool array
Returns
-------
bit_array : bytes
The first value in the input array will be the most
significant bit in the result. The length will be `floor((N +
7) / 8)` where `N` is the length of `value`.
"""
value = value.flat
bit_no = 7
byte = 0
bytes = []
for v in value:
if v:
byte |= 1 << bit_no
if bit_no == 0:
bytes.append(byte)
bit_no = 7
byte = 0
else:
bit_no -= 1
if bit_no != 7:
bytes.append(byte)
return struct_pack("%sB" % len(bytes), *bytes)
示例6: encode_float
def encode_float(value):
if isnan(value) or isinf(value):
raise DynColValueError("Float value not encodeable: {}".format(value))
encvalue = struct_pack('d', value)
# -0.0 is not supported in SQL, change to 0.0
if encvalue == b'\x00\x00\x00\x00\x00\x00\x00\x80':
encvalue = b'\x00\x00\x00\x00\x00\x00\x00\x00'
return DYN_COL_DOUBLE, encvalue
示例7: snappy_emit_backref
def snappy_emit_backref(f, offset, length):
if 4 <= length <= 11 and offset < 2048:
f.write(chr(1 | ((length - 4) << 2) | ((offset >> 8) << 5)))
f.write(chr(offset & 255))
else: # a back offset with offset > 65536 is not supported by this encoder!
encoded_offset = struct_pack("<H", offset)
while length > 0:
curr_len_chunk = min(length, 64)
f.write(chr(2 | ((curr_len_chunk - 1) << 2)))
f.write(encoded_offset)
length -= curr_len_chunk
示例8: pack_wrq
def pack_wrq(self, buffsize):
""" packs wireless request data for sending it to the kernel """
# Prepare a buffer
# We need the address of our buffer and the size for it. The
# ioctl itself looks for the pointer to the address in our
# memory and the size of it.
# Dont change the order how the structure is packed!!!
buff = array('c', '\0'*buffsize)
caddr_t, length = buff.buffer_info()
s = struct_pack('Pi', caddr_t, length)
return buff, s
示例9: pack_bytes_header
def pack_bytes_header(self, size):
stream = self.stream
if size < PLUS_2_TO_THE_8:
stream.write(BYTES_8)
stream.write(PACKED_UINT_8[size])
elif size < PLUS_2_TO_THE_16:
stream.write(BYTES_16)
stream.write(PACKED_UINT_16[size])
elif size < PLUS_2_TO_THE_32:
stream.write(BYTES_32)
stream.write(struct_pack(UINT_32_STRUCT, size))
else:
raise OverflowError("Bytes header size out of range")
示例10: a_qr_str
def a_qr_str(self, grupo_cat=None, con_dnis=True):
"""Devuelve la informacion del recuento para almacenar en qr."""
encoded_data = self.a_tag(grupo_cat, con_dnis)
datos = bytearray(b"")
# Agregamos el Token
datos.append(int(TOKEN, 16))
# agregamos el largo de los datos en 2 bytes
len_data = len(encoded_data) * 2
datos.extend(struct_pack(">H", len_data))
# metemos el resto de los datos
datos.extend(encoded_data)
# lo encodeamos en hexa y lo pasamos a mayúsculas
todo = encode(datos, "hex_codec").decode().upper()
return todo
示例11: pack_map_header
def pack_map_header(self, size):
stream = self.stream
if size < PLUS_2_TO_THE_4:
stream.write(TINY_MAP[size])
elif size < PLUS_2_TO_THE_8:
stream.write(MAP_8)
stream.write(PACKED_UINT_8[size])
elif size < PLUS_2_TO_THE_16:
stream.write(MAP_16)
stream.write(PACKED_UINT_16[size])
elif size < PLUS_2_TO_THE_32:
stream.write(MAP_32)
stream.write(struct_pack(UINT_32_STRUCT, size))
else:
raise OverflowError("Map header size out of range")
示例12: pack_string_header
def pack_string_header(self, size):
stream = self.stream
if size < PLUS_2_TO_THE_4:
stream.write(TINY_STRING[size])
elif size < PLUS_2_TO_THE_8:
stream.write(STRING_8)
stream.write(PACKED_UINT_8[size])
elif size < PLUS_2_TO_THE_16:
stream.write(STRING_16)
stream.write(PACKED_UINT_16[size])
elif size < PLUS_2_TO_THE_32:
stream.write(STRING_32)
stream.write(struct_pack(UINT_32_STRUCT, size))
else:
raise OverflowError("String header size out of range")
示例13: flush
def flush(self, end_of_message=False):
""" Flush everything written since the last chunk to the
stream, followed by a zero-chunk if required.
"""
output_buffer = self.output_buffer
if output_buffer:
lines = [struct_pack(">H", self.output_size)] + output_buffer
else:
lines = []
if end_of_message:
lines.append(b"\x00\x00")
if lines:
self.raw.writelines(lines)
self.raw.flush()
del output_buffer[:]
self.output_size = 0
示例14: encode_int
def encode_int(value):
if value < 0:
dtype = DYN_COL_INT
encvalue = -(value << 1) - 1
if value < -(2 ** 32 - 1):
raise DynColValueError("int {} out of range".format(value))
else:
if value <= (2 ** 63 - 1):
dtype = DYN_COL_INT
encvalue = value << 1
elif value <= (2 ** 64 - 1):
dtype = DYN_COL_UINT
encvalue = value
else:
raise DynColValueError("int {} out of range".format(value))
to_enc = []
while encvalue:
to_enc.append(encvalue & 0xff)
encvalue = encvalue >> 8
return dtype, struct_pack('B' * len(to_enc), *to_enc)
示例15: binoutput
def binoutput(self, value, mask):
if np.any(mask):
vo_warn(W39)
value = value.flat
bit_no = 7
byte = 0
bytes = []
for v in value:
if v:
byte |= 1 << bit_no
if bit_no == 0:
bytes.append(byte)
bit_no = 7
byte = 0
else:
bit_no -= 1
if bit_no != 7:
bytes.append(byte)
assert len(bytes) == self._bytes
return struct_pack("%sB" % len(bytes), *bytes)