Welcome to python-isal’s documentation! — python-isal 0.1.dev703+gbcaaa9b07 documentation (original) (raw)

Introduction

Faster zlib and gzip compatible compression and decompression by providing Python bindings for the ISA-L library.

This package provides Python bindings for the ISA-L library. The Intel(R) Intelligent Storage Acceleration Library (ISA-L) implements several key algorithms in assembly language. This includes a variety of functions to provide zlib/gzip-compatible compression.

python-isal provides the bindings by offering four modules:

isal_zlib and igzip are almost fully compatible with zlib andgzip from the Python standard library. There are some minor differences see: differences-with-zlib-and-gzip-modules.

Quickstart

The python-isal modules can be imported as follows

from isal import isal_zlib from isal import igzip from isal import igzip_lib

isal_zlib and igzip are meant to be used as drop in replacements so their api and functions are the same as the stdlib’s modules. Except where ISA-L does not support the same calls as zlib (See differences below).

A full API documentation can be found on our readthedocs page.

python -m isal.igzip implements a simple gzip-like command line application (just like python -m gzip). Full usage documentation can be found on our readthedocs page.

Installation

Installation with pip

Installation is supported on Linux, MacOS and Windows. On most platforms wheels are provided. The installation will include a staticallly linked version of ISA-L. If a wheel is not provided for your system the installation will build ISA-L first in a temporary directory. Please check theISA-L homepage for the build requirements.

The latest development version of python-isal can be installed with:

pip install git+https://github.com/rhpvorderman/python-isal.git

This requires having the build requirements installed. If you wish to link dynamically against a version of libisal installed on your system use:

PYTHON_ISAL_LINK_DYNAMIC=true pip install isal --no-binary isal

ISA-L is available in numerous Linux distro’s as well as on conda via the conda-forge channel. Checkout the ports documentation on the ISA-L project wiki to find out how to install it. It is important that the development headers are also installed.

On Debian and Ubuntu the ISA-L libraries (including the development headers) can be installed with:

sudo apt install libisal-dev

Installation via conda

Python-isal can be installed via conda, for example using the miniconda installer with a properly setup conda-forgechannel. When used with bioinformatics tools setting up biocondaprovides a clear set of installation instructions for conda.

python-isal is available on conda-forge and can be installed with:

conda install python-isal

This will automatically install the ISA-L library dependency as well, since it is available on conda-forge.

python-isal as a dependency in your project

Python-isal supports a limited amount of platforms for which wheels have been made available. To prevent your users from running into issues when installing your project please list a python-isal dependency as follows.

setup.cfg:

install_requires = isal; platform.machine == "x86_64" or platform.machine == "AMD64" or platform.machine == "aarch64"

setup.py:

extras_require={ ":platform.machine == 'x86_64' or platform.machine == 'AMD64' or platform.machine == 'aarch64'": ['isal'] },

Differences with zlib and gzip modules

API Documentation: isal_zlib

The functions in this module allow compression and decompression using the zlib library, which is based on GNU zip.

‘wbits’ is window buffer size and container format.

Compressor objects support compress() and flush() methods; decompressor objects support decompress() and flush().

class isal.isal_zlib.Compress

Object returned by isal_zlib.compressobj

compress(data, /)

Returns a bytes object containing compressed data.

data

Binary data to be compressed.

After calling this function, some of the input data may still be stored in internal buffers for later processing. Call the flush() method to clear these buffers.

flush(mode=4, /)

Return a bytes object containing any remaining compressed data.

mode

One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH. If mode == Z_FINISH, the compressor object can no longer be used after calling the flush() method. Otherwise, more data can still be compressed.

class isal.isal_zlib.Decompress

Object returned by isal_zlib.compressobj.

decompress(data, /, max_length=0)

Return a bytes object containing the decompressed version of the data.

data

The binary data to decompress.

max_length

The maximum allowable length of the decompressed data. Unconsumed input data will be stored in the unconsumed_tail attribute.

After calling this function, some of the input data may still be stored in internal buffers for later processing. Call the flush() method to clear these buffers.

eof

True if the end-of-stream marker has been reached.

flush(length=16384, /)

Return a bytes object containing any remaining decompressed data.

length

the initial size of the output buffer.

unconsumed_tail

A bytes object that contains any data that was not consumed by the last decompress() call because it exceeded the limit for the uncompressed data buffer. This data has not yet been seen by the zlib machinery, so you must feed it (possibly with further data concatenated to it) back to a subsequent decompress() method call in order to get correct output.

unused_data

Data found after the end of the compressed stream.

isal.isal_zlib.adler32(data, value=1, /)

Compute an Adler-32 checksum of data.

value

Starting value of the checksum.

The returned checksum is an integer.

isal.isal_zlib.compress(data, /, level=2, wbits=15)

Returns a bytes object containing compressed data.

data

Binary data to be compressed.

level

Compression level, in 0-3.

wbits

The window buffer size and container format.

isal.isal_zlib.compressobj(level=2, method=8, wbits=15, memLevel=8, strategy=0, zdict=None)

Return a compressor object.

level

The compression level (an integer in the range 0-3; default is currently equivalent to 2). Higher compression levels are slower, but produce smaller results.

method

The compression algorithm. If given, this must be DEFLATED.

wbits

memLevel

Controls the amount of memory used for internal compression state. Valid values range from 1 to 9. Higher values result in higher memory usage, faster compression, and smaller output.

strategy

Used to tune the compression algorithm. Not supported by ISA-L. Only a default strategy is used.

zdict

The predefined compression dictionary - a sequence of bytes containing subsequences that are likely to occur in the input data.

isal.isal_zlib.crc32(data, value=0, /)

Compute a CRC-32 checksum of data.

value

Starting value of the checksum.

The returned checksum is an integer.

isal.isal_zlib.crc32_combine()

Combine crc1 and crc2 into a new crc that is accurate for the combined data blocks that crc1 and crc2 where calculated from.

crc1

the first crc32 checksum

crc2

the second crc32 checksum

crc2_length

the length of the data block crc2 was calculated from

isal.isal_zlib.decompress(data, /, wbits=15, bufsize=16384)

Returns a bytes object containing the uncompressed data.

data

Compressed data.

wbits

The window buffer size and container format.

bufsize

The initial output buffer size.

isal.isal_zlib.decompressobj(wbits=15, zdict=b'')

Return a decompressor object.

wbits

The window buffer size and container format.

zdict

The predefined compression dictionary. This must be the same dictionary as used by the compressor that produced the input data.

API-documentation: igzip

Similar to the stdlib gzip module. But using the Intel Storage Accelaration Library to speed up its methods.

class isal.igzip.IGzipFile(filename=None, mode=None, compresslevel=2, fileobj=None, mtime=None)

The IGzipFile class simulates most of the methods of a file object with the exception of the truncate() method.

This class only supports opening files in binary mode. If you need to open a compressed file in text mode, use the gzip.open() function.

__init__(filename=None, mode=None, compresslevel=2, fileobj=None, mtime=None)

Constructor for the IGzipFile class.

At least one of fileobj and filename must be given a non-trivial value.

The new class instance is based on fileobj, which can be a regular file, an io.BytesIO object, or any other object which simulates a file. It defaults to None, in which case filename is opened to provide a file object.

When fileobj is not None, the filename argument is only used to be included in the gzip file header, which may include the original filename of the uncompressed file. It defaults to the filename of fileobj, if discernible; otherwise, it defaults to the empty string, and in this case the original filename is not included in the header.

The mode argument can be any of ‘r’, ‘rb’, ‘a’, ‘ab’, ‘w’, ‘wb’, ‘x’, or ‘xb’ depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is ‘rb’. A mode of ‘r’ is equivalent to one of ‘rb’, and similarly for ‘w’ and ‘wb’, ‘a’ and ‘ab’, and ‘x’ and ‘xb’.

The compresslevel argument is an integer from 0 to 3 controlling the level of compression; 0 is fastest and produces the least compression, and 3 is slowest and produces the most compression. Unlike gzip.GzipFile 0 is NOT no compression. The default is 2.

The mtime argument is an optional numeric timestamp to be written to the last modification time field in the stream when compressing. If omitted or None, the current time is used.

write(data)

Write buffer b to the IO stream.

Return the number of bytes written, which is always the length of b in bytes.

Raise BlockingIOError if the buffer is full and the underlying raw stream cannot accept more data at the moment.

exception isal.igzip.BadGzipFile

Exception raised in some cases for invalid gzip files.

isal.igzip.GzipFile

alias of IGzipFile

isal.igzip.compress(data, compresslevel: int = 3, *, mtime: SupportsInt | None = None) → bytes

Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-3.

isal.igzip.decompress(data)

Decompress a gzip compressed string in one shot. Return the decompressed string.

This function checks for extra gzip members. Using isal_zlib.decompress(data, wbits=31) is faster in cases where only one gzip member is guaranteed to be present.

isal.igzip.open(filename, mode='rb', compresslevel=2, encoding=None, errors=None, newline=None)

Open a gzip-compressed file in binary or text mode. This uses the isa-l library for optimized speed.

The filename argument can be an actual filename (a str or bytes object), or an existing file object to read from or write to.

The mode argument can be “r”, “rb”, “w”, “wb”, “x”, “xb”, “a” or “ab” for binary mode, or “rt”, “wt”, “xt” or “at” for text mode. The default mode is “rb”, and the default compresslevel is 2.

For binary mode, this function is equivalent to the GzipFile constructor: GzipFile(filename, mode, compresslevel). In this case, the encoding, errors and newline arguments must not be provided.

For text mode, a GzipFile object is created, and wrapped in an io.TextIOWrapper instance with the specified encoding, error handling behavior, and line ending(s).

API-documentation: igzip_threaded

isal.igzip_threaded.open(filename, mode='rb', compresslevel=2, encoding=None, errors=None, newline=None, *, threads=1, block_size=1048576)

Utilize threads to read and write gzip objects and escape the GIL. Comparable to gzip.open. This method is only usable for streamed reading and writing of objects. Seeking is not supported.

threads == 0 will defer to igzip.open. A threads < 0 will attempt to use the number of threads in the system.

Parameters:

Returns:

An io.BufferedReader, io.BufferedWriter, or io.TextIOWrapper, depending on the mode.

API Documentation: igzip_lib

Pythonic interface to ISA-L’s igzip_lib.

This module comes with the following constants:

ISAL_BEST_SPEED The lowest compression level (0)
ISAL_BEST_COMPRESSION The highest compression level (3)
ISAL_DEFAULT_COMPRESSION The compromise compression level (2)
DEF_BUF_SIZE Default size for the starting buffer (16K)
MAX_HIST_BITS Maximum window size bits (15).
COMP_DEFLATE Flag to compress to a raw deflate block
COMP_GZIP Flag to compress a gzip block, consisting of a gzip header, a raw deflate block and a gzip trailer.
COMP_GZIP_NO_HDR Flag to compress a gzip block without a header.
COMP_ZLIB Flag to compress a zlib block, consisting of a zlib header, a raw deflate block and a zlib trailer.
COMP_ZLIB_NO_HDR Flag to compress a zlib block without a header.
DECOMP_DEFLATE Flag to decompress a raw deflate block.
DECOMP_GZIP Flag to decompress a gzip block including header and verify the checksums in the trailer.
DECOMP_GZIP_NO_HDR Decompresses a raw deflate block (no header, no trailer) and updates the crc member on the IgzipDecompressor object with a crc32 checksum.
DECOMP_GZIP_NO_HDR_VER Like DECOMP_GZIP_NO_HDR but reads the trailer and verifies the crc32 checksum.
DECOMP_ZLIB Flag to decompress a zlib block including header and verify the checksums in the trailer.
DECOMP_ZLIB_NO_HDR Decompresses a raw deflate block (no header, no trailer) and updates the crc member on the IgzipDecompressor object with an adler32 checksum.
DECOMP_ZLIB_NO_HDR_VER Like DECOMP_ZLIB_NO_HDR but reads the trailer and verifies the adler32 checksum.
MEM_LEVEL_DEFAULT The default memory level for the internal level buffer. (Equivalent to MEM_LEVEL_LARGE.)
MEM_LEVEL_MIN The minimum memory level.
MEM_LEVEL_SMALL
MEM_LEVEL_MEDIUM
MEM_LEVEL_LARGE
MEM_LEVEL_EXTRA_LARGE The largest memory level.

class isal.igzip_lib.IgzipDecompressor(flag=0, hist_bits=15, zdict=b'')

Create a decompressor object for decompressing data incrementally.

flag

Flag signifying which headers and trailers the stream has.

hist_bits

The lookback distance is 2 ^ hist_bits.

zdict

Dictionary used for decompressing the data

For one-shot decompression, use the decompress() function instead.

crc

The checksum that is saved if DECOMP_ZLIB* or DECOMP_GZIP* flags are used.

decompress(data, /, max_length=-1)

Decompress data, returning uncompressed data as bytes.

If max_length is nonnegative, returns at most max_length bytes of decompressed data. If this limit is reached and further output can be produced, self.needs_input will be set to False. In this case, the next call to decompress() may provide data as b’’ to obtain more of the output.

If all of the input data was decompressed and returned (either because this was less than max_length bytes, or because max_length was negative),self.needs_input will be set to True.

Attempting to decompress data after the end of stream is reached raises an EOFError. Any data found after the end of the stream is ignored and saved in the unused_data attribute.

eof

True if the end-of-stream marker has been reached.

needs_input

True if more input is needed before more decompressed data can be produced.

unused_data

Data found after the end of the compressed stream.

isal.igzip_lib.compress(data, /, level=2, flag=0, mem_level=0, hist_bits=15)

Returns a bytes object containing compressed data.

data

Binary data to be compressed.

level

Compression level, in 0-3.

flag

Controls which header and trailer are used.

mem_level

Sets the memory level for the memory buffer. Larger buffers improve performance.

hist_bits

Sets the size of the view window. The size equals 2^hist_bits. Similar to zlib wbits value except that the header and trailer are controlled by the flag parameter.

isal.igzip_lib.decompress(data, /, flag=0, hist_bits=15, bufsize=16384)

Returns a bytes object containing the uncompressed data.

data

Compressed data.

flag

The container format.

hist_bits

The window buffer size.

bufsize

The initial output buffer size.

python -m isal.igzip usage

A simple command line interface for the igzip module. Acts like igzip.

usage: python -m isal.igzip [-h] [-0 | -1 | -2 | -3 | -d] [-c | -o OUTPUT] [-n] [-f] [file]

Positional Arguments

file

Named Arguments

-0, --fast

use compression level 0 (fastest)

-1

use compression level 1

-2

use compression level 2 (default)

-3, --best

use compression level 3 (best)

-d, --decompress

Decompress the file instead of compressing.

Default: True

-c, --stdout

write on standard output

Default: False

-o, --output

Write to this output file

-n, --no-name

do not save or restore the original name and timestamp

Default: False

-f, --force

Overwrite output without prompting

Default: False

Contributing

Please make a PR or issue if you feel anything can be improved. Bug reports are also very welcome. Please report them on the github issue tracker.

Development

The repository needs to be cloned recursively to make sure theISA-L repository is checked out:git clone --recursive https://github.com/pycompression/python-isal.git. If the repository is already checked out you can use git submodule update --init.

Patches should be made on a feature branch. To run the testing install toxwith pip install tox and run the commands tox -e lint andtox. That will run most of the testing that is also performed by the CI. For changes to the documentation run tox -e docs. For changes to the C code please also run tox -e asan to check for memory leaks. This requires libasan to be installed.

Building requires theISA-L build requirementsas well.

Acknowledgements

This project builds upon the software and experience of many. Many thanks to:

Python-isal would not have been possible without you!

Changelog

version 1.8.0

version 1.7.2

version 1.7.1

version 1.7.0

version 1.6.1

version 1.6.0

version 1.5.3

version 1.5.2

version 1.5.1

version 1.5.0

version 1.4.1

version 1.4.0

version 1.3.0

version 1.2.0

version 1.1.0

version 1.0.1

version 1.0.0

Python-isal has been rewritten as a C-extension (first implementation was in Cython). This has made the library faster in many key areas.

version 0.11.1

version 0.11.0

In this release the python -m isal.igzip relatively slow decompression rate has been improved in both speed and usability. Previously it was 19% slower than igzip when used with the -d flag for decompressing, now it is just 8% slower. Also some extra flags were added to make it easier to select the output file.

version 0.10.0

version 0.9.0

version 0.8.1

version 0.8.0

version 0.7.0

version 0.6.1

version 0.6.0

version 0.5.0

version 0.4.0

version 0.3.0

version 0.2.0

version 0.1.0