Skip to main content

Python bindings to the heatshrink library

Project description

buildstatus appveyor coverage

PyHeatshrink

Compression using the Heatshrink algorithm in Python 3.

Installation

From PyPI:

$ pip install heatshrink2

Usage

Files/Streams

The file interface attempts to imitate the behaviour of the built-in file object and other file-like objects (E.g. bz2.BZ2File), thus you can expect all methods implemented in file to also be available.

You can open a heatshrink file by using the open function:

>>> import heatshrink2
>>> with heatshrink2.open('data.bin', 'wb') as fout:
...     fout.write(b"Is there anybody in there?")
...
26
>>>

You can also use HeatshrinkFile directly:

>>> from heatshrink2 import HeatshrinkFile
>>> with HeatshrinkFile('data.bin') as fin:
...     print(fin.read(256))
...
b'Is there anybody in there?'
>>> with HeatshrinkFile('data.bin') as fin:
...     for line in fin:
...         print(line)
...
b'Is there anybody in there?'
>>>

Byte strings

The encoder accepts any iterable and returns a byte string containing encoded (compressed) data.

>>> import heatshrink2
>>> heatshrink2.compress(b'a string')
b'\xb0\xc8.wK\x95\xa6\xddg'
>>>

The decoder accepts any object that implements the buffer protocol and returns a byte representation of the decoded data.

>>> import heatshrink2
>>> heatshrink2.decompress(b'\xb0\xc8.wK\x95\xa6\xddg')
b'a string'
>>>

Parameters

Both the encoder and decoder allow providing window_sz2 and lookahead_sz2 keywords:

window_sz2 - The window size determines how far back in the input can be searched for repeated patterns. A window_sz2 of 8 will only use 256 bytes (2^8), while a window_sz2 of 10 will use 1024 bytes (2^10). The latter uses more memory, but may also compress more effectively by detecting more repetition.

lookahead_sz2 - The lookahead size determines the max length for repeated patterns that are found. If the lookahead_sz2 is 4, a 50-byte run of ‘a’ characters will be represented as several repeated 16-byte patterns (2^4 is 16), whereas a larger lookahead_sz2 may be able to represent it all at once. The number of bits used for the lookahead size is fixed, so an overly large lookahead size can reduce compression by adding unused size bits to small patterns.

input_buffer_size - How large an input buffer to use for the decoder. This impacts how much work the decoder can do in a single step, and a larger buffer will use more memory. An extremely small buffer (say, 1 byte) will add overhead due to lots of suspend/resume function calls, but should not change how well data compresses.

Check out the heatshrink configuration page for more details.

For more use cases, please refer to the tests folder.

Command line

The command line tool can compress and decompress files.

Below is an example of the compress and decompress subcommands.

$ ls -l tests/files/foo.txt
-rw-rw-r-- 1 erik erik 3970 jan  5 12:23 tests/files/foo.txt
$ python -m heatshrink2 compress tests/files/foo.txt foo.hs
$ ls -l foo.hs
-rw-rw-r-- 1 erik erik 2727 jan  5 12:24 foo.hs
$ python -m heatshrink2 decompress foo.hs foo.txt
$ cmp tests/files/foo.txt foo.txt

Benchmarks

The benchmarks check compression/decompression against a ~6MB file:

$ python scripts/benchmark.py

Testing

Running tests is as simple as doing:

$ python setup.py test

License

ISC license

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

heatshrink2-0.9.0.tar.gz (107.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

heatshrink2-0.9.0-cp38-cp38-win_amd64.whl (60.3 kB view details)

Uploaded CPython 3.8Windows x86-64

heatshrink2-0.9.0-cp38-cp38-win32.whl (52.0 kB view details)

Uploaded CPython 3.8Windows x86

heatshrink2-0.9.0-cp37-cp37m-win_amd64.whl (58.6 kB view details)

Uploaded CPython 3.7mWindows x86-64

heatshrink2-0.9.0-cp37-cp37m-win32.whl (50.2 kB view details)

Uploaded CPython 3.7mWindows x86

File details

Details for the file heatshrink2-0.9.0.tar.gz.

File metadata

  • Download URL: heatshrink2-0.9.0.tar.gz
  • Upload date:
  • Size: 107.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.1

File hashes

Hashes for heatshrink2-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4d5c07879d40e79c315f8f236da63d6699420cb949c8a967e1ec2ea365ddcdf9
MD5 09ef3ae2c94cee05f4cdc87c9c8afe94
BLAKE2b-256 4ecb9899810ad8cf9cd32e31e4b2c4de7c2883b5cfe96b65ceed3273bf000e8f

See more details on using hashes here.

File details

Details for the file heatshrink2-0.9.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: heatshrink2-0.9.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 60.3 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

File hashes

Hashes for heatshrink2-0.9.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 17af14c74748c0318006324744327ddb4f4e856a1384bbb0ec4e55364a6d2e0e
MD5 c6fc3d949308788c7a1185dd4cc128cd
BLAKE2b-256 30279f48c4e424bf287badc9c537bff2978ce45ac589575b2e4a2e2fbbf38e29

See more details on using hashes here.

File details

Details for the file heatshrink2-0.9.0-cp38-cp38-win32.whl.

File metadata

  • Download URL: heatshrink2-0.9.0-cp38-cp38-win32.whl
  • Upload date:
  • Size: 52.0 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

File hashes

Hashes for heatshrink2-0.9.0-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 f3f51902f9b568122b0a4fbcda42346221fa64c76727d30c11eec3a757b1682f
MD5 3f54cc36a3e9d3e93b77a137d61b6f13
BLAKE2b-256 fa62c512e9129786e78b278241ce190d8008c3230a7f7b3b8f05fc818e202af0

See more details on using hashes here.

File details

Details for the file heatshrink2-0.9.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: heatshrink2-0.9.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 58.6 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

File hashes

Hashes for heatshrink2-0.9.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 cbcb72511c029f92ae2a97652e99276618eb8d9135e2ac1ef98026049afc1d6f
MD5 2e42c161fa4ef0b8ea644b264c021e08
BLAKE2b-256 dde7b2898e05c4fc5cb7a763b7f758ce798845e8066c02714329427d03b3f1df

See more details on using hashes here.

File details

Details for the file heatshrink2-0.9.0-cp37-cp37m-win32.whl.

File metadata

  • Download URL: heatshrink2-0.9.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 50.2 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

File hashes

Hashes for heatshrink2-0.9.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 1cad4c3cc29c7ce0590e3456b8a00052e78a81494c32de9a5c1ed47973a8abf0
MD5 1f162f5f69457f0f1d08090c100372d8
BLAKE2b-256 2538a3e8113c6ac3d5f2cc95b66b1d04b0a3dd90b594647cb0c917f2cb5caa7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page