#python-bindings #brotli #algorithm #snappy #thin #aws-lambda #lz4

cramjam

Thin Python bindings to de/compression algorithms in Rust

8 stable releases

2.4.0 Sep 10, 2021
2.3.1 May 18, 2021
2.3.0 Apr 15, 2021
2.2.0 Mar 30, 2021
1.1.0 May 3, 2020

#363 in Compression

Download history 38/week @ 2024-02-21 17/week @ 2024-02-28

55 downloads per month

MIT license

1MB
1.5K SLoC

Rust 1.5K SLoC // 0.0% comments Python 222 SLoC // 0.1% comments

pyrus-cramjam

Code Style CI PyPI Anaconda-Server Badge Downloads

API Documentation

Install

pip install --upgrade cramjam  # Requires no Python or system dependencies!

Extremely thin Python bindings to de/compression algorithms in Rust. Allows for using algorithms such as Snappy, without any system dependencies.

This is handy when being used in environments like AWS Lambda, where installing packages like python-snappy becomes difficult because of system level dependencies.


Benchmarks

Some basic benchmarks are available in the benchmarks directory


Available algorithms:

  • Snappy
  • Brotli
  • Lz4
  • Gzip
  • Deflate
  • ZSTD

All available for use as:

>>> import cramjam
>>> import numpy as np
>>> compressed = cramjam.snappy.compress(b"bytes here")
>>> decompressed = cramjam.snappy.decompress(compressed)
>>> decompressed
cramjam.Buffer(len=10)  # an object which implements the buffer protocol
>>> bytes(decompressed)
b"bytes here"
>>> np.frombuffer(decompressed, dtype=np.uint8)
array([ 98, 121, 116, 101, 115,  32, 104, 101, 114, 101], dtype=uint8)

Where the API is cramjam.<compression-variant>.compress/decompress and accepts bytes/bytearray/numpy.array/cramjam.File/cramjam.Buffer objects.

de/compress_into Additionally, all variants support decompress_into and compress_into. Ex.

>>> import numpy as np
>>> from cramjam import snappy, Buffer
>>>
>>> data = np.frombuffer(b'some bytes here', dtype=np.uint8)
>>> data
array([115, 111, 109, 101,  32,  98, 121, 116, 101, 115,  32, 104, 101,
       114, 101], dtype=uint8)
>>>
>>> compressed = Buffer()
>>> snappy.compress_into(data, compressed)
33  # 33 bytes written to compressed buffer
>>>
>>> compressed.tell()  # Where is the buffer position?
33  # goodie!
>>>
>>> compressed.seek(0)  # Go back to the start of the buffer so we can prepare to decompress
>>> decompressed = b'0' * len(data)  # let's write to `bytes` as output
>>> decompressed
b'000000000000000'
>>>
>>> snappy.decompress_into(compressed, decompressed)
15  # 15 bytes written to decompressed
>>> decompressed
b'some bytes here'

Special note!
If you know the length of the de/compress output, you can provide output_len=<<some int>> to any de/compress to get ~1.5-3x performance increase as this allows single buffer allocation; doesn't really apply if you're using cramjam.Buffer or cramjam.File objects.

Dependencies

~11MB
~212K SLoC