config files in python

M.-A. Lemburg mal at egenix.com
Tue May 6 04:33:46 EDT 2008


On 2008-05-06 01:16, Matimus wrote:
> On May 4, 11:35 pm, sandipm <sandip.m... at gmail.com> wrote:
>> Hi,
>>  In my application, I have some configurable information which is used
>> by different processes. currently I have stored configration in a
>> conf.py file as name=value pairs, and I am importing conf.py file to
>> use this variable. it works well
>>
>> import conf
>> print conf.SomeVariable
>>
>> but if I need to change some configuration parameteres,  it would need
>> me to restart processes.
>>
>> I want to store this data in some conf file (txt) and would like to
>> use it same way as I am using these variables as defined in py
>> files.
>>
>> one solution I can think of is writing data as a dictionary into conf
>> file. and then by reading data, apply eval on that data. and update
>> local dict? but this is not a good solution....
>>
>> any pointers?
>>
>> Sandip
> 
> I would load the configuration file using `imp.load_source'. This
> allows you to load the config file by filename, and gets away from the
> issue of accidentally importing a file somewhere else in pythons
> search path. Also, calling imp.load_source will reload the module when
> called a second time.
 >
> http://docs.python.org/lib/module-imp.html

Why not just use execfile() ?

http://www.python.org/doc/2.2.3/lib/built-in-funcs.html

> [conf.py]
> a = 1
> b = 2
> class c:
>     a = "hello"
>     b = "world"
> [/end conf.py]
> 
>>>> conf = imp.load_source("conf", "./conf.py")
>>>> conf.a
> 1
>>>> conf.b
> 2
>>>> conf.c.a
> 'hello'
>>>> conf.c.b
> 'world'
> 
> 
> 
> There are so many ways potential solutions to your problem that,
> without any more details, it is hard to suggest anything.
> 
> Here are some potential solutions:
> 
> ConfigParser - module for handling ini files
> xml - several built-in modules for handling XML files
> sqlite3 - a `lite' SQL database built-in in python 2.5 + (can be used
> for config data)
> windows registry  - _winreg module
> pickle - serialize python objects
> marshal - similar to pickle, only works for simple objects
> 
> Those are just the built-in solutions. If you wanna look at 3rd party
> solutions, prepare for overload. The number of alternative INI parsers
> alone is staggering.
> 
> Also, there are many ways to organize your data and use a solution
> similar to what you are already using.
> 
> I guess what I'm trying to say is... don't roll your own, it would be
> a waste of time, this problem has been solved 100s of times. That is,
> unless you want to do it for fun.
> 
> Matt
> --
> http://mail.python.org/mailman/listinfo/python-list

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, May 06 2008)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
            Registered at Amtsgericht Duesseldorf: HRB 46611



More information about the Python-list mailing list