Dictionnary vs Class for configuration

F

Famille Delorme

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
 
J

Jorge Godoy

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

Larry Bates

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 said:
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
 
D

Donn Cave

"Famille Delorme said:
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, (e-mail address removed)
 
A

Andrei

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):
(e-mail address removed). Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
 
Y

Y2KYZFR1

Donn Cave said:
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, (e-mail address removed)


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

Peter Otten

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
 
3

3Zen

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

Larry Bates said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top