[Tutor] setting program configuration for all files and modules of a program

2008-04-14 Thread Tim Michelsen
Hello,
I am building a simple GUI for a calculation program. I am using config 
files (*.cfg) and reading them in with ConfigParser. This works well 
because I have nearly all code in 1 text file.
But I would like to modularize my code and separate the GUI code from 
the functional code that provides the calculation operations. This will 
help to expand the functionality at a later stage. I want to achieve 
this through splitting the code into different modules.
The problem is now that I would always have to write the configration 
file specific code in each module that uses the configration settings 
(see code below).

How can I provide the settings stored in the configuration file 
throughout my program to all functions without needing to initiate the 
ConfigParser object in each module?

I through of having a special module settings.py which I could use to 
read in the configuration from the file and then import this in each module.

Is there a more decent and elegant way?
What is the state of the art in storing and parsing configuraions in 
python programs?

Kind regards,
Timmie



# CODE #
import sys
import locale
import ConfigParser
import gettext


## Path to configuration file
configuration_file_name = './config/program_settings.cfg'# name of 
the configuration file

## read configuration from config file
try:
 program_config = ConfigParser.ConfigParser()
 program_config.readfp(open(configuration_file_name))
except IOError, (errno, strerror):
 error_msg_exception = _(I/O error(%s): %s) % (errno, strerror)
 error_msg_no_config = _(No configuration file with name ), 
configuration_file_name, _(found in the program directory. Please copy 
your configuration there. Values will be replaced by defaults.)
 print error_msg_exception
 print error_msg_no_config
 easygui.msgbox(error_msg_no_config)
 #pass

output = program_config.get('output', 'file_extension')
#

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setting program configuration for all files and modules of a program

2008-04-14 Thread Kent Johnson
Tim Michelsen wrote:
 What is the state of the art in storing and parsing configuraions in 
 python programs?

It is pretty common to have a configuration module that is imported 
wherever the configuration is needed. This is simple but it is 
essentially global state and shares some of the disadvantages of 
globals; in particular it couples all your code to the settings module 
and makes testing more difficult.

The alternative is to pass a configuration object to whatever needs it. 
This eliminates the coupling but is more painful to implement.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor