python-snappy
python-snappy is a Python library that provides a high-performance wrapper for Google's Snappy compression library. It's designed for fast data compression and decompression, suitable for applications like data transfer, storage, and database indexing. The current version is 0.7.3. Releases are made periodically to maintain compatibility with newer Python versions and address underlying library changes.
Warnings
- gotcha The `python-snappy` library is a wrapper around the Snappy C++ library. You must have the underlying `snappy` library installed on your system before `pip install python-snappy` will succeed.
- gotcha `python-snappy` functions (like `snappy.compress` and `snappy.decompress`) only operate on `bytes` objects. Passing a `str` object will result in a `TypeError`.
- gotcha When using `StreamCompressor`, it's crucial to call `compressor.flush()` at the end of the data stream to ensure all buffered data is emitted. Failing to do so can lead to incomplete or corrupted compressed output.
- gotcha The latest versions of `python-snappy` (0.7.0+) explicitly require Python 3.7 or newer. Older Python 3 versions (e.g., 3.6) or Python 2 are no longer supported.
Install
-
pip install python-snappy -
sudo apt-get install libsnappy-dev # Debian/Ubuntu sudo yum install snappy-devel # RHEL/Fedora brew install snappy # macOS with Homebrew
Imports
- snappy
import snappy
- compress
snappy.compress
- decompress
snappy.decompress
- StreamCompressor
snappy.StreamCompressor
- StreamDecompressor
snappy.StreamDecompressor
Quickstart
import snappy
# Basic compression and decompression
original_data = b"This is some data to be compressed using snappy!" * 100
compressed_data = snappy.compress(original_data)
decompressed_data = snappy.decompress(compressed_data)
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size: {len(compressed_data)} bytes")
print(f"Decompressed data matches original: {original_data == decompressed_data}")
# Streaming compression and decompression
compressor = snappy.StreamCompressor()
compressed_stream_chunks = []
# Simulate sending data in chunks
for chunk in [b'part one', b'part two', b'part three']:
compressed_stream_chunks.append(compressor.compress(chunk))
compressed_stream_chunks.append(compressor.flush()) # Important to flush remaining data
full_compressed_stream = b''.join(compressed_stream_chunks)
decompressor = snappy.StreamDecompressor()
decompressed_stream_chunks = []
# Simulate receiving and decompressing data in chunks
for chunk in [full_compressed_stream[:15], full_compressed_stream[15:]]:
decompressed_stream_chunks.append(decompressor.decompress(chunk))
final_decompressed_stream = b''.join(decompressed_stream_chunks)
print(f"Stream decompressed data: {final_decompressed_stream}")
print(f"Stream decompressed data matches original: {final_decompressed_stream == b'part onepart twopart three'}")