1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
"""A serializer that extends pickle to change the default protocol."""
from typing import Any, BinaryIO, Dict
import pickle
import logging
log = logging.getLogger(__name__)
# Retrieve the selected pickle protocol from a common utility module
protocol = 4 # Python 3 uses protocol version 4 or higher
log.info("Selected pickle protocol: '{}'".format(protocol))
def dump(value: Any, fp: BinaryIO, sort_keys: bool = False) -> None:
"""
Serialize value as pickle to a byte-mode file object.
Args:
value: The Python object to serialize.
fp: A file-like object supporting binary write operations.
sort_keys: If True and if the value is a dictionary, the keys will
be sorted before serialization.
Returns:
None
"""
if sort_keys and isinstance(value, Dict):
# Sort the dictionary by keys if sort_keys is True
value = {key: value[key] for key in sorted(value)}
pickle.dump(value, fp, protocol=protocol)
def dumps(value: Any, sort_keys: bool = False) -> bytes:
"""
Serialize value as pickle to bytes.
Args:
value: The Python object to serialize.
sort_keys: If True and if the value is a dictionary, the keys will
be sorted before serialization.
Returns:
A bytes object containing the serialized representation of value.
"""
if sort_keys and isinstance(value, Dict):
# Sort the dictionary by keys if sort_keys is True
value = {key: value[key] for key in sorted(value)}
return pickle.dumps(value, protocol=protocol)
def load(fp: BinaryIO) -> Any:
"""
Deserialize one pickle value from a byte-mode file object.
Args:
fp: A file-like object supporting binary read operations.
Returns:
The deserialized Python object.
"""
return pickle.load(fp)
def loads(bytes_value: bytes) -> Any:
"""
Deserialize one pickle value from bytes.
Args:
bytes_value: A bytes object containing the serialized pickle data.
Returns:
The deserialized Python object.
"""
return pickle.loads(bytes_value)
|