Difference between revisions of "The configparser module in Python"

From Jens' Hjørne
Jump to navigationJump to search
m
m
 
Line 1: Line 1:
The official documentation for the configparser module can be found [https://docs.python.org/3/library/configparser.html here]. The current page is only meant as my comments to that page. For actual proper documentation of the module, please refer to official page. Unles otherwise specified all discussion on this page assumes default values of all options for the ConfigParser object.
+
The official documentation for the configparser module can be found [https://docs.python.org/3/library/configparser.html here]. This page is only meant as my comments to that page. For actual proper documentation of the module, please refer to official page. Unless otherwise specified all discussion on this page assumes default values of all options for the ConfigParser object.
  
 
==DEFAULT and other section names==
 
==DEFAULT and other section names==
  
The official documentation mentions that default values will override fallback values, if the default value exist when get is called. But if you aren't reading the documentation serially, then you might not know, that default values is taken from the section named <tt>'DEFAULT'</tt>.  
+
The official documentation mentions that default values will override fallback values, if the default value exist when <tt>get</tt> is called. But if you aren't reading the documentation serially, then you might not realise, that default values is taken from the section named <tt>'DEFAULT'</tt>.  
  
 
As of the time of writing this (2022-04-20), if you create a section named by the empty string, it will display as precisely that (a section called ''[empty-string]'' containing whatever key.value pairs is put in it), until the file is written to disk. When the file is written to disk, the key,value pairs in the section called ''[empty-string]'' will be written to the <tt>DEFAULT</tt> section, and an empty section, called ''[empty-string]'', will be created.
 
As of the time of writing this (2022-04-20), if you create a section named by the empty string, it will display as precisely that (a section called ''[empty-string]'' containing whatever key.value pairs is put in it), until the file is written to disk. When the file is written to disk, the key,value pairs in the section called ''[empty-string]'' will be written to the <tt>DEFAULT</tt> section, and an empty section, called ''[empty-string]'', will be created.

Latest revision as of 13:29, 21 April 2022

The official documentation for the configparser module can be found here. This page is only meant as my comments to that page. For actual proper documentation of the module, please refer to official page. Unless otherwise specified all discussion on this page assumes default values of all options for the ConfigParser object.

DEFAULT and other section names

The official documentation mentions that default values will override fallback values, if the default value exist when get is called. But if you aren't reading the documentation serially, then you might not realise, that default values is taken from the section named 'DEFAULT'.

As of the time of writing this (2022-04-20), if you create a section named by the empty string, it will display as precisely that (a section called [empty-string] containing whatever key.value pairs is put in it), until the file is written to disk. When the file is written to disk, the key,value pairs in the section called [empty-string] will be written to the DEFAULT section, and an empty section, called [empty-string], will be created.

This means that this code:

import configparser as cp
conf = cp.ConfigParser()
conf[] = {'Int value 1': 42, 'String value 1': 'String value test', 'Float value 1': 3.1415}
with open('test.ini', 'w') as f:
    conf.write(f)

Will produce this file:

[DEFAULT]
int value 1 = 42
string value 1 = String value test
float value 1 = 3.1415

[]

Assuming the DEFAULT section, when given en empty string, is somewhat documented in the discussion of the has_option(section, option) method, where it states "If the specified section is None or an empty string, DEFAULT is assumed." But leaving an empty section called [empty-string] isn't mentioned at all. Leaving this empty section in the written file breaks configparser itself, because it will throw an exception, when reading the file containing this empty section.

Keeping the test.ini file written by the earlier code, we can use this code to read it:

import configparser as cp
conf = cp.ConfigParser()
conf.read('test.ini')

This will produce an exception, caused by the empty section:

configparser.ParsingError: Source contains parsing errors: 'test.ini'
        [line  6]: '[]\n'

This seems to be because the section is empty, since putting a value in the section, i.e. like this

[]
float value 1 = 3.1415

will produce this exception:

configparser.DuplicateOptionError: While reading from 'test.ini' [line  7]: option 'float value 1' in section 'DEFAULT' already exists

Specifying strict=False when creating the object will revert this exception to the previous (configparser.ParsingError: Source contains parsing errors: 'test.ini'\n[line 6]: '[]\n').