Dictionnary vs Class for configuration
Peter Otten
__peter__ at web.de
Sat May 1 03:16:17 EDT 2004
Famille Delorme wrote:
With a little bit of glue you can define the configuration in _your_ style
while accessing it in your _professor's_:
<config.py>
config_sql = {
"DATABASE" : "nanana",
"USERDB" : "bob",
"PASSWORD" : "********"
}
config_script = {
"TIMETOSLEEP" : 100,
"PATH" : "/home/script"
}
</config.py>
<configwrapper.py>
import config
class Config:
def __init__(self, section):
d = getattr(config, "config_%s" % section.lower())
for k, v in d.iteritems():
setattr(self, k.lower(), v)
</configwrapper.py>
Use it:
<useconfig.py>
from configwrapper import Config
cfg = Config("SQL")
print cfg.database
</useconfig.py>
Your _users_ might prefer a solution based on ConfigParser, as they most
likely have already been exposed to its format:
[SQL]
DATABASE=nanana
USERDB=bob
PASSWORD=********
Peter
PS: Remember to avoid storing the password in plain text
More information about the Python-list
mailing list