yurilaaziz / config42

The config-manager package is a complete configuration reader and manager.
GNU General Public License v3.0
23 stars 5 forks source link

How to get the final configuration with defaults included? #13

Open asaff1 opened 4 years ago

asaff1 commented 4 years ago

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

Select one:

Operating System? (include version)

Python Version Requirement

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?

yurilaaziz commented 4 years ago

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)

asaff1 commented 4 years ago

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.

yurilaaziz commented 4 years ago

yes, a PR is really appreciated!