config files in python

George Sakkis george.sakkis at gmail.com
Mon May 5 23:11:54 EDT 2008


On May 5, 6:57 pm, Matimus <mccre... at gmail.com> wrote:
> On May 5, 10:22 am, Francesco Bochicchio <bock... at virgilio.it> wrote:
>
>
>
> > On Mon, 05 May 2008 00:35:51 -0700, sandipm 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
>
> >  The 'simple but relatively dangerous way', already suggested, is to
> >  reload() the module.
>
> > A safer way - but requiring more work - could be to build something around
> > the Configparser module in the standard library ...
>
> > Ciao
> > -----
> > FB
>
> How is it relatively dangerous? I mean, aside from any `dangers'
> associated with importing a user defined module in the first place.

Read the caveats in reload()'s description. Briefly:
- It does not reload names imported with "from  ... import ...".
- Any existing instances of classes defined in the module still refer
to the original class, not the reloaded one (assuming it is still
present).
- The module's dictionary (containing the module's global variables)
is retained and updated in place, so names that are deleted in the
module are still available.
- It is not recursive.
- And more...

George



More information about the Python-list mailing list