Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Simple ini Config parser examples needed

Thread Tools

Re: Simple ini Config parser examples needed

Tim Chase
Posts: n/a
> Tim provided a correct-looking answer, albeit somewhat
> complex, as it doesn't reuse the logic in the ConfigParser.

It didn't start out very complex, but it was so easy to make it a
bit more robust with such a scant few lines of code that I went
ahead. The original just looked like

options = {}
for line in file("simple.ini"):
line = line.rstrip("\r\n")
if '=' in line:
key, value = line.split('=', 1)
options[key] = value

which is about as simple as it gets. However, this doesn't
handle commented lines nicely (OP didn't mention whether there
were comments), the keys are case-sensitive (often not a desired
behavior), and leading/trailing whitespace for both the key &
value are preserved (also rarely desired), and in the event of a
malformed line (with no '='), it fails indistinguishably from a
blank line and silently.

The big advantages given by the ConfigParser hack (tacking on a
fake section-header)

- it provides the convenience methods for pulling out an
int/float/bool in addition to a string

- possibly handles continued lines (I saw something in the source
referring to this)

- it handles merging config files from multiple sources (such as
/etc/foo_rc then merged with ~/.foo_rc)

- encourages use-of and learning-about a standard library module

- and it handles string substitution if you want it.

It also provides for defaults, but the standard dict has the
get() method to allow for defaults, and the library offers the

As a personal aside, it does bug me that the ConfigParser .get*()
methods don't afford an optional default value like dict.get() in
the event you want a value that may be present in the config file
but isn't in the [DEFAULT] section when present raising a
NoSectionError or NoOptionError. I.e. I'd like to be able to call

cp.get('some_section', 'some_key', '42')

on an empty config.ini file and get back the "42" as if it
existed in "some_section" rather than throwing an exception/error
(and why the heck do these "exceptions" descend from Error rather
than Exception? These aren't errors...they're exceptions)



Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Boot.ini not in root? Where is Boot.ini for Win 2000 pro? Computer Support 15 02-03-2007 03:39 AM
PyINI : Cross-Platform INI parser SeSe Python 7 02-13-2005 12:43 AM
.ini file parser (ConfigParser) Ian Bicking Python 0 10-22-2004 04:29 AM
simple examples needed with sql2000,, visual basic Ryan McBride ASP .Net 2 12-14-2003 01:05 PM
simple examples needed with sql2000,, visual basic Ryan McBride ASP .Net Datagrid Control 2 12-14-2003 01:05 PM