[python] python's configparser may be better than managing settings in yaml

This is the content of configparser of the standard python library. configparser can write options that depend on certain options. You can also fill in the dependencies later. With this one point alone, it may be more convenient to write the config file in json and json.load (), or write the config file in yaml and yaml.load ().

Specifically, the following code works.

There is a configuration file (config.ini) like the one below

[DEFAULT]
here = /foo/bar/boo
app = %(here)s/app%(suffix)s

The DEFAULT section has options for here and app. Here app depends on here, suffix. Also, the suffix option is not described in the configuration file.

from configparser import ConfigParser

parser = ConfigParser(default_section="DEFAULT")
with open("config.ini") as inp
    parser.readfp(inp)
parser["DEFAULT"]["suffix"] = ".py"

Here, I got the value of the app option that depends on the suffix.

print(parser["DEFAULT"]["app"])  # /foo/bar/boo/app.py

Recommended Posts

[python] python's configparser may be better than managing settings in yaml
If you write go table driven test in python, it may be better to use subTest
Golang vs. Python – Is Golang Better Than Python?