jaanli / nomen

:goat: Lightweight configuration trees with command line flags :goat:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Command line arguments override doesn't work

ischurov opened this issue · comments

It seems that nomen doesn't work for me. I have the following file:

import nomen
import yaml
config = """
x: 10
flag: true
"""

if __name__ == "__main__":
    dictionary = yaml.load(config)
    cfg = nomen.Config(dictionary)
    print(cfg)

When I start it from command line and pass an argument --x 101, it does not override default x's value:

(py3.6) user@maccie:temp/nomen-test $ python test_nomen.py --x 101
test_nomen.py:9: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  dictionary = yaml.load(config)
flag: true
x: 10

(py3.6) user@maccie:temp/nomen-test $ python --version
Python 3.6.1 :: Continuum Analytics, Inc.

I use MacOS. Tried Anaconda and Homebrew python with the same results.

Unfortunately, I cannot debug it, because debugger crashes after entering config.py — probably due to infinite recursion in __getattr__.

Thanks @ischurov — that's my fault.

You need to include cfg.parse_args().

I decided to separate that from initializing the object, so that ipdb and jupyter notebooks would have an easier time. Let me know if you have thoughts on this.

This works on my end on python 3.6/MacOS/Anaconda:

import nomen
import yaml
config = """
x: 10
flag: true
"""

if __name__ == "__main__":
    dictionary = yaml.load(config)
    cfg = nomen.Config(dictionary)
    cfg.parse_args()
    print(cfg)

Thanks, works like a charm now!

Cool feel free to ping me if you have any issues or ideas on how to simplify / improve this :)