本文整理汇总了Python中snappy.compress方法的典型用法代码示例。如果您正苦于以下问题:Python snappy.compress方法的具体用法?Python snappy.compress怎么用?Python snappy.compress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类snappy
的用法示例。
在下文中一共展示了snappy.compress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def encode(self, cmd_id: int, snappy_support: bool) -> MessageAPI:
raw_payload_data = self.serialization_codec.encode(self.payload)
if snappy_support:
payload_data = self.compression_codec.compress(raw_payload_data)
else:
payload_data = raw_payload_data
cmd_id_data = rlp.encode(cmd_id, sedes=rlp.sedes.big_endian_int)
frame_size = len(cmd_id_data) + len(payload_data)
if frame_size.bit_length() > 24:
raise ValueError("Frame size has to fit in a 3-byte integer")
# Frame-size is a 3-bit integer
header = frame_size.to_bytes(3, 'big') + RLPX_HEADER_DATA
body = cmd_id_data + payload_data
return Message(header, body)
示例2: read
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def read(self, _):
# Stop copying when the bevy is full.
if self.chunk_count_in_bevy >= self.owner.chunks_per_segment:
return ""
chunk = self.stream.read(self.owner.chunk_size)
if not chunk:
return ""
self.size += len(chunk)
if self.owner.compression == lexicon.AFF4_IMAGE_COMPRESSION_ZLIB:
compressed_chunk = zlib.compress(chunk)
elif (snappy and self.owner.compression ==
lexicon.AFF4_IMAGE_COMPRESSION_SNAPPY):
compressed_chunk = snappy.compress(chunk)
elif self.owner.compression == lexicon.AFF4_IMAGE_COMPRESSION_STORED:
compressed_chunk = chunk
compressedLen = len(compressed_chunk)
self.chunk_count_in_bevy += 1
if compressedLen < self.owner.chunk_size - 16:
self.bevy_index.append((self.bevy_length, compressedLen))
self.bevy_length += compressedLen
return compressed_chunk
else:
self.bevy_index.append((self.bevy_length, self.owner.chunk_size))
self.bevy_length += self.owner.chunk_size
return chunk
示例3: FlushChunk
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def FlushChunk(self, chunk):
if len(chunk) == 0:
return
bevy_offset = self.bevy_length
if self.compression == lexicon.AFF4_IMAGE_COMPRESSION_ZLIB:
compressed_chunk = zlib.compress(chunk)
elif (snappy and self.compression ==
lexicon.AFF4_IMAGE_COMPRESSION_SNAPPY):
compressed_chunk = snappy.compress(chunk)
elif self.compression == lexicon.AFF4_IMAGE_COMPRESSION_STORED:
compressed_chunk = chunk
compressedLen = len(compressed_chunk)
if compressedLen < self.chunk_size - 16:
self.bevy_index.append((bevy_offset, compressedLen))
self.bevy.append(compressed_chunk)
self.bevy_length += compressedLen
else:
self.bevy_index.append((bevy_offset, self.chunk_size))
self.bevy.append(chunk)
self.bevy_length += self.chunk_size
#self.bevy_index.append((bevy_offset, len(compressed_chunk)))
#self.bevy.append(compressed_chunk)
#self.bevy_length += len(compressed_chunk)
self.chunk_count_in_bevy += 1
#self.buffer = chunk[self.chunk_size:]
if self.chunk_count_in_bevy >= self.chunks_per_segment:
self._FlushBevy()
示例4: compress
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def compress(self, data):
if self._message_encoding == "gzip" or self._message_encoding == "deflate":
import zlib
return zlib.compress(data)
elif self._message_encoding == "snappy":
import snappy
return snappy.compress(data)
else:
raise UnsupportedMessageEncodingError(
"Unsupported compression: {}".format(self._message_encoding))
示例5: encode
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def encode(self, obj, container):
return self._compressor.compress(
self._format_encoder.encode(obj, container))
示例6: write_message
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def write_message(self, data: bytes, compress=False):
if compress:
data = self.compress(data)
if len(data) > self._max_message_length:
raise MessageTooLargeError(
"Trying to send message larger than max: {message_length} > {max_message_length}".format(
message_length=len(data),
max_message_length=self._max_message_length,
)
)
self._buffer.append(struct.pack('>?I', compress, len(data)))
self._buffer.append(data)
示例7: _zlib_no_compress
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def _zlib_no_compress(data):
"""Compress data with zlib level 0."""
cobj = zlib.compressobj(0)
return b"".join([cobj.compress(data), cobj.flush()])
示例8: compress
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def compress(data):
return snappy.compress(data)
示例9: __init__
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def __init__(self, level):
# Jython zlib.compress doesn't support -1
if level == -1:
self.compress = zlib.compress
# Jython zlib.compress also doesn't support 0
elif level == 0:
self.compress = _zlib_no_compress
else:
self.compress = lambda data: zlib.compress(data, level)
示例10: compress
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def compress(self, data):
return data
示例11: _pack_msgpack_snappy
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def _pack_msgpack_snappy(obj):
# print "pack", obj
tmp = msgpack.dumps(obj, encoding='utf-8')
if len(tmp) > 1000:
return b'S' + snappy.compress(tmp)
else:
return b'\0' + tmp
示例12: compress
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def compress(data):
"""
Compresses given data via the snappy algorithm.
The result is preceded with a header containing the string 'SNAPPY' and the
default and min-compat versions (both ``1``).
The block size for the compression is hard-coded at 32kb.
If ``python-snappy`` is not installed a ``RuntimeError`` is raised.
"""
if not snappy_available:
raise RuntimeError("Snappy compression unavailable.")
buff = BytesIO()
buff.write(raw_header)
for block_num in range(0, len(data), BLOCK_SIZE):
block = data[block_num:block_num + BLOCK_SIZE]
compressed = snappy.compress(block)
buff.write(struct.pack("!i", len(compressed)))
buff.write(compressed)
result = buff.getvalue()
buff.close()
return result
示例13: _compress_event
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def _compress_event(self, event: BaseEvent) -> Union[BaseEvent, bytes]:
if self.has_snappy_support:
import snappy
return cast(
bytes,
snappy.compress(pickle.dumps(event, protocol=pickle.HIGHEST_PROTOCOL)),
)
else:
return event
示例14: _serialize_gossip
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def _serialize_gossip(operation: ssz.Serializable) -> bytes:
encoded_operation = ssz.encode(operation)
return snappy.compress(encoded_operation)
示例15: compress
# 需要导入模块: import snappy [as 别名]
# 或者: from snappy import compress [as 别名]
def compress(self, data: bytes) -> bytes:
return snappy.compress(data)