简体   繁体   中英

How to define and select groups of values using configobj?

I would like to define several groups of values where the values of a particular group are used if that group is selected.

Here's a an example to make that clearer:

[environment]
type=prod

[prod]
folder=data/
debug=False

[dev]
folder=dev_data/
debug=True

Then to use it:

print config['folder'] # prints 'data/' because config['environment']=='prod'

Is there a natural or idiomatic way to do this in configobj or otherwise?


Additional Info

My current thoughts are overwriting or adding to the resulting config object using some logic post parsing the config file. However, this feels contrary to the nature of a config file, and feels like it would require somewhat complex logic to validate.

I know this is maybe not exactly what you're searching for, but have you considered using json for easy nested access?

For example, if your config file looks like

{
    "environment": {
        "type": "prod"
    },
    "[dev]": {
        "debug": "True",
        "folder": "dev_data/"
    },
    "[prod]": {
        "debug": "False",
        "folder": "data/"
    }
}

you can access it with [dev] or [prod] key to get your folder:

>>> config = json.loads(config_data)
>>> config['[dev]']['folder']
'dev_data/'
>>> config['[prod]']['folder']
'data/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM