objects#
This module contains methods for pickle operations and file hashing.
[!WARNING]
save_picklewill work only onpicklableobjects
save_pickle#
- pocketwelt.objects.save_pickle(obj: Any, path: str, replace: bool = False) None#
Save an object to a file using pickle serialization.
- Parameters:
obj (Any) – Python object to be saved.
path (str) – File path where the object will be saved.
replace (bool, optional) – If True, overwrites the file if it already exists. Defaults to False.
- Raises:
FileExistsError – If the file already exists and replace is False.
ValueError – If given path does not containt .pkl extension.
load_pickle#
- pocketwelt.objects.load_pickle(path: str) Any#
Load an object from a file using pickle deserialization.
- Parameters:
path (str) – The file path from which to load the object.
- Raises:
FileNotFoundError – If the specified file does not exist.
ValueError – If given path does not containt .pkl extension.
- Returns:
Deserialized Python object loaded from the file.
- Return type:
Any
hash_file#
- pocketwelt.objects.hash_file(file: str | bytes, size: int | None = None) str#
Hash file using SHA-256.
- Parameters:
file (Union[str, bytes]) – The file to hash. Can be either a file path (str) or file content (bytes).
size (int, optional) – The number of bytes to read from the file. If None, reads the entire file. Defaults to None.
- Returns:
The hexadecimal representation of the SHA-256 hash.
- Return type:
str
- Raises:
AssertionError – If the filetype is neither str nor bytes.