{"id":1683,"library":"python-snappy","title":"python-snappy","description":"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.","status":"active","version":"0.7.3","language":"en","source_language":"en","source_url":"https://github.com/intake/python-snappy","tags":["compression","snappy","data-processing","high-performance"],"install":[{"cmd":"pip install python-snappy","lang":"bash","label":"Install `python-snappy`"},{"cmd":"sudo apt-get install libsnappy-dev  # Debian/Ubuntu\nsudo yum install snappy-devel       # RHEL/Fedora\nbrew install snappy                # macOS with Homebrew","lang":"bash","label":"Install system-level Snappy library (prerequisite)"}],"dependencies":[],"imports":[{"symbol":"snappy","correct":"import snappy"},{"note":"Called as a function from the top-level 'snappy' module.","symbol":"compress","correct":"snappy.compress"},{"note":"Called as a function from the top-level 'snappy' module.","symbol":"decompress","correct":"snappy.decompress"},{"note":"Used for incremental compression of data streams.","symbol":"StreamCompressor","correct":"snappy.StreamCompressor"},{"note":"Used for incremental decompression of data streams.","symbol":"StreamDecompressor","correct":"snappy.StreamDecompressor"}],"quickstart":{"code":"import snappy\n\n# Basic compression and decompression\noriginal_data = b\"This is some data to be compressed using snappy!\" * 100\ncompressed_data = snappy.compress(original_data)\ndecompressed_data = snappy.decompress(compressed_data)\n\nprint(f\"Original size: {len(original_data)} bytes\")\nprint(f\"Compressed size: {len(compressed_data)} bytes\")\nprint(f\"Decompressed data matches original: {original_data == decompressed_data}\")\n\n# Streaming compression and decompression\ncompressor = snappy.StreamCompressor()\ncompressed_stream_chunks = []\n\n# Simulate sending data in chunks\nfor chunk in [b'part one', b'part two', b'part three']:\n    compressed_stream_chunks.append(compressor.compress(chunk))\ncompressed_stream_chunks.append(compressor.flush()) # Important to flush remaining data\n\nfull_compressed_stream = b''.join(compressed_stream_chunks)\n\ndecompressor = snappy.StreamDecompressor()\ndecompressed_stream_chunks = []\n\n# Simulate receiving and decompressing data in chunks\nfor chunk in [full_compressed_stream[:15], full_compressed_stream[15:]]:\n    decompressed_stream_chunks.append(decompressor.decompress(chunk))\n\nfinal_decompressed_stream = b''.join(decompressed_stream_chunks)\nprint(f\"Stream decompressed data: {final_decompressed_stream}\")\nprint(f\"Stream decompressed data matches original: {final_decompressed_stream == b'part onepart twopart three'}\")","lang":"python","description":"This quickstart demonstrates basic one-shot compression/decompression and the use of `StreamCompressor` and `StreamDecompressor` for handling larger data in chunks. Ensure the underlying Snappy C library is installed first."},"warnings":[{"fix":"Install `libsnappy-dev` (Debian/Ubuntu), `snappy-devel` (RHEL/Fedora), or `snappy` (macOS via Homebrew) using your system's package manager before installing the Python package.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure all data intended for compression or decompression is of type `bytes`. Use `my_string.encode('utf-8')` to convert strings to bytes.","message":"`python-snappy` functions (like `snappy.compress` and `snappy.decompress`) only operate on `bytes` objects. Passing a `str` object will result in a `TypeError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always append the result of `compressor.flush()` to your compressed data stream after all chunks have been processed by `compressor.compress()`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your environment is running Python 3.7 or newer. If you must use an older Python version, you might need to pin `python-snappy` to an older compatible version (e.g., `<0.7.0`).","message":"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.","severity":"gotcha","affected_versions":"0.7.0+"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}