compress_pickle¶
|
Serialize an object and write it to a file-like object. |
|
Serialize an object and return its binary representation. |
|
Load an object from its serialized binary representation stored in a file-like object. |
|
Load an object from its serialized binary representation. |
A thin wrapper of standard pickle with standard compression libraries
- compress_pickle.compress_pickle.dump(obj: Any, path: Union[str, bytes, os.PathLike, IO[bytes]], compression: Optional[str] = 'infer', pickler_method: str = 'pickle', pickler_kwargs: Optional[Dict[str, Any]] = None, mode: Optional[str] = None, *, set_default_extension: bool = True, **kwargs)[source]¶
Serialize an object and write it to a file-like object.
It is possible to specify a desired serialization method and a compression protocol to use. For example, if
gzipcompression is specified, the object is serialized using the standardpickle.dump()and compressed using gzip.Behind the scenes, this function only instantiates
BaseCompresserandBasePicklerIOinstances depending on the supplied names, and then callscompress_and_pickle()with them.- Parameters
obj (Any) – The object that will be serialized
path (Union[PathType, FileType]) – A path-like object (
str,bytes,os.PathType) or a file-like object (io.BaseIOinstances). The path to which to dump theobj.compression (Optional[str]) – The compression protocol to use. By default, the compression is inferred from the path’s extension. This compression name passed to
get_compresser()to create aBaseCompresserinstance. This instance will be used to create a file-like object onto which to write the serialized binary representation ofobj. To see available compression protocols refer toget_known_compressions().pickler_method (str) – The name of the serialization method to use. This method name is passed to
get_pickler()to create aBasePicklerIOinstance. The default method is"pickle", which means that aBuiltinPicklerIOwill be created by default, and itsdump()will be used for serializing theobj. Refer toget_known_picklers()to see the available serialization methods.pickler_kwargs (Optional[Dict[str, Any]]) – An optional dictionary of keyword arguments to pass to
dump()whenobjis serialized. For example, this could be{"protocol": -1, "fix_imports": True}when the default"pickle"serialization method is used.mode (Optional[str]) – Mode with which to open the file buffer. The default changes according to the compression protocol. Refer to
get_compression_write_mode()to see the defaults.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
compress_pickle.utils.instantiate_compresser(), which in turn creates the compresser instance that will be used.
Notes
If the supplied
pathis a file-like object,dumpdoes not close it before exiting. The users must handle the closing on their own. If the suppliedpathis a path-like object,dumpopens and then closes the file after the writting process finishes.
- compress_pickle.compress_pickle.dumps(obj: Any, compression: Optional[str], pickler_method: str = 'pickle', pickler_kwargs: Optional[Dict[str, Any]] = None, **kwargs) bytes[source]¶
Serialize an object and return its binary representation.
It is possible to specify a desired serialization method and a compression protocol to use. For example, if
gzipcompression is specified, the serialized object is compressed using gzip.Behind the scenes, this function only instantiates
BaseCompresseraround aio.BinaryIOandBasePicklerIOinstances depending on the supplied names, and then callscompress_and_pickle()with them. The contents of theio.BinaryIOare then returned.- Parameters
obj (Any) – The object that will be serialized.
compression (Optional[str]) – The compression protocol to use. By default, the compression is inferred from the path’s extension. This compression name passed to
get_compresser()to create aBaseCompresserinstance. This instance will be used to create a file-like object onto which to write the serialized binary representation ofobj. To see available compression protocols refer toget_known_compressions().pickler_method (str) – The name of the serialization method to use. This method name is passed to
get_pickler()to create aBasePicklerIOinstance. The default method is"pickle", which means that aBuiltinPicklerIOwill be created by default, and itsdump()will be used for serializing theobj. Refer toget_known_picklers()to see the available serialization methods.pickler_kwargs (Optional[Dict[str, Any]]) – An optional dictionary of keyword arguments to pass to
dump()whenobjis serialized. For example, this could be{"protocol": -1, "fix_imports": True}when the default"pickle"serialization method is used.
- Returns
The resulting bytes of the serialized
objafter being compressed.- Return type
Notes
The
compressionargument is mandatory because it cannot be inferred.
- compress_pickle.compress_pickle.load(path: Union[str, bytes, os.PathLike, IO[bytes]], compression: Optional[str] = 'infer', pickler_method: str = 'pickle', pickler_kwargs: Optional[Dict[str, Any]] = None, mode: Optional[str] = None, *, set_default_extension: bool = True, **kwargs) Any[source]¶
Load an object from its serialized binary representation stored in a file-like object.
It is possible to specify a desired serialization method and a compression protocol to use. For example, if
gzipcompression is specified, the contents of the supplied file-like object are uncompressed using gzip and then loaded using standardpickle.load().Behind the scenes, this function only instantiates
BaseCompresserandBasePicklerIOinstances depending on the supplied names, and then callsuncompress_and_unpickle()with them.- Parameters
path (Union[PathType, FileType]) – A path-like object (
str,bytes,os.PathType) or a file-like object (io.BaseIOinstances). The path to which to dump theobj.compression (Optional[str]) – The compression protocol to use. By default, the compression is inferred from the path’s extension. This compression name passed to
get_compresser()to create aBaseCompresserinstance. This instance will be used to open a file-like object from which to read the serialized binary representation ofobj. To see available compression protocols refer toget_known_compressions().pickler_method (str) – The name of the serialization method to use. This method name is passed to
get_pickler()to create aBasePicklerIOinstance. The default method is"pickle", which means that aBuiltinPicklerIOwill be created by default, and itsload()will be used for unserializing theobj. Refer toget_known_picklers()to see the available serialization methods.pickler_kwargs (Optional[Dict[str, Any]]) – An optional dictionary of keyword arguments to pass to
load()when theobjis loaded. For example, this could be{"fix_imports": True}when the default"pickle"serialization method is used.mode (Optional[str]) – Mode with which to open the file buffer. The default changes according to the compression protocol. Refer to
get_compression_read_mode()to see the defaults.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
compress_pickle.utils.instantiate_compresser(), which in turn creates the compresser instance that will be used.
- Returns
obj – The object that is loaded from its compressed binary representation.
- Return type
Any
Notes
If the supplied
pathis a file-like object,loaddoes not close it before exiting. The users must handle the closing on their own. If the suppliedpathis a path-like object,loadopens and then closes the file after the writting process finishes.
- compress_pickle.compress_pickle.loads(data: bytes, compression: Optional[str], pickler_method: str = 'pickle', pickler_kwargs: Optional[Dict[str, Any]] = None, **kwargs) Any[source]¶
Load an object from its serialized binary representation.
It is possible to specify a desired serialization method and a compression protocol to use. For example, if
gzipcompression is specified, the supplied binary data is uncompressed with gzip and then loaded using standardpickle.load().Behind the scenes, this function only instantiates
BaseCompresseraround aio.BinaryIO(data)stream andBasePicklerIOinstances depending on the supplied names, and then callsuncompress_and_unpickle()with them.- Parameters
data (bytes) – The bytes that contain the compressed serialized binary representation of the object that must be loaded.
compression (Optional[str]) – The compression protocol to use. By default, the compression is inferred from the path’s extension. This compression name passed to
get_compresser()to create aBaseCompresserinstance. This instance will be used to open a file-like object from which to read the serialized binary representation ofobj. To see available compression protocols refer toget_known_compressions().pickler_method (str) – The name of the serialization method to use. This method name is passed to
get_pickler()to create aBasePicklerIOinstance. The default method is"pickle", which means that aBuiltinPicklerIOwill be created by default, and itsload()will be used for unserializing theobj. Refer toget_known_picklers()to see the available serialization methods.pickler_kwargs (Optional[Dict[str, Any]]) – An optional dictionary of keyword arguments to pass to
load()when theobjis loaded. For example, this could be{"fix_imports": True}when the default"pickle"serialization method is used.kwargs – Any extra keyword arguments are passed to
compress_pickle.utils.instantiate_compresser(), which in turn creates the compresser instance that will be used.
- Returns
obj – The object that is loaded from its compressed binary representation.
- Return type
Any
Notes
The
compressionargument is mandatory because it cannot be inferred.