By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,091 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

Dictionnary vs Class for configuration

P: n/a
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


Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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. <go***@ieee.org>
Jul 18 '05 #2

P: n/a
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" <fa*******@free.fr> wrote in message
news:40**********************@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


Jul 18 '05 #3

P: n/a
In article <40**********************@news.free.fr>,
"Famille Delorme" <fa*******@free.fr> 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, do**@u.washington.edu
Jul 18 '05 #4

P: n/a
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):
ce******@jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
Jul 18 '05 #5

P: n/a
Donn Cave <do**@u.washington.edu> wrote in message news:<do************************@nntp1.u.washingto n.edu>...
In article <40**********************@news.free.fr>,
"Famille Delorme" <fa*******@free.fr> 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, do**@u.washington.edu

listen do Donn! He is the only one that has given the right answer :-)
Jul 18 '05 #6

P: n/a
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

Jul 18 '05 #7

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

"Larry Bates" <lb****@swamisoft.com> a écrit dans le message de
news:5p********************@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" <fa*******@free.fr> wrote in message
news:40**********************@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



Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.