Skip to content

Commit

Permalink
init_instance_by_config enhancement (#1103)
Browse files Browse the repository at this point in the history
* fix SepDataFrame when we del it to empty

* init_instance_by_config enhancement

* Update test_sepdf.py
  • Loading branch information
you-n-g committed May 21, 2022
1 parent 9a40fd3 commit cc94c32
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
19 changes: 13 additions & 6 deletions qlib/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def get_callable_kwargs(config: Union[dict, str], default_module: Union[str, Mod


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 = {},
Expand Down Expand Up @@ -409,6 +409,9 @@ def init_instance_by_config(
- "a.b.c.ClassName" getattr(<a.b.c.module>, "ClassName")() will be used.
object example:
instance of accept_types
Path example:
specify a pickle object
- it will be treated like 'file:///<path to pickle file>/obj.pkl'
default_module : Python module
Optional. It should be a python module.
NOTE: the "module_path" will be override by `module` arguments
Expand All @@ -432,11 +435,15 @@ def init_instance_by_config(
if isinstance(config, accept_types):
return config

if isinstance(config, str):
# path like 'file:///<path to pickle 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:///<path to pickle 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)
Expand Down
3 changes: 2 additions & 1 deletion tests/misc/test_sepdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def test_index_data(self):
# 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__":
Expand Down

0 comments on commit cc94c32

Please sign in to comment.