Application programming interface

Exception

exception PpmdError

This exception is raised when an error occurs.

Simple compression/decompression

This section contains:

compress(bytes_or_str: Union[bytes, bytearray, memoryview, str], max_order: int, mem_size: int, variant: str)

Compress bytes_or_str, return the compressed data.

Parameters
  • bytes_or_str (bytes-like object or str) – Data to be compressed. When it is type of str, encoded with “UTF-8” encoding before compress.

  • max_order (int) – maximum order of PPMd algorithm

  • mem_size (int) – memory size used for building PPMd model

  • variant (str) – PPMd variant name, only accept “H” or “I”

Returns

Compressed data

Return type

bytes

compressed_data = compress(data)
decompress_str(data: Union[bytes, memoryview], max_order: int, mem_size: int, encoding: str, variant: str)

Decompress data, return the decompressed text.

When encoding specified, return the decoded data as str type by specified encoding. Otherwise it returns data decoding by default “UTF-8”.

Parameters
  • data (bytes-like object) – Data to be decompressed.

  • max_order (int) – maximum order of PPMd algorithm

  • mem_size (int) – memory size used for building PPMd model

  • encoding (str) – Encoding name to use when decoding raw decompressed data

  • variant (str) – PPMd variant name, only accept “H” or “I”

Returns

Decompressed text

Return type

str

Raises

PpmdError – If decompression fails.

decompressed_text = decompress_str(data)
decompress(data: Union[bytes, memoryview], max_order: int, mem_size: int, variant: str)

Decompress data, return the decompressed data.

Parameters
  • data (bytes-like object) – Data to be decompressed

  • max_order (int) – maximum order of PPMd algorithm

  • mem_size (int) – memory size used for building PPMd model

  • variant (str) – PPMd variant name, only accept “H” or “I”

Returns

Decompressed data

Return type

bytes

Raises

PpmdError – If decompression fails.

decompressed_data = decompress(data)

Streaming compression

class PpmdCompressor

A streaming compressor. It’s thread-safe at method level.

__init__(self, max_order: int, mem_size: int, variant: str, restore_method: int)

Initialize a PpmdCompressor object. restore_method param is affected only when variant is “I”.

Parameters
  • max_order (int) – maximum order of PPMd algorithm

  • mem_size (int) – memory size used for building PPMd model

  • variant (str) – PPMd variant name, only accept “H” or “I”

  • restore_method (int) – PPMD8_RESTORE_METHOD_RESTART(0) or PPMD8_RESTORE_METHOD_CUTOFF(1)

compress(self, data)

Provide data to the compressor object.

Parameters

data (bytes-like object) – Data to be compressed.

Returns

A chunk of compressed data if possible, or b'' otherwise.

Return type

bytes

flush(self)

Flush any remaining data in internal buffer.

The compressor object can not be used after this method is called.

Returns

Flushed data.

Return type

bytes

c = PpmdCompressor()

dat1 = c.compress(b'123456')
dat2 = c.compress(b'abcdef')
dat3 = c.flush()

Streaming decompression

class PpmdDecompressor

A streaming decompressor. Thread-safe at method level. A restore_method param is affected only when variant is “I”.

__init__(self, max_order: int, mem_size: int, variant: str, restore_method: int)

Initialize a PpmdDecompressor object.

Parameters
  • max_order (int) – maximum order of PPMd algorithm

  • mem_size (int) – memory size used for building PPMd model

  • variant (str) – PPMd variant name, only accept “H” or “I”

  • restore_method (int) – PPMD8_RESTORE_METHOD_RESTART(0) or PPMD8_RESTORE_METHOD_CUTOFF(1)

decompress(self, data, max_length=-1)

Decompress data, returning decompressed data as a bytes object.

Parameters
  • data (bytes-like object) – Data to be decompressed.

  • max_length (int) – Maximum size of returned data. When it’s negative, the output size is unlimited. When it’s non-negative, returns at most max_length bytes of decompressed data. If this limit is reached and further output can (or may) be produced, the needs_input attribute will be set to False. In this case, the next call to this method may provide data as b'' to obtain more of the output.

needs_input

If the max_length output limit in decompress() method has been reached, and the decompressor has (or may has) unconsumed input data, it will be set to False. In this case, pass b'' to decompress() method may output further data.

If ignore this attribute when there is unconsumed input data, there will be a little performance loss because of extra memory copy. This flag can be True even all input data are consumed, when decompressor can be able to accept more data in some case.

eof

True means the end of the first frame has been reached. If decompress data after that, an EOFError exception will be raised. This flag can be False even all input data are consumed, when decompressor can be able to accept more data in some case.

unused_data

A bytes object. When PpmdDecompressor object stops after end mark, unused input data after the end mark. Otherwise this will be b''.

d1 = PpmdDecompressor()

decompressed_dat = d1.decompress(dat1)
decompressed_dat += d1.decompress(dat2)
decompressed_dat += d1.decompress(dat3)