Skip to main content

Python binding to the heatshrink LZSS compression library.

Supported versions:
Python >= 2.6 – Full
Python 3 – Experimental

Tested platforms:
* OS X > 10.10
* Debian 8
* FreeBSD 10

Installation

From PyPI:

$ easy_install heatshrink
$ pip install heatshrink

Manual installation:

$ python setup.py install

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 heatshrink
>>> with heatshrink.open('data.bin', mode='wb') as fp:
...     fp.write("Is there anybody in there?")

You can also use EncodedFile directly:

>>> from heatshrink import EncodedFile
>>> with EncodedFile('data.bin') as fp:
...     # Read a buffer
...     print('Buffered: %r' % fp.read(256))
...     # Iterate through lines
...     for line in fp:
...         print('Read line: %r' % line)

Byte strings

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

>>> import heatshrink
>>> encoded = heatshrink.encode('a string')
>>> type(encoded)
<type 'str'>  # <class 'bytes'> in Python 3
>>> encoded
'\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 heatshrink
>>> decoded = heatshrink.decode(b'\xb0\xc8.wK\x95\xa6\xddg')
>>> type(decoded)
<type 'str'>  # <class 'bytes'> in Python 3
>>> decoded
'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.

Benchmarks

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

$ python bench/benchmarks.py

Testing

Running tests is as simple as doing:

$ python setup.py test

License

ISC license

Release files for Heatshrink 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Heatshrink 0.3.2
File Size Uploaded
Heatshrink-0.3.2.tar.gz 280.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Heatshrink 0.3.2
File Interpreter ABI Platform
Heatshrink-0.3.2.linux-x86_64.tar.gz Details

Total release size: 472.4 kB

Release files / Heatshrink-0.3.2.tar.gz

Download URL Heatshrink-0.3.2.tar.gz
Size 280.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f8cbbabc4ca4b1de25ba8017baad85669bd35e867107a8717e2bfdc21b02b147
BLAKE2b-256 checksum
How to use checksums
b9236f8dc6744de2c6ad36477de04567be5ced5b2d5e64f0844c0815b198d95a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / Heatshrink-0.3.2.linux-x86_64.tar.gz

Download URL Heatshrink-0.3.2.linux-x86_64.tar.gz
Size 191.9 kB
Tags Source
SHA-256 checksum
How to use checksums
379c966aa3e7e504c1ffe0d0a74510d21523006ea332b8e5fccbd6edbfa13866
BLAKE2b-256 checksum
How to use checksums
50e38e9e54bd9271fd8de74b1bd326212292b07fa6159ae17fa4c5963429941e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page