diff --git a/qlib/utils/__init__.py b/qlib/utils/__init__.py index eaacc613..e6b38b38 100644 --- a/qlib/utils/__init__.py +++ b/qlib/utils/__init__.py @@ -376,7 +376,7 @@ get_cls_kwargs = get_callable_kwargs # NOTE: this is for compatibility for the def init_instance_by_config( - config: Union[str, dict, object], + config: Union[str, dict, object, Path], default_module=None, accept_types: Union[type, Tuple[type]] = (), try_kwargs: Dict = {}, @@ -409,6 +409,9 @@ def init_instance_by_config( - "a.b.c.ClassName" getattr(, "ClassName")() will be used. object example: instance of accept_types + Path example: + specify a pickle object + - it will be treated like 'file:////obj.pkl' default_module : Python module Optional. It should be a python module. NOTE: the "module_path" will be override by `module` arguments @@ -432,11 +435,15 @@ def init_instance_by_config( if isinstance(config, accept_types): return config - if isinstance(config, str): - # path like 'file:////obj.pkl' - pr = urlparse(config) - if pr.scheme == "file": - with open(os.path.join(pr.netloc, pr.path), "rb") as f: + if isinstance(config, (str, Path)): + if isinstance(config, str): + # path like 'file:////obj.pkl' + pr = urlparse(config) + if pr.scheme == "file": + with open(os.path.join(pr.netloc, pr.path), "rb") as f: + return pickle.load(f) + else: + with config.open("rb") as f: return pickle.load(f) klass, cls_kwargs = get_callable_kwargs(config, default_module=default_module) diff --git a/tests/misc/test_sepdf.py b/tests/misc/test_sepdf.py index 0caa9282..9fdc0bb2 100644 --- a/tests/misc/test_sepdf.py +++ b/tests/misc/test_sepdf.py @@ -53,7 +53,8 @@ class SepDF(unittest.TestCase): # it will not raise error, and df will be an empty dataframe del sdf["g1"] - del sdf["g2"] # sdf should support deleting all the columns + del sdf["g2"] + # sdf should support deleting all the columns if __name__ == "__main__":