config42
config42 copied to clipboard
How to get the final configuration with defaults included?
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?
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)
-
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 -
I think the
__iter__
method ofConfigManager
is not implemented as it should, currently it only dumps the keys, which means thatlist(config_manager)
does work, butdict(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 withdict(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.
yes, a PR is really appreciated!