Iterate over sections in a config file
Asked Answered
E

3

30

I recently got introduced to the library configparser. I would like to be able to check if each section has at least one Boolean value set to 1. For example:

[Horizontal_Random_Readout_Size]
Small_Readout  = 0
Medium_Readout = 0
Large_Readout  = 0

The above would cause an error.

[Vertical_Random_Readout_Size]
Small_Readout  = 0
Medium_Readout = 0
Large_Readout  = 1

The above would pass. Below is some pseudo code of what I had in mind:

exit_test = False
for sections in config_file:
    section_check = False
    for name in parser.options(section):
        if parser.getboolean(section, name):
            section_check = True
    if not section_check:
        print "ERROR:Please specify a setting in {} section of the config file".format(section)
        exit_test = True
    if exit_test:
        exit(1)

Questions:

1) How do I perform the first for loop and iterate over the sections of the config file?

2) Is this a good way of doing this or is there a better way? (If there isn't please answer question one.)

Erigena answered 27/2, 2014 at 12:7 Comment(0)
O
85

Using ConfigParser you have to parse your config.

After parsing you will get all sections using .sections() method.

You can iterate over each section and use .items() to get all key/value pairs of each section.

for each_section in conf.sections():
    for (each_key, each_val) in conf.items(each_section):
        print each_key
        print each_val
Organotherapy answered 27/2, 2014 at 12:16 Comment(1)
Note that the [DEFAULT] section is not included in the list returned by conf.sections()Rufus
M
1

To complete the answer by @Nilesh and comment from @PashMic, here is an example that really iterate over ALL sections, including DEFAULT:

all_section_names: list[str] = conf.sections()
all_section_names.append("DEFAULT")
for section_name in all_section_names:
    for key, value in conf.items(section_name):
        ...

Note that even if there is no real "DEFAULT" section, this will still works. There will just be no item retreived by conf.items("DEFAULT").

Marinara answered 16/6, 2022 at 16:26 Comment(0)
A
0

Best bet is to load ALL the lines in the file into some kind of array (I'm going to ignore the issue of how much memory that might use and whether to page through it instead).

Then from there you know that lines denoting headings follow a certain format, so you can iterate over your array to create an array of objects containing the heading name; the line index (zero based reference to master array) and whether that heading has a value set.

From there you can iterate over these objects in cross-reference to the master array, and for each heading check the next "n" lines (in the master array) between the current heading and the next.

At this point you're down to the individual config values for that heading so you should easily be able to parse the line and detect a value, whereupon you can break from the loop if true, or for more robustness issue an exclusivity check on those heading's values in order to ensure ONLY one value is set.

Using this approach you have access to all the lines, with one object per heading, so your code remains flexible and functional. Optimise afterwards.

Hope that makes sense and is helpful.

Abert answered 27/2, 2014 at 12:14 Comment(1)
Note that Python has a dedicated library to parse the format of that config file (which holds everything in one structure in memory), after which this is simply an issue of what API calls to use.Adolpho

© 2022 - 2024 — McMap. All rights reserved.