I was looking at couple of the python projects. Almost every good python project usually have a app.conf or app.ini file for their configuration management. After that they use something like configparser.py
module to extract configuration information from the app.conf file. In the end they somehow have to use the values in their .py
module.
Let's go to an example
app.conf
[BREED]
beagle=dog
ragdoll=cat
deilenaar=rabbit
cockatoo=parrot
app.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import configparser
config = configparser.ConfigParser()
config.read('app.conf')
print(config['BREED']['cockatoo']) # prints parrot
Then why not to use a dictionary and use that dictionary instead something like the following.
#!usr/bin/env python
# -*- coding: utf-8 -*-
config = {'BREED': {'beagle': 'dog',
'ragdoll': 'cat',
'deilenaar': 'rabbit',
'cockatoo': 'parrot'}}
print(config['BREED']['cockatoo']) # prints parrot
If we need to use config
globally then we will put the variable in __init__.py
in the root of the package. In that way we don't have to read the app.conf file anymore.
I believe there is a really good reason to use app.conf file. So what is that reason ?