Skip to content

Instantly share code, notes, and snippets.

@budui
Last active August 16, 2019 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save budui/8c454ad572763854996549655515271f to your computer and use it in GitHub Desktop.
Save budui/8c454ad572763854996549655515271f to your computer and use it in GitHub Desktop.
use toml as config format.
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()
@budui
Copy link
Author

budui commented Aug 15, 2019

example usage:

python3 toml-arg.py -c stage1.toml -t "loss.mask_l1.weight=100" -t "log.verify.freq=10" -t "ti=[1, 2]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment