Skip to content

Commit

Permalink
Fix bug caused by duplicated code (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzan-info authored Dec 27, 2023
1 parent d93441d commit 15e386d
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions unimol_tools/unimol_tools/utils/config_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 15e386d

Please sign in to comment.