Global Configuration Variables

Michael B. Allen mballen at erols.com
Tue Oct 17 02:04:25 EDT 2000


Hi!

I would like to have some global configuration parameters in a class
that can be accessed like this:

server = smtplib.SMTP(Config.mailserver)
...

My class might have default values, be stored in a module called
config.py, and look like this:

class Config:
    spoolfile = 'mail.spool'
    mailserver = os.environ['HOSTNAME']
    mailaddr = os.environ['USER'] + '@' + mailserver
	...

and then I want to be able to override these defaults with something like:

def configinit(filename):
    execfile(filename, Config.__dict__)

Where the passed file might contain:

mailserver = 'mail.myserver.org'
...

This seem like it should work but I am generally confused with using
globals and how I'm supposed to import my config module. If I import
like this:

from kpdbconfig import Config
from kpdbconfig import configinit

I get an exception :~(

Traceback (innermost last):
  File "./t0.py", line 174, in ?
    conifginit('c0.conf')
NameError: conifginit

What's the best way to do this?

Thanks,
Mike



More information about the Python-list mailing list