Last active
December 7, 2022 03:17
-
-
Save wsntxxn/5baa8d7177f0c3e1995d9212637fd687 to your computer and use it in GitHub Desktop.
Argument parser to parse parameters in configuration files. Convenient modification based on command lines are supported.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import fire | |
import toml | |
import yaml | |
def merge_a_into_b(a, b): | |
# merge dict a into dict b. values in a will overwrite b. | |
for k, v in a.items(): | |
if isinstance(v, dict) and k in b: | |
assert isinstance( | |
b[k], dict | |
), "Cannot inherit key '{}' from base!".format(k) | |
merge_a_into_b(v, b[k]) | |
else: | |
b[k] = v | |
def load_config(config_file): | |
with open(config_file, "r") as reader: | |
config = yaml.load(reader, Loader=yaml.FullLoader) | |
if "inherit_from" in config: | |
base_config_file = config["inherit_from"] | |
base_config_file = os.path.join( | |
os.path.dirname(config_file), base_config_file | |
) | |
assert not os.path.samefile(config_file, base_config_file), \ | |
"inherit from itself" | |
base_config = load_config(base_config_file) | |
del config["inherit_from"] | |
merge_a_into_b(config, base_config) | |
return base_config | |
return config | |
def parse_config_cmdargs(config, **cmdargs): | |
toml_list = [] | |
for k, v in cmdargs.items(): | |
if isinstance(v, str): | |
toml_list.append(f"{k}='{v}'") | |
elif isinstance(v, bool): | |
toml_list.append(f"{k}={str(v).lower()}") | |
else: | |
toml_list.append(f"{k}={v}") | |
toml_str = "\n".join(toml_list) | |
cmd_config = toml.loads(toml_str) | |
config = load_config(config) | |
merge_a_into_b(cmd_config, config) | |
return config | |
if __name__ == '__main__': | |
fire.Fire(parse_config_cmdargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment