How to setup Configuration files using config.ini and Python

How to setup Configuration files using config.ini and Python

Table of Contents

What is a configuration file ?

The first valid question you might need to ask yourself is what is these configuration files, what does it contain. You have seen them in other git hub repositories or sometimes comes in a bundle with the software you installed. Let’s answer this. Configuration files contain data that acts as initial settings for your application like.

  • The names of the databases to connect to.
  • The URI of some other third party resources which your application might use.
  • Different Environment settings (Development, Staging, Production).
  • Environmental variables to fetch.
  • Default values to use.
  • Set policies for users etc.

Configuration files can come in the form of .cnf, .cfg, .confg, .ini, .yaml etc.

Understanding config.ini structure

Now we know that config.ini hold the configuration details for our application. Now lets us understand the structure of the ini file.

Given below is a sample config.ini file.

				
					[DEFAULT]
home_dir = '/users'
prod_dir = '/meg'
# we can have no values
temp_dir = 
AppName = coolapp
Version = 1.2.0
Concurrency = 45
WelcomeMessage = Welcome to the cool app we aim
                 to please, Do give us your valuable feedback
# Basic Interpolation syntax
AppPath=%(home_dir)s/main
ThreadUsage = True
#maxMemory = 1000

[Development]
userPath = ${home_dir}/serv/app
DbName = DemoApp
DbURI = mongodb://sysop:moon@localhost
maxMemory = 1000

[Production]
# Extended Interpolation syntax
userPath = ${DEFAULT:home_dir}/${DEFAULT:prod_dir}/serv/app
DbName = ProdApp
DbURI = mongodb://sysop:moon@localhost/records
maxMemory = 8000

				
			

The config.ini file consists of sections [sections]. We can observe the same in the sample config.ini given here.

The sections are highlighted. Generally, here we give our main group of configurations, in the example, I have mentioned default settings and the keys and the values to be used, then Development settings and production settings. We can access them individually in python.

Some other characteristics of config.ini include.

Interpolation in Config Parser

The ConfigParser which we will use to parse this config.ini supports Interpolation.

This means values can be preprocessed before returning them. In the given example under the DEFAULT section, we have the key AppPath which refers to a value in which we pass the value of the key home_dir, this passing of values is done in the same section so this is called Basic Interpolation.

Under the DEVELOPMENT and PRODUCTION section for key userPath, we are passing the home_dir key’s value which is under the DEFAULT section this is called Extended Interpolation. This is handy when you want to change a part of the string in all the sections in all the environments.

Parse config.ini in Python code

To parse the config.ini file we will use the config parser library.
Let’s begin by making a new python script my_config.py and importing the config parser library.

Configuration file

This is our initial setup. Let’s go line by line.

We first import the library

				
					import configparser
				
			

We instantiate the class with External interpolation.

				
					config = configparser.ConfigParser()
				
			

Now we read our configuration file config.ini.

				
					config.read('config.ini')
				
			

Now lets print out each sections we have in the config.ini file.

				
					print(config.sections())
>>> ['Development', 'Production']

				
			

You may notice only the DEVELOPMENT and PRODUCTION environment sections are printed. The DEFAULT section does not get printed out as it is a section that internally provides default values for all other sections.

Lets read  section variables. We can do this in two ways. Shown below:

 

				
					
value = config["Development"]["DbName"]
# or
value = config.get("Development","DbName",fallback='No such things as monsters')
				
			

You will notice that the config parser, parse the configuration file like a dictionary so each section is the main key of the outer dictionary and inside that, there are nested key-value pairs forming the inner dictionary.
We can also use the config.get() method for reading the same the first argument is the section name and the second is the key name, we also have a fallback option suppose the value is not present then this fallback value is chosen

				
					[DEFAULT]
AppName = coolapp
Version = 1.2.0
Concurrency = 45
WelcomeMessage = Welcome to the cool app we aim
                 to please, Do give us your valuable feedback
ThreadUsage = True
#maxMemory = 1000

[Development]
DbName = DemoApp
DbURI = mongodb://sysop:moon@localhost
maxMemory = 1000



				
			
				
					value = config.get("Development","Concurrency",fallback=50)
print(value)
>>> 45
				
			

From the example on your left, we can see that the DEVELOPMENT section has no key Concurrency but we give a fallback value of 50 in the above code, but if we print out the Concurrency value(above) we can see that the DEFAULT(left) section Concurrency have higher precedence compared to the rest.

				
					from configparser import ConfigParser, ExtendedInterpolation
config = ConfigParser(interpolation=ExtendedInterpolation())
				
			

Creating Configuration file from python to ini file

As discussed above we can treat a config parser much like a dictionary. We are going to create our own configuration and create the config2.ini file.

Let’s start by importing the necessary library and instantiating the class.

				
					import configparser

config = configparser.ConfigParser()
				
			

Now let’s add a default section, which is a simple dictionary.
we also will add a section called DEVELOPMENT, which again is a dictionary..

				
					config['DEFAULT'] = {"AppName":"AppSynth_2","concurrency":70,"compressionLevel":2}
config['DEVELOPMENT'] = {}
config['DEVELOPMENT']['user'] = 'dev_user'
config['DEVELOPMENT']['localhost'] = "http://127.0.0.1:8081" 

				
			

We will write this now to our config2.ini file

				
					with open('config2.ini','w') as configfile:
    config.write(configfile)
				
			

We get the config2.ini file created.

Python config.ini file

Share

Facebook
Twitter
Pinterest
LinkedIn