Python handling configuration files




















Therefore, you should use dummy data non-real data on VCS instead of real secret data for protecting you from external security attacks. And then you will replace the dummy data with real configuration values in production server by yourself, but, it would be cumbersome. This way loads the configuration values defined in external file, not the built-in data structures. It is more general way because this way will treats the configuration as just configuration , not as a part of code.

Note that configparser is for Python 3. You can also use other formats such as xml, yaml. The basic approach is same, so, any formats are ok if there is a way for parsing that formats. In this approach, the configuration will be separated from code, so if you are using Git, just able to specify the file to ignore from VCS. However, when the configuration is completely separated from VCS, your co-worker may not able to use the configuration. So, you can use a following tip for solving this problem.

Suppose the config. Then make an example configuration file with name which indicate it is an example like config. And you should manage the only config. This allows other developers to know the format and manipulate the configuration by themselves. Of course, they should rename that file name with config. This way does not use the files, instead system environment variables as configuration values.

Seems not to be used at all for configuration files by the Python community. One is BeautifulSoup:. File Endings give the user and the system an indicator about the content of a file. Reasonable file endings for configuration files are. For the other options, users might get asked which program they want to use. It should be declared close to where it is used. Let me explain these principles and their consequences below. However, there is a number of drawbacks to this approach: It is not possible to detect inconsistent spelling, for example whether a key was "user" or "users".

If there is an inconsistency, there is no single point where the correct schema is defined. Correct is whatever happens to be in the dictionary. Missing data is not discovered until the data is actually accessed. Tools that check consistent formatting of variable names cannot be used.

Repeated string parsing and dictionary lookups are unnecessarily expensive. Doing so solves the problems listed above: In compiled languages the compiler obviously tells you right away if there is a spelling mistake, but also for Python a sufficiently modern IDE usually points out if an undeclared variable or class member is used.

The class definition is the one ground truth that defines what the correct name is. Even in Python it can happen that a declared variable has not been initialized see PEP , but in many cases the IDE or linter tells you about it.

Renaming is easily done using IDE support. Normal formatters or style checkers can be applied. Early Validation For most configuration values, there is a certain shape, type, or range of data that makes sense.

For example, you could write assert len config. However, this leads to a couple of problems: The value must be validated at every location where it is used, leading to code duplication. Alternatively, you need to remember whether it was already validated or not when you use it. If something is wrong, then the problem shows up only when the configuration value is accessed for the first time.

This makes it harder to spot errors and takes more effort to check that a new configuration value is actually valid. As written above, in Python even if it says port: int in the class declaration, config. That is something that you definitely do not want to check every time you use the value. Declare Configuration Where it is Used This last principle states that configuration entries should be declared close to the place where they are used, for example as a class in the same module as the code that uses it.

However, it has a couple of advantages in terms of software engineering, when compared with declaring all the configuration entries in a single place: The physical closeness helps navigating, for example it is easier to find the places where a certain configuration entry is used. Furthermore, if you are using a data structure that also defines the valid bounds for a configuration value, it makes sense to do that close to the code that is relying on these bounds.

It helps to avoid using the same configuration entry in different, unrelated components. Assume you have an entry, for example something like timeout , defined in a central place and accessible from all modules, then it is tempting to reuse that same timeout entry in various unrelated places rather than adding a new entry, naming and documenting it appropriately.

If a configuration is defined locally in the module it is easier to see that this is a bad thing to do, for example you would most likely not import web. When testing a component that takes configuration as a parameter, you only need to mock a configuration object with the locally used entries, rather than the complete configuration for the whole application.

Learn more. Asked 2 years, 5 months ago. Active 2 years, 5 months ago. Viewed times. Is this a good way or is there a better way to do this? ConfigParser self. Vinay Hegde 1, 1 1 gold badge 9 9 silver badges 23 23 bronze badges.

HMan06 HMan06 2 2 gold badges 8 8 silver badges 21 21 bronze badges. You haven't said if you plan to edit the config file in Python. I would tend to pass the ConfigParser object around self. I'm not even sure I'd create a dedicated Config class as you've done.



0コメント

  • 1000 / 1000