There are many options for writing configuration files with well-written parsers:
there is no reason for any configuration to be analyzed as a python script directly. This can lead to many problems: from security aspects to complex debugging errors that can be raised at the end of the program life cycle.
There's even discussion to build an alternative to setup.py for python packages, which is pretty close to a python-based source code configuration from the point of view of a python encoder.
Otherwise, you can just see python objects exported as strings that look a bit like json, albeit a little more flexible ... Which is fine if you don't eval() / exec() them or even import them, but pass it through a parser, such as 'ast.literal_eval' or parsing , so that you can only load static data, not executable code.
Only a few times when I realized that something close to the configuration file written in python is a module included in the library that defines the constants used by this library, intended for the user to process the library. Iβm not even sure that it will be a good design decision, but I would understand that.
edit:
I would not consider django settings.py an example of good practice, although I believe that this is part of what I consider a configuration file for literate users, which works great, since django aims to be mainly used by coders and system administrators . In addition, django offers a configuration method through a web page.
To accept the @lego arguments:
- No need to parse the file
there is no need to explicitly disassemble it, although the costs of parsing are anecdotal, even more so taking into account security and additional security and the ability to detect problems at an early stage
- Configuration settings can be more than just key / values
ini files, you can define almost any basic python type using json / yaml or xml. And you do not want to define classes or initialize complex objects in the configuration file ...
- Writing configuration files is easy:
but using a good editor, the syntax of json / yaml or even xml can be checked and checked to have a perfectly processed file.
not an argument neither, as you say, is double-labeled, you may have something that understands well, but raises an exception after many hours of work.
- Configuration options are easily accessible and namespaces:
using json / yaml or xml, parameters can be easily placed in a namespace and naturally used as python objects.
- But the most powerful reason: redefines, redefines, redefines
This is not a good argument for python code. Given that your code consists of several modules that are interdendant and use a common configuration file, and each of them has its own configuration, it is quite easy to load the first main configuration file as an old old python dictionary, and other configuration files are simply loaded by dictionary updates.
If you want to track changes, there are many recipes for organizing the dicts hierarchy, which returns to another dict if it does not contain a value.
And finally, configuration values ββchanged at runtime cannot (actually should not) be serialized correctly in python, as this means changing the currently running program.
I am not saying that you should not use python to store configuration variables, I am just saying that no matter what syntax you choose, you must get it through the parser before getting it as instances in your program. Never download user modifiable content without double checking. Never trust your users!
If django people do this, it's because they built a framework that only makes sense when building a lot of plugins to create an application. And then, to configure the application, you use the database (which is a kind of configuration file ... on steroids) or the actual files.
NTN