Unverified Commit b955ac99 authored by Yuge Zhang's avatar Yuge Zhang Committed by GitHub
Browse files

Use PyYAML instead of ruamel.yaml (#3702)

parent 7075a836
...@@ -3,7 +3,7 @@ hyperopt == 0.1.2 ...@@ -3,7 +3,7 @@ hyperopt == 0.1.2
json_tricks json_tricks
netifaces netifaces
psutil psutil
ruamel.yaml pyyaml
requests requests
responses responses
schema schema
......
...@@ -20,7 +20,7 @@ websockets ...@@ -20,7 +20,7 @@ websockets
filelock filelock
prettytable prettytable
psutil psutil
ruamel.yaml pyyaml
ipython ipython
gym gym
tianshou tianshou
......
...@@ -18,7 +18,7 @@ def get_tuner_class_dict(): ...@@ -18,7 +18,7 @@ def get_tuner_class_dict():
config_file = str(get_config_file('registered_algorithms.yml')) config_file = str(get_config_file('registered_algorithms.yml'))
if os.path.exists(config_file): if os.path.exists(config_file):
with open(config_file, 'r') as f: with open(config_file, 'r') as f:
config = yaml.load(f, Loader=yaml.SafeLoader) config = yaml.safe_load(f)
else: else:
config = {} config = {}
ret = {} ret = {}
......
...@@ -6,7 +6,7 @@ import dataclasses ...@@ -6,7 +6,7 @@ import dataclasses
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Optional, Type, TypeVar from typing import Any, Dict, Optional, Type, TypeVar
import ruamel.yaml as yaml import yaml
from . import util from . import util
...@@ -72,7 +72,7 @@ class ConfigBase: ...@@ -72,7 +72,7 @@ class ConfigBase:
Load config from YAML (or JSON) file. Load config from YAML (or JSON) file.
Keys in YAML file can either be camelCase or snake_case. Keys in YAML file can either be camelCase or snake_case.
""" """
data = yaml.load(open(path), Loader=yaml.SafeLoader) data = yaml.safe_load(open(path))
if not isinstance(data, dict): if not isinstance(data, dict):
raise ValueError(f'Content of config file {path} is not a dict/object') raise ValueError(f'Content of config file {path} is not a dict/object')
return cls(**data, _base_path=Path(path).parent) return cls(**data, _base_path=Path(path).parent)
......
...@@ -5,7 +5,7 @@ from dataclasses import dataclass ...@@ -5,7 +5,7 @@ from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
from ruamel.yaml import YAML import yaml
from .base import ConfigBase, PathLike from .base import ConfigBase, PathLike
from . import util from . import util
...@@ -118,7 +118,7 @@ class ExperimentConfig(ConfigBase): ...@@ -118,7 +118,7 @@ class ExperimentConfig(ConfigBase):
def json(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
obj = super().json() obj = super().json()
if obj.get('searchSpaceFile'): if obj.get('searchSpaceFile'):
obj['searchSpace'] = YAML().load(open(obj.pop('searchSpaceFile'))) obj['searchSpace'] = yaml.safe_load(open(obj.pop('searchSpaceFile')))
return obj return obj
## End of public API ## ## End of public API ##
......
...@@ -9,7 +9,7 @@ import time ...@@ -9,7 +9,7 @@ import time
import socket import socket
import string import string
import random import random
import ruamel.yaml as yaml import yaml
import psutil import psutil
import filelock import filelock
import glob import glob
...@@ -21,7 +21,7 @@ def get_yml_content(file_path): ...@@ -21,7 +21,7 @@ def get_yml_content(file_path):
'''Load yaml file content''' '''Load yaml file content'''
try: try:
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
return yaml.load(file, Loader=yaml.SafeLoader) return yaml.safe_load(file)
except yaml.scanner.ScannerError as err: except yaml.scanner.ScannerError as err:
print_error('yaml file format error!') print_error('yaml file format error!')
print_error(err) print_error(err)
......
...@@ -6,7 +6,7 @@ import importlib ...@@ -6,7 +6,7 @@ import importlib
import os import os
from pathlib import Path from pathlib import Path
import sys import sys
import ruamel.yaml as yaml import yaml
from nni.runtime.config import get_config_file from nni.runtime.config import get_config_file
ALGO_TYPES = ['tuners', 'assessors', 'advisors'] ALGO_TYPES = ['tuners', 'assessors', 'advisors']
...@@ -215,7 +215,7 @@ def read_registerd_algo_meta(): ...@@ -215,7 +215,7 @@ def read_registerd_algo_meta():
config_file = get_registered_algo_config_path() config_file = get_registered_algo_config_path()
if os.path.exists(config_file): if os.path.exists(config_file):
with open(config_file, 'r') as f: with open(config_file, 'r') as f:
config = yaml.load(f, Loader=yaml.SafeLoader) config = yaml.safe_load(f)
else: else:
config = defaultdict(list) config = defaultdict(list)
for t in ALGO_TYPES: for t in ALGO_TYPES:
...@@ -226,4 +226,4 @@ def read_registerd_algo_meta(): ...@@ -226,4 +226,4 @@ def read_registerd_algo_meta():
def write_registered_algo_meta(config): def write_registered_algo_meta(config):
config_file = get_registered_algo_config_path() config_file = get_registered_algo_config_path()
with open(config_file, 'w') as f: with open(config_file, 'w') as f:
f.write(yaml.dump(dict(config), default_flow_style=False)) f.write(yaml.safe_dump(dict(config), default_flow_style=False))
...@@ -13,7 +13,7 @@ tuner: ...@@ -13,7 +13,7 @@ tuner:
kappa: 5.0 kappa: 5.0
xi: 0.0 xi: 0.0
nu: 2.5 nu: 2.5
alpha: 1e-6 alpha: 1.0e-6
cold_start_num: 10 cold_start_num: 10
selection_num_warm_up: 100000 selection_num_warm_up: 100000
selection_num_starting_points: 250 selection_num_starting_points: 250
......
...@@ -9,7 +9,7 @@ import subprocess ...@@ -9,7 +9,7 @@ import subprocess
import sys import sys
import time import time
import ruamel.yaml as yaml import yaml
import validators import validators
from utils import (CLEAR, EXPERIMENT_URL, GREEN, RED, REST_ENDPOINT, from utils import (CLEAR, EXPERIMENT_URL, GREEN, RED, REST_ENDPOINT,
...@@ -80,7 +80,7 @@ def prepare_config_file(test_case_config, it_config, args): ...@@ -80,7 +80,7 @@ def prepare_config_file(test_case_config, it_config, args):
# generate temporary config yml file to launch experiment # generate temporary config yml file to launch experiment
new_config_file = config_path + '.tmp' new_config_file = config_path + '.tmp'
dump_yml_content(new_config_file, test_yml_config) dump_yml_content(new_config_file, test_yml_config)
print(yaml.dump(test_yml_config, default_flow_style=False), flush=True) print(yaml.safe_dump(test_yml_config, default_flow_style=False), flush=True)
return new_config_file return new_config_file
......
...@@ -9,7 +9,7 @@ import sys ...@@ -9,7 +9,7 @@ import sys
import subprocess import subprocess
import requests import requests
import time import time
import ruamel.yaml as yaml import yaml
import shlex import shlex
EXPERIMENT_DONE_SIGNAL = 'Experiment done' EXPERIMENT_DONE_SIGNAL = 'Experiment done'
...@@ -43,12 +43,12 @@ def remove_files(file_list): ...@@ -43,12 +43,12 @@ def remove_files(file_list):
def get_yml_content(file_path): def get_yml_content(file_path):
'''Load yaml file content''' '''Load yaml file content'''
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
return yaml.load(file, Loader=yaml.Loader) return yaml.safe_load(file)
def dump_yml_content(file_path, content): def dump_yml_content(file_path, content):
'''Dump yaml file content''' '''Dump yaml file content'''
with open(file_path, 'w') as file: with open(file_path, 'w') as file:
file.write(yaml.dump(content, default_flow_style=False)) file.write(yaml.safe_dump(content, default_flow_style=False))
def setup_experiment(installed=True): def setup_experiment(installed=True):
'''setup the experiment if nni is not installed''' '''setup the experiment if nni is not installed'''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment