Dictionnary vs Class for configuration

3Zen fadelorme at free.fr
Sat May 1 09:27:18 EDT 2004


Thank you, I think a ini file is better for configuration. I will look the
ConfigParser module.

"Larry Bates" <lbates at swamisoft.com> a écrit dans le message de
news:5p6dnTePS_YtPg_dRVn-hw at comcast.com...
> Ultimately you must answer to your professor, but...
>
> I firmly believe that configuration parameters belong
> in a configuration file.  ConfigParser module handles
> these very well.  Essentially it builds the dictionary
> for you in the form of a ConfigParser class instance.
> Then when you wish to change a parameter, edit the
> config file and the next time the program runs it reads
> the new parameters.  You didn't mention your platform,
> but I do a lot on Windows and "freeze" my programs using
> py2exe, so configuration files come in really handy to
> make those "run-time" variables available to the program.
> I also find that people are quite comfortable editing
> these files.
>
> Config file would look something like:
>
> [database]
> name=nanana
> userdb=bob
> password=********
>
> [other]
> timetosleep=100
> path=/home/script
>
> program to read this:
>
> import sys
> def ini_error(section, option):
>     #
>     # Insert code here to handle missing parameters
>     #
>     print "Unable to locate section=%s, option=%s in .INI file" %
(section,
> option)
>     sys.exit(2)
>
>
> import ConfigParser
> ini_filename="/directory/where/config/stored/program.ini"
> INI=ConfigParser.ConfigParser()
> INI.read(ini_filename)
>
> section="database"
> option="name"
> try:    database=INI.get(section, option)
> except: ini_error(section, option)
>
> option="userdb"
> try:    userdb=INI.get(section, option)
> except: ini_error(section, option)
>
> option="password"
> try:    password=INI.get(section, option)
> except: ini_error(section, option)
>
> section="other"
> option="name"
> try:    timetosleep=INI.getint(section, option)
> except: timetosleep=100
>
> option="path"
> try:    path=INI.get(section, option)
> except: ini_error(section, option)
>
> You get the idea.
>
> Regards,
> Larry Bates
> Syscon, Inc.
>
> "Famille Delorme" <fadelorme at free.fr> wrote in message
> news:40929016$0$8635$626a14ce at news.free.fr...
> > Sorry if this discussion are already submited, but I don't find anything
> > really interresting for me.
> > And sorry for my bad english, it is not my native language.
> >
> > 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.
> >
> >
> > Thank you for futures answers,
> > 3Zen
> >
> >
> >
> >
> >
> >
>
>





More information about the Python-list mailing list