import pickle import shutil import tomllib from pathlib import Path from typing import Any from lang_main.loggers import logger_shared_helpers as logger # ** Lib def create_saving_folder( saving_path_folder: str | Path, overwrite_existing: bool = False, ) -> None: # check for existence of given path if isinstance(saving_path_folder, str): saving_path_folder = Path(saving_path_folder) if not saving_path_folder.exists(): saving_path_folder.mkdir(parents=True) else: if overwrite_existing: # overwrite if desired (deletes whole path and re-creates it) shutil.rmtree(saving_path_folder) saving_path_folder.mkdir(parents=True) else: logger.info( ( 'Path >>%s<< already exists and remained unchanged. If you want to ' 'overwrite this path, use parameter >>overwrite_existing<<.', ), saving_path_folder, ) def load_toml_config( path_to_toml: str | Path, ) -> dict[str, Any]: with open(path_to_toml, 'rb') as f: data = tomllib.load(f) logger.info('Loaded TOML config file successfully.') return data # saving and loading using pickle # careful: pickling from unknown sources can be dangerous def save_pickle( obj: Any, path: str | Path, ) -> None: with open(path, 'wb') as file: pickle.dump(obj, file, protocol=pickle.HIGHEST_PROTOCOL) logger.info('Saved file successfully under %s', path) def load_pickle( path: str | Path, ) -> Any: with open(path, 'rb') as file: obj = pickle.load(file) logger.info('Loaded file successfully.') return obj def get_entry_point( saving_path: Path, filename: str, ) -> Path: entry_point_path = (saving_path / filename).with_suffix('.pkl') if not entry_point_path.exists(): raise FileNotFoundError( f'Could not find provided entry data under path: >>{entry_point_path}<<' ) return entry_point_path