Merge pull request #571 from houqp/qp_yaml

support loading config in yaml
pull/10/head
Eren Gölge 2020-11-14 12:56:48 +01:00 committed by GitHub
commit fca2388e6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 7 deletions

View File

@ -212,6 +212,7 @@ if __name__ == '__main__':
parser.add_argument(
'--config_path',
type=str,
required=True,
help='Path to config file for training.',
)
parser.add_argument('--debug',

View File

@ -1,5 +1,7 @@
import os
import re
import json
import yaml
import pickle as pickle_tts
@ -17,19 +19,27 @@ class AttrDict(dict):
self.__dict__ = self
def load_config(config_path):
def load_config(config_path: str) -> AttrDict:
"""Load config files and discard comments
Args:
config_path (str): path to config file.
"""
config = AttrDict()
with open(config_path, "r") as f:
input_str = f.read()
# handle comments
input_str = re.sub(r'\\\n', '', input_str)
input_str = re.sub(r'//.*\n', '\n', input_str)
data = json.loads(input_str)
ext = os.path.splitext(config_path)[1]
if ext in (".yml", ".yaml"):
with open(config_path, "r") as f:
data = yaml.safe_load(f)
else:
# fallback to json
with open(config_path, "r") as f:
input_str = f.read()
# handle comments
input_str = re.sub(r'\\\n', '', input_str)
input_str = re.sub(r'//.*\n', '\n', input_str)
data = json.loads(input_str)
config.update(data)
return config

View File

@ -23,3 +23,4 @@ pylint==2.5.3
gdown
umap
cython
pyyaml