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.BaseCompresserinstance based on the supplied (or inferred)compressionmethod. This instance will open a file like object with suppliedmode, using the providedpath. Furthermore, when thepathis 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 thepathextension. This can be done ifpathis aPathTypeinstance, or ifpathhas anameattribute 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.BaseIOinstances) that will be passed to thecompress_pickle.compressers.base.BaseCompresserclass.mode (str) – Mode with which to open the file-like stream. Only used if the
pathis 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.BaseCompresserinitialization.
- Returns
The compresser instance that will be used to create the byte stream from which a
compress_pickle.picklers.base.BasePicklerIOwill read or write serialized objects.- Return type
- Raises
TypeError – If the supplied
pathis not aPATH_TYPESinstance and thecompressionis “infer”.