Dictionnary vs Class for configuration

Discussion in 'Python' started by Famille Delorme, Apr 30, 2004.

  1. 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
    Famille Delorme, Apr 30, 2004
    #1
    1. Advertising

  2. Famille Delorme

    Jorge Godoy Guest

    On Sex 30 Abr 2004 14:38, Famille Delorme wrote:

    > 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 ?


    Not answering directly --- because if it is for your professor and he said
    he would like to have it one way you should do what he says... --- but have
    you looked at ConfigParser module?

    --
    Godoy. <>
    Jorge Godoy, Apr 30, 2004
    #2
    1. Advertising

  3. Famille Delorme

    Larry Bates Guest

    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" <> wrote in message
    news:40929016$0$8635$...
    > 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
    >
    >
    >
    >
    >
    >
    Larry Bates, Apr 30, 2004
    #3
  4. Famille Delorme

    Donn Cave Guest

    In article <40929016$0$8635$>,
    "Famille Delorme" <> wrote:
    ....
    > 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 ?


    I think there would be some arguments in favor of a class, but
    they're not very important - at least, they're certainly not
    evident in your example. But you raise an important point, about
    efficiency.

    It isn't important, and you shouldn't think about anything else
    until you understand this.

    I mean, not that efficiency isn't important in general - it
    certainly is - but not for a configuration record. It will
    have only a handful of parameters, one or two instances in
    the whole program. This is absurdly trivial. Some time,
    see if you can determine how much Python actually does just
    to start itself up each time. Just for perspective.

    Don't worry about the efficiency of a construct that you aren't
    going to exercise very hard. Worry instead about maintainability,
    because that's where software more commonly meets its fatal end.
    Think about what your instructor is saying in that context.

    Donn Cave,
    Donn Cave, Apr 30, 2004
    #4
  5. Famille Delorme

    Andrei Guest

    Famille Delorme wrote on Fri, 30 Apr 2004 19:38:53 +0200:

    > The config_section variable is included in each modules (script python) used
    > in my program
    > (with from config.py import config_section)


    I agree with the other posters that unless you know the end users know
    Python, you shouldn't configure in Python code (well, unless you have a
    system (e.g. GUI) to change it for people who don't know Python).

    > 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"
    > ....


    Given the choice between dictionary and class, I would probably take the
    dictionary, because it just has more of a "storage" feel to it. OTOH, the
    class requires less typing of quotes, which slightly decreases the amount
    of time spent on writing the program :).

    > 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?


    Speed in this case is completely irrelevant.

    > - what use more ram memory ?


    As long as you don't plan to have dozens of megabytes of configuration, I
    don't really see why this would make any difference in your choice neither.

    > - if you are administrator, what method you like for configure program ?


    GUI, INI file or, if constrained to your two options, the dictionary
    approach. Dictionary is probably easier to understand and less intimidating
    for non-programmers than the class. With the class being modified by
    non-programmers I'd be very afraid they'd break indentation even if they
    get the rest of the syntax right.

    --
    Yours,

    Andrei

    =====
    Real contact info (decode with rot13):
    . Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
    gur yvfg, fb gurer'f ab arrq gb PP.
    Andrei, Apr 30, 2004
    #5
  6. Famille Delorme

    Y2KYZFR1 Guest

    Donn Cave <> wrote in message news:<>...
    > In article <40929016$0$8635$>,
    > "Famille Delorme" <> wrote:
    > ...
    > > 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 ?

    >
    > I think there would be some arguments in favor of a class, but
    > they're not very important - at least, they're certainly not
    > evident in your example. But you raise an important point, about
    > efficiency.
    >
    > It isn't important, and you shouldn't think about anything else
    > until you understand this.
    >
    > I mean, not that efficiency isn't important in general - it
    > certainly is - but not for a configuration record. It will
    > have only a handful of parameters, one or two instances in
    > the whole program. This is absurdly trivial. Some time,
    > see if you can determine how much Python actually does just
    > to start itself up each time. Just for perspective.
    >
    > Don't worry about the efficiency of a construct that you aren't
    > going to exercise very hard. Worry instead about maintainability,
    > because that's where software more commonly meets its fatal end.
    > Think about what your instructor is saying in that context.
    >
    > Donn Cave,



    listen do Donn! He is the only one that has given the right answer :)
    Y2KYZFR1, May 1, 2004
    #6
  7. Famille Delorme

    Peter Otten Guest

    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
    Peter Otten, May 1, 2004
    #7
  8. Famille Delorme

    3Zen Guest

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

    "Larry Bates" <> a écrit dans le message de
    news:...
    > 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" <> wrote in message
    > news:40929016$0$8635$...
    > > 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
    > >
    > >
    > >
    > >
    > >
    > >

    >
    >
    3Zen, May 1, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    0
    Views:
    809
  2. NOBODY
    Replies:
    2
    Views:
    804
    Thomas Weidenfeller
    Oct 17, 2003
  3. Sophie Alléon

    newbie question about dictionnary ?

    Sophie Alléon, Sep 5, 2003, in forum: Python
    Replies:
    9
    Views:
    316
    Bengt Richter
    Sep 5, 2003
  4. Bob Ippolito

    Re: Identity dictionnary

    Bob Ippolito, Feb 29, 2004, in forum: Python
    Replies:
    1
    Views:
    303
    Jean-Sebastien Roy
    Feb 29, 2004
  5. Sigfried
    Replies:
    9
    Views:
    466
    Tom Anderson
    Dec 2, 2008
Loading...

Share This Page