Python | Reading configuration .ini files

Code # 1: config file

 

abc.ini

 

; Sample configuration file

[installation]

library = % (prefix) s / lib

include = % (prefix) s / include

bin = % (prefix) s / bin

prefix = / usr / local

 
# Customization related to debug configuration
[debug]

pid - file = / tmp / spam.pid

show_warnings = False

log_errors = true

[ server]

nworkers: 32

port: 8080

root = / www / root

signature:

Code # 2: Read the file and extract values.

from configparser import ConfigParser

 

configur = ConfigParser ()

print (configur.read ( `config.ini` ))

 

print ( " Sections: " , configur.sections ())

print ( "Installation Library:"  , configur.get ( `installation` , `library` ))

print ( "Log Errors debugged? : " , configur.getboolean ( ` debug` , `log_errors` ))

print ( "Port Server:" , configur.getint ( `server` , ` port` ))

print ( "Worker Server:" , configur.getint ( `server` , `nworkers` ))

  

Output:

 [` config.ini`] Sections: [`installation`,` debug`, `server`] Installation Library:` / usr / local / lib` Log Errors debugged? : True Port Server: 8080 Worker Server: 32 

You can also change the configuration and write it back to a file using the cfg.write () method.

Code # 3:

Output:

 [installation] library =% (prefix) s / lib include =% (prefix) s / include bin =% (prefix) s / bin prefix = / usr / local [debug] log_errors = False show_warnings = False [server] port = 9000 nworkers = 32 pid-file = /tmp/spam.pid root = / www / root 
  • Configuration files are good for specifying configuration data for your program. In each configuration file, values ​​are grouped into different sections (for example, Install, Debug, and Server).
  • Each section has a specific meaning for various variables in that section. For the same purpose, there are some significant differences between a configuration file and using a Python source file.
  • First, the syntax is much more permissive and "sloppy".

Names, used in the config file are also considered case insensitive as shown in the code below —

configur. set ( `server` , ` port` , `9000` )

configur. set ( `debug` , ` log_errors` , `False` )

  

import  sys

configur.write (sys.stdout)

configur.get ( `installation` , ` PREFIX` )

 

configur.get ( ` installation` , `prefix` )

Output:

 `/ usr / local`` / usr / local` 

When parsing values, methods such as getbool ean () look for any reasonable value. For example, they are all equivalent.

 log_errors = true log_errors = TRUE log_errors = Yes log_errors = 1 

The most notable contrast between config writing and Python code is that, unlike scripts, config files are not executed from top to bottom. Rather, the file is read completely. In case the variable replacements are made, they are made later after the fact. For example, it doesn`t matter that the prefix variable is placed after the various variables that use it.

 [installation] library =% (prefix) s / lib include =% (prefix) s / include bin =% ( prefix) s / bin prefix = / usr / local 

Several configuration files can be read together and their results can be combined into one configuration using ConfigParser, which makes it so special to use.

Example — user made their own config file which looks like.

; ~ / .config.ini [installation] prefix = / Users / beazley / test [debug] log_errors = False 

This file can be merged with the previous configuration by reading it separately

Code # 4:

import os

 
# Previously read configuration

print (configur.get ( `installation` , `prefix` ))

  
# Merge in custom config

print (configur .read (os.path.expanduser ( `~ / .config.ini` )))

print (configur.get ( `installation` , ` prefix` ))

print (configur.get ( ` installation` , `library` ))

 

print (configur.getboolean ( `debug` , ` log_errors` ))

Exit :

 `/ usr / local` [` / Users / HP / .config.ini`] `/ Users / HP / test`` / Users / HP / test / lib` False