compressers¶
Has two main parts:
The compresser classes (all
BaseCompressersubclasses)The compresser registry.
The compresser classes must provide a minimum signature defined in the the common base class: BaseCompresser, and are in charge of opening, closing and getting the byte streams into which to write (or from which to read) the binary serialized representation of arbitrary python objects.
|
Get the compresser class registered with a given compression name. |
|
Get the compresser class registered with a given file extension. |
Get the compression name registered with a given file extension. |
|
|
Register a compression method, along with its compresser class, extensions and modes. |
|
Get the compression's default mode for opening the file buffer for writing. |
|
Get the compression's default mode for opening the file buffer for reading. |
|
Add an alias for an already registered compression. |
Get a list of known compression protocols |
|
|
Check if the supplied |
Get a mapping from known compression protocols to the default filename extensions. |
|
Get the list of registered compresser classes. |
|
|
Compresser abstract base class. |
|
Compresser class that represents a simple uncompressed file object. |
|
Compresser class that wraps the gzip compression package. |
|
Compresser class that wraps the bz2 compression package. |
|
Compresser class that wraps the lzma compression package. |
|
Compresser class that wraps the zipfile compression package. |
|
Compresser class that wraps the lz4 compression package. |
Registry¶
- compress_pickle.compressers.registry.add_compression_alias(alias: str, compression: Optional[str])[source]¶
Add an alias for an already registered compression.
- Parameters
- Raises
ValueError – If the supplied
compressionis not known or if the suppliedaliasis already contained in the registry.
- compress_pickle.compressers.registry.get_compresser(compression: Optional[str]) Type[compress_pickle.compressers.base.BaseCompresser][source]¶
Get the compresser class registered with a given compression name.
- Parameters
compression (Optional[str]) – The compression name.
- Raises
ValueError – If the supplied
compressionhas not been registered.- Returns
The compresser class associated to the
compressionname.- Return type
Type[BaseCompresser]
- compress_pickle.compressers.registry.get_compresser_from_extension(extension: str) Type[compress_pickle.compressers.base.BaseCompresser][source]¶
Get the compresser class registered with a given file extension.
- Parameters
extension (str) – The file extension, for example “.zip”. Note that the dot characters will be striped from the left of any supplied extension before the lookup it. This means that “.zip” and “zip” will be considered equivalent extensions.
- Raises
ValueError – If the supplied
extensionhas not been registered.- Returns
The compresser class associated to the extension.
- Return type
Type[BaseCompresser]
- compress_pickle.compressers.registry.get_compression_from_extension(extension: str) Optional[str][source]¶
Get the compression name registered with a given file extension.
- Parameters
extension (str) – The file extension, for example “.zip”. Note that the dot characters will be striped from the left of any supplied extension before the lookup it. This means that “.zip” and “zip” will be considered equivalent extensions.
- Raises
ValueError – If the supplied
extensionhas not been registered.- Returns
The compression name associated to the extension.
- Return type
Optional[str]
- compress_pickle.compressers.registry.get_compression_read_mode(compression: Optional[str]) str[source]¶
Get the compression’s default mode for opening the file buffer for reading.
- Parameters
compression (Optional[str]) – The compression name.
- Returns
compression_read_mode – The default read mode of the given
compression.- Return type
- Raises
ValueError – If the default write mode of the supplied
compressionis not known.
- compress_pickle.compressers.registry.get_compression_write_mode(compression: Optional[str]) str[source]¶
Get the compression’s default mode for opening the file buffer for writing.
- Parameters
compression (Optional[str]) – The compression name.
- Returns
compression_write_mode – The default write mode of the given
compression.- Return type
- Raises
ValueError – If the default write mode of the supplied
compressionis not known.
- compress_pickle.compressers.registry.get_default_compression_mapping() Dict[Optional[str], str][source]¶
Get a mapping from known compression protocols to the default filename extensions.
- compress_pickle.compressers.registry.get_known_compressions() List[Optional[str]][source]¶
Get a list of known compression protocols
- Returns
compressions – List of known compression protocol names.
- Return type
List[Optional[str]]
- compress_pickle.compressers.registry.get_registered_extensions() Dict[str, Optional[str]][source]¶
Get a copy of the mapping between file extensions and registered compressers.
- compress_pickle.compressers.registry.list_registered_compressers() List[Type[compress_pickle.compressers.base.BaseCompresser]][source]¶
Get the list of registered compresser classes.
- Returns
The list of registered compresser classes.
- Return type
List[Type[BaseCompresser]]
- compress_pickle.compressers.registry.register_compresser(compression: Optional[str], compresser: Type[compress_pickle.compressers.base.BaseCompresser], extensions: Sequence[str], default_write_mode: str = 'wb', default_read_mode: str = 'rb')[source]¶
Register a compression method, along with its compresser class, extensions and modes.
- Parameters
compression (Optional[str]) – The compression name that will be registered.
compresser (Type[BaseCompresser]) – The compresser class. This should be a
BaseCompressersubclass.extensions (Sequence[str]) – A sequence of file name extensions (e.g. [“.zip”]) that will be registered to the supplied compression. These extensions will be used to infer the compression method to use from a file name. The first entry in the
extensionssequence will be used as the compression’s default extension name. For example, ifextensions = ["bz2", "bz"]both the extensions"bz2"and"bz"will be registered to thecompression, but"bz2"will be taken as the compression’s default extension. Note that the dot characters will be striped from the left of any supplied extension before registering it. This means that “.zip” and “zip” will be considered equivalent extensions.default_write_mode (str) – The write mode with which to open the file object stream by default.
default_read_mode (str) – The read mode with which to open the file object stream by default.
- Raises
ValueError – If the supplied
compressionis already contained in the registry or if any of the supplied extensions is already registered.TypeError – If the supplied compresser is not a
BaseCompressersubclass.
- compress_pickle.compressers.registry.validate_compression(compression: Optional[str], infer_is_valid: bool = True)[source]¶
Check if the supplied
compressionprotocol is supported.If it is not supported, a
ValueErroris raised.- Parameters
compression (Optional[str]) – A compression protocol. To see the known compression protocolos, use
get_known_compressions()infer_is_valid (bool) – If
True,compression="infer"is considered a valid compression protocol. IfFalse, it is not accepted as a valid compression protocol.
- Raises
ValueError – If the supplied
compressionis not supported.
Compressers¶
- class compress_pickle.compressers.base.BaseCompresser(path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, **kwargs)[source]¶
Compresser abstract base class.
This class is in charge of handing the binary stream where the pickled python objects must be written to (or read from). During an instance’s initialization, the binary stream must be opened based on a supplied
pathparameter andmode. This stream is nothing more than a python “file-like” object that is in charge of actually writting and reading the binary contents.- Parameters
path (Union[PathType, IO[bytes]]) – A PathType object (
str,bytes,os.PathType) or a file-like object (e.g.io.BaseIOinstances). The path that will be used to open the input/output binary stream.mode (str) – Mode with which to open the file buffer.
kwargs – Any other key word arguments that can be handled by the specific binary stream opener.
- class compress_pickle.compressers.no_compression.NoCompresser(path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, **kwargs)[source]¶
Compresser class that represents a simple uncompressed file object.
This class either simply calls
openon the suppliedpathor uses thepathas the binary input/output stream.- Parameters
path (Union[PathType, IO[bytes]]) – A PathType object (
str,bytes,os.PathType) or a file-like object (e.g.io.BaseIOinstances). The path that will be used to open the input/output binary stream.mode (str) – Mode with which to open the file buffer.
kwargs – Any other key word arguments that are passed to
open().
- class compress_pickle.compressers.gzip.GzipCompresser(path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, **kwargs)[source]¶
Compresser class that wraps the gzip compression package.
This class relies on the
gzipmodule to open the input/output binary stream where the pickled python objects will be written to (or read from). During an instance’s initialization, the binary stream is opened usinggzip.open(path, mode=mode, **kwargs).- Parameters
path (Union[PathType, IO[bytes]]) – A PathType object (
str,bytes,os.PathType) or a file-like object (e.g.io.BaseIOinstances). The path that will be used to open the input/output binary stream.mode (str) – Mode with which to open the file buffer.
kwargs – Any other key word arguments that are passed to
gzip.open().
- class compress_pickle.compressers.bz2.Bz2Compresser(path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, **kwargs)[source]¶
Compresser class that wraps the bz2 compression package.
This class relies on the
bz2module to open the input/output binary stream where the pickled python objects will be written to (or read from). During an instance’s initialization, the binary stream is opened usingbz2.open(path, mode=mode, **kwargs).- Parameters
path (Union[PathType, IO[bytes]]) – A PathType object (
str,bytes,os.PathType) or a file-like object (e.g.io.BaseIOinstances). The path that will be used to open the input/output binary stream.mode (str) – Mode with which to open the file buffer.
kwargs – Any other key word arguments that are passed to
bz2.open().
- class compress_pickle.compressers.lzma.LzmaCompresser(path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, **kwargs)[source]¶
Compresser class that wraps the lzma compression package.
This class relies on the
lzmamodule to open the input/output binary stream where the pickled python objects will be written to (or read from). During an instance’s initialization, the binary stream is opened usinglzma.open(path, mode=mode, **kwargs).- Parameters
path (Union[PathType, IO[bytes]]) – A PathType object (
str,bytes,os.PathType) or a file-like object (e.g.io.BaseIOinstances). The path that will be used to open the input/output binary stream.mode (str) – Mode with which to open the file buffer.
kwargs – Any other key word arguments that are passed to
lzma.open().
- class compress_pickle.compressers.zipfile.ZipfileCompresser(path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, *, arcname=None, pwd=None, zipfile_compression=None, **kwargs)[source]¶
Compresser class that wraps the zipfile compression package.
This class relies on the
zipfilemodule to open the input/output binary stream where the pickled python objects will be written to (or read from). During an instance’s initialization, azipfile.ZipFileinstance is created around the suppliedpath. The openedZipFileis called the archive and works as a kind directory, that can hold other directories or files. These are called members of the archive. TheZipfileCompressercreates the input/output stream by opening a member file in the openedZipFilearchive. The name of the archive member can be chosen with thearcnameargument.- Parameters
path (Union[PathType, IO[bytes]]) – A PathType object (
str,bytes,os.PathType) or a file-like object (e.g.io.BaseIOinstances). The path that will be used to open thezipfile.Zipfileobject. The input/output binary stream is then opened from the aforementioned object aszipfile.Zipfile(*).open(arcname, mode).mode (str) – Mode with which to open the
Zipfile(and also the archive member that is used as the input/output binary stream).arcname (Optional[str]) – The name of the archive member of the opened
zipfile.Zipfilethat will be used as the binary input/output stream. IfNone, thearcnameis assumed to be the basename ofpath(whenpathis path-like),path.name(whenpathis file-like and it has a name attribute) or “default” otherwise.pwd (Optional[str]) – The password used to decrypt encrypted ZIP files.
zipfile_compression (Optional[str]) – If not
None, it is passed as thecompressionkeyword argument tozipfile.Zipfile(...).kwargs – Any other key word arguments that are passed to
zipfile.ZipFile.
- class compress_pickle.compressers.lz4.Lz4Compresser(path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, **kwargs)[source]¶
Compresser class that wraps the lz4 compression package.
This class relies on the
lz4module to open the input/output binary stream where the pickled python objects will be written to (or read from). During an instance’s initialization, the binary stream is opened usinglz4.frame.open(path, mode=mode, **kwargs).- Parameters
path (Union[PathType, IO[bytes]]) – A PathType object (
str,bytes,os.PathType) or a file-like object (e.g.io.BaseIOinstances). The path that will be used to open the input/output binary stream.mode (str) – Mode with which to open the file buffer.
kwargs – Any other key word arguments that are passed to
lz4.frame.open().