Python bindings to the heatshrink library
Project description
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 build_ext -b .
$ python -m unittest
License
ISC license
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file heatshrink2-0.13.0.tar.gz
.
File metadata
- Download URL: heatshrink2-0.13.0.tar.gz
- Upload date:
- Size: 142.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5aa93c102ba9c4e6e4fb01974cb5b03194e10fa01bfda8bda233c632b22d3b4a |
|
MD5 | b2474d748b0d223f57e791b7e1882bf0 |
|
BLAKE2b-256 | 22535a46650f76811bfc174df261553f621ccc921ef84628cabf0261810ce140 |
File details
Details for the file heatshrink2-0.13.0-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 61.9 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 154da5a23deef5bc36e4b67ea6efb28e716a5dd89254e64dd4a3693a5e6a0e68 |
|
MD5 | 77c14a0ccd9d8424ae9ae7cbe1f356eb |
|
BLAKE2b-256 | 1a27a04c0ada1426fc9c6ff2714cda9319e1aaeb43b6563e402b8de54d71a5b0 |
File details
Details for the file heatshrink2-0.13.0-cp312-cp312-win32.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp312-cp312-win32.whl
- Upload date:
- Size: 55.7 kB
- Tags: CPython 3.12, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d30f1b8e5173f3877db178668d770fbf11226b4bd1410464f7784590208c914 |
|
MD5 | 650eabf2d43a80f738a9c837ff5dbf06 |
|
BLAKE2b-256 | 98354cea898af2b8e26cd1eba99894a8eba325461f84cdf3fe72708913d0e20e |
File details
Details for the file heatshrink2-0.13.0-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 63.3 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55dcc79c2da58507a8d90eb55032aa4a2b6c14a2ad28b700a3f76d461a9583ed |
|
MD5 | 358273c18b797b0f1e2846e18cd58285 |
|
BLAKE2b-256 | 3060675088369238a54e1217a9007066727d032921e51292816ea5d197b6c018 |
File details
Details for the file heatshrink2-0.13.0-cp311-cp311-win32.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp311-cp311-win32.whl
- Upload date:
- Size: 56.5 kB
- Tags: CPython 3.11, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6820afcef084cbaedfbaa7f57739c01a70e35bc862e0bac26281457f0afa79e5 |
|
MD5 | 2972342d74a2936e3f12858b16db6190 |
|
BLAKE2b-256 | 4fcd37429614a7dd0b6aba30d18264b5892b726d40151388c7b7f6778266a68b |
File details
Details for the file heatshrink2-0.13.0-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 63.2 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7288c549ed83fefcc1c88a402fac65cfd212a84a798a9ce8b9fa2c85df6debaa |
|
MD5 | 15dd7d03b211bfc65cf950f53bbde4e1 |
|
BLAKE2b-256 | ebb8cc12d5881e2012b4278991ff9ab71849f2c13aa07b975b85ef497124b225 |
File details
Details for the file heatshrink2-0.13.0-cp310-cp310-win32.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp310-cp310-win32.whl
- Upload date:
- Size: 56.7 kB
- Tags: CPython 3.10, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa3536078d305783bf381134e285b4357637bbbf2f149dc4371335b2020ba067 |
|
MD5 | 63ad283c6c5e2fd647637343bb5fddb1 |
|
BLAKE2b-256 | c50c68bc166e99b564c71b9356b26f4ee05ff49a4f02999b2b001187f9d7b5de |
File details
Details for the file heatshrink2-0.13.0-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 63.3 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bedcdf16da793b03ac16c67b8e636600b00d324a8d67f99e1ab6a61d409630a8 |
|
MD5 | 0ba37f311c68a1fe5a3030938e062c7e |
|
BLAKE2b-256 | 6990693b73a5e48abc144af97739133794beae3ecc50bfc8bfe14b0f80266a05 |
File details
Details for the file heatshrink2-0.13.0-cp39-cp39-win32.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp39-cp39-win32.whl
- Upload date:
- Size: 56.8 kB
- Tags: CPython 3.9, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b49a93d2a0470c0bc4e25ade1993accb6498cba589d7675006ea3de582df4325 |
|
MD5 | 69bc9dc73979f12228533b69af6254ac |
|
BLAKE2b-256 | 4a67be123db2f60c2fde57ef471969301806bae5e707e072e115f61ff186fd1e |
File details
Details for the file heatshrink2-0.13.0-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 63.5 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e5c6a661de64677e3d7b62ab56005d07958949efdf592860800c40bb78c9f59 |
|
MD5 | ab099965f0d5d862dd737c9e837b8b41 |
|
BLAKE2b-256 | c9f403ec6a89685e2642ff4ed2b96e2a97e42c1529d6c59f06969e7e3f984904 |
File details
Details for the file heatshrink2-0.13.0-cp38-cp38-win32.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp38-cp38-win32.whl
- Upload date:
- Size: 56.7 kB
- Tags: CPython 3.8, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4354ff10ec02c2400d39a430eeb6c49297b2c23370bf86bdd69f729e3847cc7e |
|
MD5 | c17cd5d5e62be85e4653c70646ff5cf4 |
|
BLAKE2b-256 | 390cde9d326c39321873cdbe392faac5ef552f7fd02ee2ddc59b288462fadea9 |
File details
Details for the file heatshrink2-0.13.0-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 63.1 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e7e1f6e084e863b1831a93bf2b143802df6a6f8538cf79c218c91282c2e3910 |
|
MD5 | 229f91f355a648d66a210d1adbf2ae78 |
|
BLAKE2b-256 | f1d3f94b818f4944dc84f60877cbb8ec5f69b51d3f18abc9890b8b56ecd2af52 |
File details
Details for the file heatshrink2-0.13.0-cp37-cp37m-win32.whl
.
File metadata
- Download URL: heatshrink2-0.13.0-cp37-cp37m-win32.whl
- Upload date:
- Size: 56.4 kB
- Tags: CPython 3.7m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8918e6df461843fbbfe6dc4826719ddb8dc9a215d7e60baca1aa1b536064f34f |
|
MD5 | a4e3bead4f16947ffebdc0d263103901 |
|
BLAKE2b-256 | 30a58d35fe7243fcd7b9620ab39313b65c3dfcfc99e3265102aaba2fdbb5c44d |