A short introduction to python parsers (part 1)

Posted by Saugata Chatterjee on May 19, 2022 · 2 mins read

We want speed. We want efficiency. We want fast production environment. But we all know parsing config files requires a lot of coding. It is possible to achieve the same result with very few lines of code, using parsing modules.

Configparser

Imagine running multiple SQL queries at multiple databases each with their own roles and permissions. Each comes with their own settings. Imagine loading the settings every time you connect to the server. The amount of code that needs to be written would be gargantuan. Too much work!!

Let Configparser do the heavy lifting. It is a parser written specifically to load configuration files.

We only need few lines of code to load the settings. We can be even use relative references. An example,

[Paths]
data_dir=/media/me/Work/projects/canvasautomation/data
input_dir=${data_dir}/datainput
output_dir=${data_dir}/dataoutput
charts_dir=${output_dir}/charts
reports_dir=${output_dir}/reports
logincredfile=${input_dir}/login_cred

Note that only the root directory has the full path and all sub-directories are using relative paths. We can load these settings directly as a dictionary using the following code.

import configparser

def load_settings(settingsfile='settings.ini'):
    parser = configparser.ConfigParser()
    # expand relative references
    parser._interpolation = configparser.ExtendedInterpolation()
    parser.read(settingsfile)
    for sect in parser.sections():
        settings = {k: v for k, v in parser.items(sect)}
    return settings

This snippet of code returns the settings as a python dict with all the relative paths properly expanded as follows.

{'data_dir': '/media/me/Work/projects/canvasautomation/data',
 'input_dir': '/media/me/Work/projects/canvasautomation/data/datainput',
 'output_dir': '/media/me/Work/projects/canvasautomation/data/dataoutput',
 'charts_dir': '/media/me/Work/projects/canvasautomation/data/dataoutput/charts',
 'reports_dir': '/media/me/Work/projects/canvasautomation/data/dataoutput/reports',
 'logincredfile': '/media/me/Work/projects/canvasautomation/data/datainput/login_cred'
 }

It’s that easy!! In the next post we will talk about other parsers.