config42 icon indicating copy to clipboard operation
config42 copied to clipboard

How to get the final configuration with defaults included?

Open asaff1 opened this issue 4 years ago • 3 comments

Check this or we will delete your issue. (fill in the checkbox with an X like so: [x])

  • [x] I have searched for other issues with the same problem or similar feature requests.

Select one:

  • [ ] Bug
  • [ ] Feature Request
  • [x] Technical Help
  • [ ] Other

Operating System? (include version)

  • [ ] macOS
  • [ ] Windows
  • [x] Linux (Ubuntu)
  • [ ] Android (include terminal)

Python Version Requirement

  • [x] I am using Python 3.6 or above

Exact Python Version?

Python 3.7.8

Pip Version?

pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.7)

Description of your issue

I hoped there's an easy way to print the final configuration, with defaults included. The as_dict method is not very useful for this:

DEFAULTS = {'a': '2', 'b': '3'}
cfg = ConfigManager(prefix='APP', defaults=DEFAULTS)
print("config:", cfg.as_dict())

Running APP_A="" python myapp.py prints:
config: {'a': ''} The problem: 'b' not included.

Also I think it should be possible to get a dict representation of the entire final config, I'm surprised that dict(config_manager) does not work. Is that on purpose?

asaff1 avatar Jul 06 '20 22:07 asaff1

Thank you for your interest in config42. Yes, as_dict() returns only the parameters read by the configuration manager (config42. The defaults params are accessible by calling them directly by using the config.get("your.param.key").

Do you prefer having a method to return the mixed configuration parameters containing the defaults params? like = as_dict(defaults=True)

yurilaaziz avatar Jul 07 '20 11:07 yurilaaziz

  1. Actually I'm not sure what as_dict() is supposed to do. Does it returns the configuration values, or a dict description of the configuration? Can you explain how the defaults argument is used here? https://config42.readthedocs.io/en/latest/tutorial.html#step-4-run-multiple-instances-of-shortly

  2. I think the __iter__ method of ConfigManager is not implemented as it should, currently it only dumps the keys, which means that list(config_manager) does work, but dict(config_manager) does not.
    I would refactor it to include defaults and to yield a pair, which will make it possible to dump the configuration with dict(config_manager). This is somewhat a breaking change for the API, but I think it does make sense.

Your suggested way is ok for me though. Also, the library could support to output in as a nested dict, or as a flat dict.

asaff1 avatar Jul 07 '20 15:07 asaff1

yes, a PR is really appreciated!

yurilaaziz avatar Jul 07 '20 17:07 yurilaaziz