mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-14 08:16:54 +08:00
init_instance_by_config enhancement (#1103)
* fix SepDataFrame when we del it to empty * init_instance_by_config enhancement * Update test_sepdf.py
This commit is contained in:
@@ -376,7 +376,7 @@ get_cls_kwargs = get_callable_kwargs # NOTE: this is for compatibility for the
|
|||||||
|
|
||||||
|
|
||||||
def init_instance_by_config(
|
def init_instance_by_config(
|
||||||
config: Union[str, dict, object],
|
config: Union[str, dict, object, Path],
|
||||||
default_module=None,
|
default_module=None,
|
||||||
accept_types: Union[type, Tuple[type]] = (),
|
accept_types: Union[type, Tuple[type]] = (),
|
||||||
try_kwargs: Dict = {},
|
try_kwargs: Dict = {},
|
||||||
@@ -409,6 +409,9 @@ def init_instance_by_config(
|
|||||||
- "a.b.c.ClassName" getattr(<a.b.c.module>, "ClassName")() will be used.
|
- "a.b.c.ClassName" getattr(<a.b.c.module>, "ClassName")() will be used.
|
||||||
object example:
|
object example:
|
||||||
instance of accept_types
|
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
|
default_module : Python module
|
||||||
Optional. It should be a python module.
|
Optional. It should be a python module.
|
||||||
NOTE: the "module_path" will be override by `module` arguments
|
NOTE: the "module_path" will be override by `module` arguments
|
||||||
@@ -432,11 +435,15 @@ def init_instance_by_config(
|
|||||||
if isinstance(config, accept_types):
|
if isinstance(config, accept_types):
|
||||||
return config
|
return config
|
||||||
|
|
||||||
if isinstance(config, str):
|
if isinstance(config, (str, Path)):
|
||||||
# path like 'file:///<path to pickle file>/obj.pkl'
|
if isinstance(config, str):
|
||||||
pr = urlparse(config)
|
# path like 'file:///<path to pickle file>/obj.pkl'
|
||||||
if pr.scheme == "file":
|
pr = urlparse(config)
|
||||||
with open(os.path.join(pr.netloc, pr.path), "rb") as f:
|
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)
|
return pickle.load(f)
|
||||||
|
|
||||||
klass, cls_kwargs = get_callable_kwargs(config, default_module=default_module)
|
klass, cls_kwargs = get_callable_kwargs(config, default_module=default_module)
|
||||||
|
|||||||
@@ -53,7 +53,8 @@ class SepDF(unittest.TestCase):
|
|||||||
# it will not raise error, and df will be an empty dataframe
|
# it will not raise error, and df will be an empty dataframe
|
||||||
|
|
||||||
del sdf["g1"]
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user