Config Parser Get All Sections On Essay

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.

Basic configparser usage

These are some examples on using ConfigParser, assuming the following INI file...

[SectionOne] Status: Single Name: Derek Value: Yes Age: 30 Single: True [SectionTwo] FavoriteColor = Green [SectionThree] FamilyName: Johnson [Others] Route: 66>>> import ConfigParser >>> Config = ConfigParser.ConfigParser() >>> Config <ConfigParser.ConfigParser instance at 0x00BA9B20> >>>"c:\\tomorrow.ini") ['c:\\tomorrow.ini'] >>> Config.sections() ['Others', 'SectionThree', 'SectionOne', 'SectionTwo'] >>>

Explanation: We first import the configparser, tell it to read the file, and get a listing of the sections. Sections are listed in square brackets [].

Next, we are going to get some settings, after defining a helper function.

The Function:

def ConfigSectionMap(section): dict1 = {} options = Config.options(section) for option in options: try: dict1[option] = Config.get(section, option) if dict1[option] == -1: DebugPrint("skip: %s" % option) except: print("exception on %s!" % option) dict1[option] = None return dict1

Now the code:

>>> Name = ConfigSectionMap("SectionOne")['name'] >>> Age = ConfigSectionMap("SectionOne")['age'] >>> print "Hello %s. You are %s years old." % (Name, Age) Hello Derek. You are 30 years old.

This works great most of the time, but what about the "" and "" values? Those are booleans. They can be either True or False, Yes or No, 1 or 0, on or off. To read a boolean value, you use: Example, continuing from above:

>>> single = Config.getboolean("SectionOne", "single") >>> single True

You can also use to get a number as an int. This may be easier to use than There is also which is used the same as getint, but, as you guessed, returns a float instead of an int.

Notes on reading an INI file

lines beginning with a semicolon ';' a pound sign '#' or the letters 'REM' (uppercase or lowercase) will be ignored. You may use these for comments if you want. You cannot put a comment on an option line. It will only be treated as a comment if it is at the beginning of the line!

Writing an INI file

When you write to an INI file, you will wipe out all comments.

Assuming the config file doesn't exist yet, this is the code to create one:

# lets create that config file for next time... cfgfile = open("c:\\next.ini",'w') # add the settings to the structure of the file, and lets write it out... Config.add_section('Person') Config.set('Person','HasEyes',True) Config.set('Person','Age', 50) Config.write(cfgfile) cfgfile.close()

Advanced configparser usage


Using ExtendedInterpolation one can make use of cross-chapter flexible parameter values. For instance, using the following ini file:

[SectionOne] Param1: Hello Param2: World [SectionTwo] Param1: ${SectionOne:Param1} ${SectionOne:Param2} [SectionThree] Alpha: One Bravo: Two Charlie: ${Alpha} Mississippi

By setting _interpolation to ExtendedInterpolation() the values become dynamic.

>>> import configparser >>> settings = configparser.ConfigParser() >>> settings._interpolation = configparser.ExtendedInterpolation() >>>'settings.ini') ['settings.ini'] >>> settings.sections() ['SectionOne', 'SectionTwo', 'SectionThree'] >>> settings.get('SectionTwo', 'Param1') 'Hello World' >>> settings.get('SectionThree', 'Charlie') 'One Mississippi'


ConfigParserExamples (last edited 2016-04-11 04:17:05 by bignose)

0 thoughts on “Config Parser Get All Sections On Essay

Leave a Reply

Your email address will not be published. Required fields are marked *