Dictionnary vs Class for configuration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Famille Delorme

    Dictionnary vs Class for configuration

    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 = {
    "TIMETOSLEE P" : 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="bo b"
    self.password=" *******"
    elif (part=="SCRIPT" ) :
    self.timetoslee p=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






  • Jorge Godoy

    #2
    Re: Dictionnary vs Class for configuration

    On Sex 30 Abr 2004 14:38, Famille Delorme wrote:
    [color=blue]
    > 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 ?[/color]

    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. <godoy@ieee.org >

    Comment

    • Larry Bates

      #3
      Re: Dictionnary vs Class for configuration

      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(secti on, 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=ConfigParse r.ConfigParser( )
      INI.read(ini_fi lename)

      section="databa se"
      option="name"
      try: database=INI.ge t(section, option)
      except: ini_error(secti on, option)

      option="userdb"
      try: userdb=INI.get( section, option)
      except: ini_error(secti on, option)

      option="passwor d"
      try: password=INI.ge t(section, option)
      except: ini_error(secti on, option)

      section="other"
      option="name"
      try: timetosleep=INI .getint(section , option)
      except: timetosleep=100

      option="path"
      try: path=INI.get(se ction, option)
      except: ini_error(secti on, option)

      You get the idea.

      Regards,
      Larry Bates
      Syscon, Inc.

      "Famille Delorme" <fadelorme@free .fr> wrote in message
      news:40929016$0 $8635$626a14ce@ news.free.fr...[color=blue]
      > 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[/color]
      dictionnarys[color=blue]
      > for configuration like this:
      > config_sql = {
      > "DATABASE" : "nanana",
      > "USERDB" : "bob",
      > "PASSWORD" : "********"
      > }
      > config_script = {
      > "TIMETOSLEE P" : 100
      > "PATH" : "/home/script"
      > }
      > The config_section variable is included in each modules (script python)[/color]
      used[color=blue]
      > 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="bo b"
      > self.password=" *******"
      > elif (part=="SCRIPT" ) :
      > self.timetoslee p=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
      >
      >
      >
      >
      >
      >[/color]


      Comment

      • Donn Cave

        #4
        Re: Dictionnary vs Class for configuration

        In article <40929016$0$863 5$626a14ce@news .free.fr>,
        "Famille Delorme" <fadelorme@free .fr> wrote:
        ....[color=blue]
        > 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 ?[/color]

        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@u.washingt on.edu

        Comment

        • Andrei

          #5
          Re: Dictionnary vs Class for configuration

          Famille Delorme wrote on Fri, 30 Apr 2004 19:38:53 +0200:
          [color=blue]
          > The config_section variable is included in each modules (script python) used
          > in my program
          > (with from config.py import config_section)[/color]

          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).
          [color=blue]
          > 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="bo b"
          > self.password=" *******"
          > elif (part=="SCRIPT" ) :
          > self.timetoslee p=10
          > self.path="/home/script"
          > ....[/color]

          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 :).
          [color=blue]
          > 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?[/color]

          Speed in this case is completely irrelevant.
          [color=blue]
          > - what use more ram memory ?[/color]

          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.
          [color=blue]
          > - if you are administrator, what method you like for configure program ?[/color]

          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):
          cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
          gur yvfg, fb gurer'f ab arrq gb PP.

          Comment

          • Y2KYZFR1

            #6
            Re: Dictionnary vs Class for configuration

            Donn Cave <donn@u.washing ton.edu> wrote in message news:<donn-EB7C7F.13060030 042004@nntp1.u. washington.edu> ...[color=blue]
            > In article <40929016$0$863 5$626a14ce@news .free.fr>,
            > "Famille Delorme" <fadelorme@free .fr> wrote:
            > ...[color=green]
            > > 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 ?[/color]
            >
            > 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@u.washingt on.edu[/color]


            listen do Donn! He is the only one that has given the right answer :-)

            Comment

            • Peter Otten

              #7
              Re: Dictionnary vs Class for configuration

              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 = {
              "TIMETOSLEE P" : 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.p y>

              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

              Comment

              • 3Zen

                #8
                Re: Dictionnary vs Class for configuration

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

                "Larry Bates" <lbates@swamiso ft.com> a écrit dans le message de
                news:5p6dnTePS_ YtPg_dRVn-hw@comcast.com. ..[color=blue]
                > 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(secti on, option):
                > #
                > # Insert code here to handle missing parameters
                > #
                > print "Unable to locate section=%s, option=%s in .INI file" %[/color]
                (section,[color=blue]
                > option)
                > sys.exit(2)
                >
                >
                > import ConfigParser
                > ini_filename="/directory/where/config/stored/program.ini"
                > INI=ConfigParse r.ConfigParser( )
                > INI.read(ini_fi lename)
                >
                > section="databa se"
                > option="name"
                > try: database=INI.ge t(section, option)
                > except: ini_error(secti on, option)
                >
                > option="userdb"
                > try: userdb=INI.get( section, option)
                > except: ini_error(secti on, option)
                >
                > option="passwor d"
                > try: password=INI.ge t(section, option)
                > except: ini_error(secti on, option)
                >
                > section="other"
                > option="name"
                > try: timetosleep=INI .getint(section , option)
                > except: timetosleep=100
                >
                > option="path"
                > try: path=INI.get(se ction, option)
                > except: ini_error(secti on, option)
                >
                > You get the idea.
                >
                > Regards,
                > Larry Bates
                > Syscon, Inc.
                >
                > "Famille Delorme" <fadelorme@free .fr> wrote in message
                > news:40929016$0 $8635$626a14ce@ news.free.fr...[color=green]
                > > 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[/color]
                > dictionnarys[color=green]
                > > for configuration like this:
                > > config_sql = {
                > > "DATABASE" : "nanana",
                > > "USERDB" : "bob",
                > > "PASSWORD" : "********"
                > > }
                > > config_script = {
                > > "TIMETOSLEE P" : 100
                > > "PATH" : "/home/script"
                > > }
                > > The config_section variable is included in each modules (script python)[/color]
                > used[color=green]
                > > 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="bo b"
                > > self.password=" *******"
                > > elif (part=="SCRIPT" ) :
                > > self.timetoslee p=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[/color][/color]
                ?[color=blue][color=green]
                > >
                > > 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
                > >
                > >
                > >
                > >
                > >
                > >[/color]
                >
                >[/color]


                Comment

                Working...