utils
¶
|
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 suppliedmode
, using the providedpath
. Furthermore, when thepath
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 thepath
extension. This can be done ifpath
is aPathType
instance, or ifpath
has aname
attribute that is notNone
. Refer toget_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 thecompress_pickle.compressers.base.BaseCompresser
class.mode (str) – Mode with which to open the file-like stream. Only used if the
path
is aPathType
. If “read”, the default read mode is automatically assigned byget_compression_read_mode()
based on the used compression method. If “write”, the default write mode is automatically assigned fromget_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 suppliedpath
. Refer toget_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
- Raises
TypeError – If the supplied
path
is not aPATH_TYPES
instance and thecompression
is “infer”.