utils

instantiate_compresser(compression, path, mode)

Initialize the compresser instance for the supplied path and compression.

compress_pickle.utils.instantiate_compresser(compression: Optional[str], path: Union[str, bytes, os.PathLike, IO[bytes]], mode: str, set_default_extension: bool = True, **kwargs) compress_pickle.compressers.base.BaseCompresser[source]

Initialize the compresser instance for the supplied path and compression.

Initiate a compress_pickle.compressers.base.BaseCompresser instance based on the supplied (or inferred) compression method. This instance will open a file like object with supplied mode, using the provided path. Furthermore, when the path is not a binary stream, this function can also potentially set the path’s extension to the default extension registered to the used compression method.

Parameters
  • compression (Optional[str]) – The compression method name. Refer to get_known_compressions() for a list of the known compression methods. If "infer", the compression method is inferred from the path extension. This can be done if path is a PathType instance, or if path has a name attribute that is not None. Refer to get_registered_extensions() for the mapping between extensions and compression methods.

  • path (Union[PathType, FileType]) – A path-like object (str, bytes, os.PathType) or a file-like object (io.BaseIO instances) that will be passed to the compress_pickle.compressers.base.BaseCompresser class.

  • mode (str) – Mode with which to open the file-like stream. Only used if the path is a PathType. If “read”, the default read mode is automatically assigned by get_compression_read_mode() based on the used compression method. If “write”, the default write mode is automatically assigned from get_compression_write_mode() based on the used compression method.

  • set_default_extension (bool) – If True, the default extension given the provided compression protocol is set to the supplied path. Refer to get_default_compression_mapping() for the default extension registered to each compression method.

  • **kwargs – Any extra keyword arguments are passed to the compress_pickle.compressers.base.BaseCompresser initialization.

Returns

The compresser instance that will be used to create the byte stream from which a compress_pickle.picklers.base.BasePicklerIO will read or write serialized objects.

Return type

BaseCompresser

Raises

TypeError – If the supplied path is not a PATH_TYPES instance and the compression is “infer”.