Skip to content

Commit

Permalink
auxiliary.py
Browse files Browse the repository at this point in the history
  • Loading branch information
luiztauffer committed Sep 12, 2024
1 parent 273e60d commit 2ffa3c1
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/vame/util/auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
Licensed under GNU Lesser General Public License v3.0
"""

import os, yaml
import os
import yaml
from pathlib import Path
import ruamel.yaml
from typing import Tuple
Expand All @@ -28,8 +29,10 @@ def create_config_template() -> Tuple[dict, ruamel.yaml.YAML]:
"""
Creates a template for the config.yaml file.
Returns:
Tuple[dict, ruamel.yaml.YAML]: A tuple containing the template dictionary and the Ruamel YAML instance.
Returns
-------
Tuple[dict, ruamel.yaml.YAML]
A tuple containing the template dictionary and the Ruamel YAML instance.
"""
yaml_str = """\
# Project configurations
Expand Down Expand Up @@ -120,21 +123,25 @@ def create_config_template() -> Tuple[dict, ruamel.yaml.YAML]:
anneal_function:
kl_start:
annealtime:
"""
"""
ruamelFile = ruamel.yaml.YAML()
cfg_file = ruamelFile.load(yaml_str)
return(cfg_file,ruamelFile)
return (cfg_file, ruamelFile)


def read_config(configname: str) -> dict:
"""
Reads structured config file defining a project.
Args:
configname (str): Path to the config file.
Parameters
----------
configname : str
Path to the config file.
Returns:
dict: The contents of the config file as a dictionary.
Returns
-------
dict
The contents of the config file as a dictionary.
"""
ruamelFile = ruamel.yaml.YAML()
path = Path(configname)
Expand All @@ -157,26 +164,30 @@ def read_config(configname: str) -> dict:
write_config(configname, cfg)
else:
raise

else:
raise FileNotFoundError(
"Config file is not found. Please make sure that the file exists and/or that you passed the path of the config file correctly!"
)
return cfg


def write_config(configname: str, cfg: dict) -> None:
def write_config(
configname: str,
cfg: dict,
) -> None:
"""
Write structured config file.
Args:
configname (str): Path to the config file.
cfg (dict): Dictionary containing the config data.
Parameters
----------
configname : str
Path to the config file.
cfg : dict
Dictionary containing the config data.
"""
with open(configname, 'w') as cf:
with open(configname, "w") as cf:
ruamelFile = ruamel.yaml.YAML()
cfg_file,ruamelFile = create_config_template()
cfg_file, ruamelFile = create_config_template()
for key in cfg.keys():
cfg_file[key]=cfg[key]

cfg_file[key] = cfg[key]
ruamelFile.dump(cfg_file, cf)

0 comments on commit 2ffa3c1

Please sign in to comment.