Python API
- class tamp.Compressor(f, *, window: int = 10, literal: int = 8, dictionary: bytearray | None = None)[source]
Compresses data to a file or stream.
- Parameters:
f (Union[str, Path, FileLike]) -- Path/FileHandle/Stream to write compressed data to.
window (int) -- Size of window buffer in bits. Higher values will typically result in higher compression ratios and higher computation cost. A same size buffer is required at decompression time. Valid range:
[8, 15]
. Defaults to10
(1024
byte buffer).literal (int) -- Number of used bits in each byte of data. The default
8
bits can store all data. A common other value is7
for storing ascii characters where the most-significant-bit is always 0. Smaller values result in higher compression ratios for no additional computation cost. Valid range:[5, 8]
.dictionary (Optional[bytearray]) -- Use the given initialized buffer inplace. At decompression time, the same initialized buffer must be provided.
window
must agree with the dictionary size. If providing a pre-allocated buffer, but with default initialization, it must first be initialized withinitialize_dictionary()
- flush(write_token: bool = True) int [source]
Flushes all internal buffers.
This compresses any data remaining in the input buffer, and flushes any remaining data in the output buffer to disk.
- close() int [source]
Flushes all internal buffers and closes the output file or stream, if tamp opened it.
- Returns:
Number of compressed bytes flushed to disk.
- Return type:
- __enter__() Compressor [source]
Use
Compressor
as a context manager.with tamp.Compressor("output.tamp") as f: f.write(b"foo")
- class tamp.TextCompressor(f, *, window: int = 10, literal: int = 8, dictionary: bytearray | None = None)[source]
Compresses text to a file or stream.
- Parameters:
f (Union[str, Path, FileLike]) -- Path/FileHandle/Stream to write compressed data to.
window (int) -- Size of window buffer in bits. Higher values will typically result in higher compression ratios and higher computation cost. A same size buffer is required at decompression time. Valid range:
[8, 15]
. Defaults to10
(1024
byte buffer).literal (int) -- Number of used bits in each byte of data. The default
8
bits can store all data. A common other value is7
for storing ascii characters where the most-significant-bit is always 0. Smaller values result in higher compression ratios for no additional computation cost. Valid range:[5, 8]
.dictionary (Optional[bytearray]) -- Use the given initialized buffer inplace. At decompression time, the same initialized buffer must be provided.
window
must agree with the dictionary size. If providing a pre-allocated buffer, but with default initialization, it must first be initialized withinitialize_dictionary()
- __enter__() Compressor [source]
Use
Compressor
as a context manager.with tamp.Compressor("output.tamp") as f: f.write(b"foo")
- close() int [source]
Flushes all internal buffers and closes the output file or stream, if tamp opened it.
- Returns:
Number of compressed bytes flushed to disk.
- Return type:
- tamp.compress(data: bytes | str, *, window: int = 10, literal: int = 8, dictionary: bytearray | None = None) bytes [source]
Single-call to compress data.
- Parameters:
window (int) -- Size of window buffer in bits. Higher values will typically result in higher compression ratios and higher computation cost. A same size buffer is required at decompression time. Valid range:
[8, 15]
. Defaults to10
(1024
byte buffer).literal (int) -- Number of used bits in each byte of data. The default
8
bits can store all data. A common other value is7
for storing ascii characters where the most-significant-bit is always 0. Valid range:[5, 8]
.dictionary (Optional[bytearray]) -- Use the given initialized buffer inplace. At decompression time, the same initialized buffer must be provided.
window
must agree with the dictionary size. If providing a pre-allocated buffer, but with default initialization, it must first be initialized withinitialize_dictionary()
- Returns:
Compressed data
- Return type:
- class tamp.Decompressor(f, *, dictionary: bytearray | None = None)[source]
Decompresses a file or stream of tamp-compressed data.
Can be used as a context manager to automatically handle file opening and closing:
with tamp.Decompressor("compressed.tamp") as f: decompressed_data = f.read()
- Parameters:
f (Union[file, str]) -- File-like object to read compressed bytes from.
dictionary (Optional[bytearray]) -- Use the given initialized buffer inplace. At compression time, the same initialized buffer must be provided. Decompression stream's
window
must agree with the dictionary size. If providing a pre-allocated buffer, but with default initialization, it must first be initialized withinitialize_dictionary()
- __enter__()[source]
Use
Decompressor
as a context manager.with tamp.Decompressor("output.tamp") as f: decompressed_data = f.read()
- class tamp.TextDecompressor(f, *, dictionary: bytearray | None = None)[source]
Decompresses a file or stream of tamp-compressed data into text.
- Parameters:
f (Union[file, str]) -- File-like object to read compressed bytes from.
dictionary (Optional[bytearray]) -- Use the given initialized buffer inplace. At compression time, the same initialized buffer must be provided. Decompression stream's
window
must agree with the dictionary size. If providing a pre-allocated buffer, but with default initialization, it must first be initialized withinitialize_dictionary()
- __enter__()[source]
Use
Decompressor
as a context manager.with tamp.Decompressor("output.tamp") as f: decompressed_data = f.read()
- tamp.decompress(data: bytes, *, dictionary: bytearray | None = None) bytearray [source]
Single-call to decompress data.
- Parameters:
data (bytes) -- Tamp-compressed data to decompress.
dictionary (Optional[bytearray]) -- Use the given initialized buffer inplace. At compression time, the same initialized buffer must be provided. Decompression stream's
window
must agree with the dictionary size. If providing a pre-allocated buffer, but with default initialization, it must first be initialized withinitialize_dictionary()
- Returns:
Decompressed data.
- Return type:
- tamp.open(f, mode='rb', **kwargs)
Opens a file for compressing/decompressing.
Example usage:
with tamp.open("file.tamp", "w") as f: # Opens a compressor in text-mode f.write("example text") with tamp.open("file.tamp", "r") as f: # Opens a decompressor in text-mode assert f.read() == "example text"
- Parameters:
f (Union[str, Path]) -- PathLike object to open.
mode (str) --
Opening mode. Must be some combination of
{"r", "w", "b"}
.Read-text-mode (
"r"
) will return atamp.TextDecompressor
. Read data will bestr
.Read-binary-mode (
"rb"
) will return atamp.Decompressor
. Read data will bebytes
.Write-text-mode (
"w"
) will return atamp.TextCompressor
.str
must be provided towrite()
.Write-binary-mode (
"wb"
) will return atamp.Compressor
.bytes
must be provided towrite()
.
kwargs -- Passed along to class constructor.
- Returns:
File-like object for compressing/decompressing.