[Tutor] How do you implement a config file?

Carroll, Barry Barry.Carroll at psc.com
Mon Jul 17 18:47:56 CEST 2006


Tracy:

> Date: Sat, 15 Jul 2006 22:44:43 -0700 (PDT)
> From: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
> Subject: Re: [Tutor] How do you implement a config file?
> To: Tracy R Reed <treed at ultraviolet.org>
> Cc: tutor at python.org
> Message-ID: <Pine.LNX.4.64.0607152238410.27927 at hkn.eecs.berkeley.edu>
> Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
> 
> 
> 
> > I am writing a small python application that needs a few variables
to be
> > end user configurable. Right now I just have the variables right up
> > front where the user can tweak them in the program code and a big
> > commented line that says "Nothing editable past this point." But I
would
> > like to be able to break this out into a separate config file. There
are
> > two ways I see to do this:
> 
> Hi Tracy,
> 
> [config parser approach cut]
> 
> > I don't see any good way to do that either without actually making
my
> > config file a module and calling it config.py instead of
> > application.config.
> 
> This second approach --- using a module as a configuration file --- is
the
> programmer-friendly one.  *grin* If you can get away with this, it's
> probably the simplest to implement.  It also seems to be the approach
that
> most Python programs use, bar more sophisticated approaches like XML
or
> some other structured data format.
> 
> We had some discussion about this earlier the last few weeks (and
months!
> Someone should put this on the FAQ!), and the concensus seems to be
that
> ConfigParser is a bit limiting:
> 
>
http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/3116794
>      http://mail.python.org/pipermail/tutor/2006-June/047557.html

I like to implement config data as a Python dictionary.  Doing so makes
things almost trivial. To use your example:

In config.py
>>>>>>>
configdata = {
  "foo": ['bar','baz','bah']
  .
  .
  .
}
>>>>>>>

In application.py
>>>>>>>
from config import configdata
.
.
.
foo = configdata["foo"]
>>>>>>>

The drawback, of course, is that the application's administrator must
understand enough Python to fill in a dictionary.  As Danny says, if you
can get away with that caveat, the dictionary approach is easy to
implement.  

Another method is to store your config data in XML format.  This is a
little more complex to implement, but has a couple of advantages. First,
since XML is a pretty widely accepted standard, is reasonable to expect
the application's administrator to know it.  Second, you can name the
config file whatever you want: "application.config" works just fine
(although you might want to add "XML" to the file name somewhere).

Again, from your example:

In appXML.config:
>>>>>>>
[configdata]
  [foo]
    bar
    baz
    bah
  [/foo]
  .
  .
  .
[/configdata]
>>>>>>>

I haven't used XML for this purpose (yet), so I can't help you with the
Python XML parser, but you can get more information on the WEB:

      http://pyxml.sourceforge.net/topics/

HTH.

Regards,
 
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed




More information about the Tutor mailing list