Dealing with config files what's the options

Dave Brueck dave at pythonapocrypha.com
Fri Feb 25 22:42:00 EST 2005


Tom Willis wrote:
> On Fri, 25 Feb 2005 15:02:04 -0700, Dave Brueck
>>>How about writing them in Python?
>>
>>Depending on who will be editing the config files, this can be a great approach.
[snip]
> I actually thought of this, and I was kind of on the fence due to the
> intended audience.
> 
> I don't think it's too much to ask that they are comfy with the
> concept of variables. I mean, if it was a shell script they'd be at
> the top of the file anyway.
> 
> Then again I'm some what hesitant to help them make the connection
> that I'm giving them the ability to indirectly edit the code. Kind of
> like opening pandoras box. Once the figure out they can open any file
> (*.py) with notepad, there will be utter anarchy and I'll get the call
> at 4am that somethings wrong with the production data.

If you're giving them an executable (i.e. py2exe'd), then you can just exclude 
config.py from the exe.

Either way, if you're not so worried about malicious breakage but ignorant 
breakage, then you could always name your config file something like 
'options.cfg' and then:

import new, sys

# Do this at app startup
sys.modules['config'] = new.module('config')
exec file('options.cfg').read() in config.__dict__

# All other modules do this
import config
conn = config.maxConnections
... etc ...

-Dave



More information about the Python-list mailing list