Dynamically create ConfigurationSets? #93
-
I'm hoping to build a ConfigurationSet based on user input where the user may choose one or more Configurations. Can this be done dynamically (in terms of number of Configurations) with import config
config_default = {
"1": 1.1,
"2": 2.2,
"3": 3.3,
}
config_choices = {
"a": {
"1": 45.2,
},
"b": {
"2": 6.7,
},
}
user_input = ["a", "b"]
# user_input = ["a"]
cfg = config.ConfigurationSet(
[config.config_from_dict(config_choices[choice]) for choice in user_input] + [config_default]
) Context: I'd like the user to be able to select one or more configs from a set of predefined configs. The configs will fall back sequentially and ultimately rely on a default config. I want the non-default configs to only specify differences from the default. I have gotten all the pieces of this to work using Using Python 3.9 and Edit: Make keys strings. Didn't realize this was required. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yes, this was one of the use cases the lib was build for. Try this: cfg = config.ConfigurationSet(
config.config_from_dict(config_default),
*[config.config_from_dict(config_choices[choice]) for choice in user_input]
) The issue with your code above is that a |
Beta Was this translation helpful? Give feedback.
Yes, this was one of the use cases the lib was build for. Try this:
The issue with your code above is that a
ConfigurationSet
expects the configs as arguments and you're passing a list -- unwrapping with the*
will do the trick.