From 04b916c8aedfe76937740a08f27021c8fd4a370b Mon Sep 17 00:00:00 2001 From: Young Date: Tue, 16 Feb 2021 15:07:14 +0000 Subject: [PATCH 1/2] safe yaml loader --- qlib/__init__.py | 2 +- qlib/utils/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/qlib/__init__.py b/qlib/__init__.py index 596202061..20c2cab4a 100644 --- a/qlib/__init__.py +++ b/qlib/__init__.py @@ -147,7 +147,7 @@ def init_from_yaml_conf(conf_path, **kwargs): """ with open(conf_path) as f: - config = yaml.load(f, Loader=yaml.FullLoader) + config = yaml.load(f, Loader=yaml.SafeLoader) config.update(kwargs) default_conf = config.pop("default_conf", "client") init(default_conf, **config) diff --git a/qlib/utils/__init__.py b/qlib/utils/__init__.py index 799ab377a..f5e981c24 100644 --- a/qlib/utils/__init__.py +++ b/qlib/utils/__init__.py @@ -128,7 +128,7 @@ def parse_config(config): # Check whether config is file if os.path.exists(config): with open(config, "r") as f: - return yaml.load(f) + return yaml.load(f, Loader=yaml.SafeLoader) # Check whether the str can be parsed try: return yaml.load(config) From 83237ba4ed8244aa6eb89109b4443e5ca6881dda Mon Sep 17 00:00:00 2001 From: Young Date: Wed, 17 Feb 2021 05:17:18 +0000 Subject: [PATCH 2/2] yml afe load --- qlib/__init__.py | 2 +- qlib/contrib/online/manager.py | 2 +- qlib/contrib/online/utils.py | 2 +- qlib/contrib/tuner/config.py | 2 +- qlib/utils/__init__.py | 4 ++-- qlib/workflow/cli.py | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/qlib/__init__.py b/qlib/__init__.py index 20c2cab4a..99035e501 100644 --- a/qlib/__init__.py +++ b/qlib/__init__.py @@ -147,7 +147,7 @@ def init_from_yaml_conf(conf_path, **kwargs): """ with open(conf_path) as f: - config = yaml.load(f, Loader=yaml.SafeLoader) + config = yaml.safe_load(f) config.update(kwargs) default_conf = config.pop("default_conf", "client") init(default_conf, **config) diff --git a/qlib/contrib/online/manager.py b/qlib/contrib/online/manager.py index cf850b9da..b0be7f076 100644 --- a/qlib/contrib/online/manager.py +++ b/qlib/contrib/online/manager.py @@ -110,7 +110,7 @@ class UserManager: raise ValueError("User data for {} already exists".format(user_id)) with config_file.open("r") as fp: - config = yaml.load(fp) + config = yaml.safe_load(fp) # load model model = init_instance_by_config(config["model"]) diff --git a/qlib/contrib/online/utils.py b/qlib/contrib/online/utils.py index 611af63e4..71a6a91ec 100644 --- a/qlib/contrib/online/utils.py +++ b/qlib/contrib/online/utils.py @@ -88,7 +88,7 @@ def prepare(um, today, user_id, exchange_config=None): dates.append(get_next_trading_date(dates[-1], future=True)) if exchange_config: with pathlib.Path(exchange_config).open("r") as fp: - exchange_paras = yaml.load(fp) + exchange_paras = yaml.safe_load(fp) else: exchange_paras = {} trade_exchange = Exchange(trade_dates=dates, **exchange_paras) diff --git a/qlib/contrib/tuner/config.py b/qlib/contrib/tuner/config.py index f23d1b874..247fa6a4f 100644 --- a/qlib/contrib/tuner/config.py +++ b/qlib/contrib/tuner/config.py @@ -14,7 +14,7 @@ class TunerConfigManager: self.config_path = config_path with open(config_path) as fp: - config = yaml.load(fp) + config = yaml.safe_load(fp) self.config = copy.deepcopy(config) self.pipeline_ex_config = PipelineExperimentConfig(config.get("experiment", dict()), self) diff --git a/qlib/utils/__init__.py b/qlib/utils/__init__.py index f5e981c24..be7969b65 100644 --- a/qlib/utils/__init__.py +++ b/qlib/utils/__init__.py @@ -128,10 +128,10 @@ def parse_config(config): # Check whether config is file if os.path.exists(config): with open(config, "r") as f: - return yaml.load(f, Loader=yaml.SafeLoader) + return yaml.safe_load(f) # Check whether the str can be parsed try: - return yaml.load(config) + return yaml.safe_load(config) except BaseException: raise ValueError("cannot parse config!") diff --git a/qlib/workflow/cli.py b/qlib/workflow/cli.py index f7455797b..6eba96277 100644 --- a/qlib/workflow/cli.py +++ b/qlib/workflow/cli.py @@ -44,7 +44,7 @@ def sys_config(config, config_path): # worflow handler function def workflow(config_path, experiment_name="workflow", uri_folder="mlruns"): with open(config_path) as fp: - config = yaml.load(fp, Loader=yaml.SafeLoader) + config = yaml.safe_load(fp) # config the `sys` section sys_config(config, config_path)