Dictionnary vs Class for configuration
Robert Brewer
fumanchu at amor.org
Fri Apr 30 14:02:38 EDT 2004
Famille Delorme wrote:
> I wrote a program in Python for a school project and I am in trouble.
> I have make a Python script called conf.py. This file
> contains dictionnarys
> for configuration like this:
> config_sql = {
> "DATABASE" : "nanana",
> "USERDB" : "bob",
> "PASSWORD" : "********"
> }
> config_script = {
> "TIMETOSLEEP" : 100
> "PATH" : "/home/script"
> }
> The config_section variable is included in each modules
> (script python) used
> in my program
> (with from config.py import config_section)
> And the use is like this
> from conf.py import config_sql
> print config["DATABASE"]
>
> But my master say it's better to do a class like this
> class config :
> def __init__(self, part=="") :
> if (part=="SQL") :
> self.database="nanana"
> self.userdb="bob"
> self.password="*******"
> elif (part=="SCRIPT") :
> self.timetosleep=10
> self.path="/home/script"
> ....
>
> and the use like this
> from conf.py import config
> cfg=config("SQL")
> print cfg.database
>
> We have do only a file for configuration because the
> administrator is no
> searching for configuration.
> I want know :
> - what is more faster, dictionnary method or class method?
> - what use more ram memory ?
> - if you are administrator, what method you like for
> configure program ?
>
> Note:
> * the class will not have methods, it is not necessary.
> * the program is composed of several modules which import
> config_section.
People who install your code are 'deployers' of that code. When you use
'import' to load configuration data from a Python module, you force your
deployers to learn Python. This is a bad choice, in my opinion.
Therefore, both methods are insufficient.
Look at the ConfigParser module for a better way to maintain data which
is provided by your deployers.
Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org
More information about the Python-list
mailing list