Compression Structs#

Basic structs#

class caterpillar.py.Compressed(compressor: _Compressor, struct: _ContainsStruct | _StructLike, comp_kwargs: dict | None = None, decomp_kwargs: dict | None = None)[source]#

A transformer class for handling compression and decompression of data.

This class takes a compression algorithm that implements the _Compressor protocol and applies it to the data for compression or decompression operations. It allows for additional customization through the comp_kwargs and decomp_kwargs parameters.

Parameters:
  • compressor (_Compressor) – The compression algorithm that implements the _Compressor protocol.

  • struct (Union[_ContainsStruct, _StructLike]) – The struct to be compressed or decompressed, should implement _ContainsStruct or _StructLike.

  • comp_kwargs (Optional[dict]) – Optional keyword arguments for the compression method.

  • decomp_kwargs (Optional[dict]) – Optional keyword arguments for the decompression method.

encode(obj: bytes, context: _ContextLike) bytes[source]#

Compress the input data using the provided compressor.

Parameters:
  • obj (bytes) – The data to be compressed.

  • context (_ContextLike) – Context information for compression (e.g., field-specific metadata).

Returns:

The compressed data.

Return type:

bytes

decode(parsed: bytes, context: _ContextLike) bytes[source]#

Decompress the input data using the provided compressor.

Parameters:
  • parsed (bytes) – The compressed data to be decompressed.

  • context (_ContextLike) – Context information for decompression (e.g., field-specific metadata).

Returns:

The decompressed data.

Return type:

bytes

Supported compression types#

caterpillar.py.ZLibCompressed(obj: _ContainsStruct | _StructLike | Callable | int, comp_kwargs: dict | None = None, decomp_kwargs: dict | None = None)[source]#

Create a struct representing zlib compression.

caterpillar.py.Bz2Compressed(obj: _ContainsStruct | _StructLike | Callable | int, comp_kwargs: dict | None = None, decomp_kwargs: dict | None = None)[source]#

Create a struct representing bz2 compression.

caterpillar.py.LZMACompressed(obj: _ContainsStruct | _StructLike | Callable | int, comp_kwargs: dict | None = None, decomp_kwargs: dict | None = None)[source]#

Create a struct representing lzma compression.

caterpillar.py.LZOCompressed(obj: _ContainsStruct | _StructLike | Callable | int, comp_kwargs: dict | None = None, decomp_kwargs: dict | None = None)[source]#

Create a struct representing LZO compression.