def load(cls: tp.Type[X], filepath: tp.PathLike) -> X: """Loads a pickle file and checks that it contains an optimizer. The optimizer class is not always fully reliable though (e.g.: optimizer families) so the user is responsible for it. """ filepath = Path(filepath) with filepath.open("rb") as f: opt = pickle.load(f) assert isinstance(opt, cls), f"You should only load {cls} with this method (found {type(opt)})" return opt
def import_additional_module(filepath: tp.PathLike) -> None: """Imports an additional file at runtime Parameter --------- filepath: str or Path the file to import """ filepath = Path(filepath) spec = importlib.util.spec_from_file_location( "nevergrad.additionalimport." + filepath.with_suffix("").name, str(filepath)) module = importlib.util.module_from_spec(spec) # type: ignore assert spec.loader is not None # type: ignore spec.loader.exec_module(module) # type: ignore