Last active
August 16, 2019 07:49
-
-
Save budui/8c454ad572763854996549655515271f to your computer and use it in GitHub Desktop.
use toml as config format.
This file contains 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 toml | |
from argparse import ArgumentParser | |
from os import path | |
import collections | |
# python 3.8+ compatibility | |
try: | |
collectionsAbc = collections.abc | |
except: | |
collectionsAbc = collections | |
def update(d, u): | |
for k, v in u.items(): | |
dv = d.get(k, {}) | |
if not isinstance(dv, collectionsAbc.Mapping): | |
d[k] = v | |
elif isinstance(v, collectionsAbc.Mapping): | |
d[k] = update(dv, v) | |
else: | |
d[k] = v | |
return d | |
def load_config(config_path): | |
print("reading config from <{}>\n".format(path.abspath(config_path))) | |
try: | |
with open(config_path, "r") as f: | |
config = toml.load(f) | |
return config | |
except FileNotFoundError as e: | |
print("can not find config file") | |
raise e | |
def parse_argument(): | |
parser = ArgumentParser("Train") | |
parser.add_argument("-c", "--config", type=str, help="config file path", required=True) | |
parser.add_argument("-t", "--toml", type=str, action="append") | |
options = parser.parse_args() | |
return options | |
def main(): | |
options = parse_argument() | |
config = load_config(options.config) | |
print(config) | |
if options.toml is not None: | |
tomls = "\n".join(options.toml) | |
new_config = toml.loads(tomls) | |
print(new_config) | |
print(update(config, new_config)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example usage: