renaming and add pickling
This commit is contained in:
@@ -1,12 +1,51 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pickle
|
||||
import shutil
|
||||
from collections.abc import Sequence
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from dopt_basics.datetime import TIMEZONE_CEST, get_timestamp
|
||||
|
||||
|
||||
def save_pickle(
|
||||
obj: Any,
|
||||
folder: Path,
|
||||
filename: str,
|
||||
create_folder: bool = False,
|
||||
) -> None:
|
||||
pth_file = prepare_path(
|
||||
folder,
|
||||
None,
|
||||
filename=filename,
|
||||
suffix=".pkl",
|
||||
create_folder=create_folder,
|
||||
)
|
||||
if not pth_file.parent.exists():
|
||||
raise FileNotFoundError(
|
||||
"The directory structure disered seems not to exist. "
|
||||
"Consider using >create_folder=True<"
|
||||
)
|
||||
|
||||
with open(pth_file, "wb") as pkl_handle:
|
||||
pickle.dump(obj, pkl_handle, protocol=5)
|
||||
|
||||
|
||||
def load_pickle(
|
||||
folder: Path,
|
||||
filename: str,
|
||||
) -> Any:
|
||||
pth_file = prepare_path(folder, None, filename=filename, suffix=".pkl")
|
||||
if not pth_file.parent.exists():
|
||||
raise FileNotFoundError(f"File not found under: >{pth_file}<")
|
||||
|
||||
with open(pth_file, "rb") as pkl_handle:
|
||||
obj = pickle.load(pkl_handle)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
def create_folder(
|
||||
path: Path,
|
||||
delete_existing: bool = False,
|
||||
@@ -16,7 +55,7 @@ def create_folder(
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def prepare_save_path(
|
||||
def prepare_path(
|
||||
root_folder: Path,
|
||||
dirs: Sequence[str] | None,
|
||||
filename: str | None,
|
||||
Reference in New Issue
Block a user