473,467 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 = {
"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
7 1536
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matt Mastracci | last post by:
It seems like the C# compile can't resolve the difference between a namespace and a class in a situation that is clearly unambiguous. Note that it sees the namespace "C" before the class "C". I...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
3
by: Florida Coder | last post by:
I have the need to store some application specific configuration data to be used by a class library and or a windows service. I would like to do this in a fashion similar to the way we do with...
9
by: Steve | last post by:
Hello -- I've been struggling with this problem for over a day now. I'd like to know (without passing parameters) which class, and preferably, which method of that class has called my function....
9
by: Geoffrey | last post by:
Hello, Form my software, I create a class to manage configuration. Configuration parameters are retrieved from wml file, ini file, registry, .... For this, I have a constructor that create a...
16
by: Robert Dufour | last post by:
Here's the class code snippet Imports System.Configuration.ConfigurationManager Public Class Class1 Public _strTestSetting As String Public Sub SetTestsetting()
5
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The...
7
by: Peter Bradley | last post by:
OK. A bit behind the times, I know; but we're just moving over to .NET 2.0. How on earth do you manage configuration settings in a class library in .NET 2.0? In version 1.1, we used a handy class...
11
by: pmstel | last post by:
I have a dictionnary with variable in it that I would like to get it in global variable. example: sketch_dict = { "width":48.0, "length":12.5, "thk":0.75 } for it in sketch_dict: if...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.