From 15e386d32c2dba28515b6524c602f0427e6cccd3 Mon Sep 17 00:00:00 2001 From: liuzan <62529552+liuzan-info@users.noreply.github.com> Date: Wed, 27 Dec 2023 21:27:03 +0800 Subject: [PATCH] Fix bug caused by duplicated code (#201) --- .../unimol_tools/utils/config_handler.py | 56 ++++++++----------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/unimol_tools/unimol_tools/utils/config_handler.py b/unimol_tools/unimol_tools/utils/config_handler.py index daa274d..58df06d 100644 --- a/unimol_tools/unimol_tools/utils/config_handler.py +++ b/unimol_tools/unimol_tools/utils/config_handler.py @@ -13,39 +13,29 @@ class YamlHandler: - - import os - - class YamlHandler: - def __init__(self, file_path): - """ - A custom logger class that provides logging functionality to console and file. - - :param file_path: (str) The yaml file path of the config. - """ - if not os.path.exists(file_path): - raise FileExistsError(OSError) - self.file_path = file_path - - def read_yaml(self, encoding='utf-8'): - """ read yaml file and convert to easydict - - :param encoding: (str) encoding method uses utf-8 by default - - :return: Dict (addict), the usage of Dict is the same as dict - """ - with open(self.file_path, encoding=encoding) as f: - return Dict(yaml.load(f.read(), Loader=yaml.FullLoader)) - - def write_yaml(self, data, out_file_path, encoding='utf-8'): - """ write dict or easydict to yaml file(auto write to self.file_path) - - :param data: (dict or Dict(addict)) dict containing the contents of the yaml file - """ - with open(out_file_path, encoding=encoding, mode='w') as f: - return yaml.dump(addict2dict(data) if isinstance(data, Dict) else data, - stream=f, - allow_unicode=True) + def __init__(self, file_path): + """ + A custom logger class that provides logging functionality to console and file. + :param file_path: (str) The yaml file path of the config. + """ + if not os.path.exists(file_path): + raise FileExistsError(OSError) + self.file_path = file_path + def read_yaml(self, encoding='utf-8'): + """ read yaml file and convert to easydict + :param encoding: (str) encoding method uses utf-8 by default + :return: Dict (addict), the usage of Dict is the same as dict + """ + with open(self.file_path, encoding=encoding) as f: + return Dict(yaml.load(f.read(), Loader=yaml.FullLoader)) + def write_yaml(self, data, out_file_path, encoding='utf-8'): + """ write dict or easydict to yaml file(auto write to self.file_path) + :param data: (dict or Dict(addict)) dict containing the contents of the yaml file + """ + with open(out_file_path, encoding=encoding, mode='w') as f: + return yaml.dump(addict2dict(data) if isinstance(data, Dict) else data, + stream=f, + allow_unicode=True) def addict2dict(addict_obj):