-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added pickle io handling util functions
- Loading branch information
1 parent
9a35f0c
commit 058b54d
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""Helper functions to handle IO operations.""" | ||
|
||
import pathlib | ||
import pickle | ||
|
||
from typing import Union | ||
|
||
from transport_performance.utils.defence import ( | ||
_check_parent_dir_exists, | ||
_enforce_file_extension, | ||
_is_expected_filetype, | ||
) | ||
|
||
|
||
def to_pickle( | ||
picklable_object: object, | ||
path: Union[str, pathlib.Path], | ||
) -> None: | ||
"""Pickle an object for later reuse. | ||
Parameters | ||
---------- | ||
picklable_object : Any | ||
Object to be saved to pickle file. If the directory does not exist one | ||
will be generated. | ||
path : Union[str, pathlib.Path] | ||
Path to save pickle file. Must have a ".pkl" or ".pickle" file | ||
extension or it will be coerced to ".pkl". | ||
Raises | ||
------ | ||
UserWarning | ||
When `path` does not have a ".pkl" or ".pickle" file extension. The | ||
warning will signify for coercion to a path with the ".pkl" extension. | ||
""" | ||
# defensive checks | ||
_check_parent_dir_exists(path, "path", create=True) | ||
path = _enforce_file_extension( | ||
path, | ||
exp_ext=[".pkl", ".pickle"], | ||
default_ext=".pkl", | ||
param_nm="path", | ||
) | ||
|
||
# write to file | ||
with open(path, "wb") as f: | ||
pickle.dump(picklable_object, f) | ||
|
||
|
||
def from_pickle(path: Union[str, pathlib.Path]) -> object: | ||
"""Read a pickled object from file. | ||
Parameters | ||
---------- | ||
path : Union[str, pathlib.Path] | ||
Path of saved pickle file. | ||
Returns | ||
------- | ||
Any | ||
Object in pickle file. Must have a ".pkl" or ".pickle" file extension. | ||
Raises | ||
------ | ||
TypeError | ||
`path` is not a string or a pathlib.Path type. | ||
FileNotFoundError | ||
`path` does not exist | ||
ValueError | ||
`path` does not have either a ".pkl" or ".pickle" file extnesion. | ||
""" | ||
# defensive checks | ||
_is_expected_filetype( | ||
path, "path", check_existing=True, exp_ext=[".pkl", ".pickle"] | ||
) | ||
|
||
# read and return object | ||
with open(path, "rb") as f: | ||
return pickle.load(f) |