The "right" way to use config files

Fabien fabien.maussion at gmail.com
Sat Aug 9 07:48:17 EDT 2014


Folks,

I am not a computer scientist (just a scientist) and I'd like to ask 
your opinion about a design problem I have. It's not that I can't get my 
program to work, but rather that I have trouble to find an "elegant" 
solution to the problem of passing information to my program's elements. 
I have trouble to be clear in my request so my apologies for the long 
text...

The tool I am developing is a classical data-analysis workflow. Ideally, 
all the program's configurations are located in a single .cfg file which 
I parse with ConfigObg. The file contains I/O informations 
(path_to_input, path_to_output) as well as internal options 
(use_this_function, dont_use_this_one, function1_paramx = y), etc...

Currently, my program is a "super-object" which is initialized once and 
work schematically as follows:

main():
	obj = init_superobj(config file)
	obj.preprocess()
         obj.process()
	obj.write()

the superobj init routine parses the config files and reads the input data.

and a processing step can be, for example:

def process():

	if self.configfile.as_bool('do_funcion1'):
		params = config.parse_function1_params()
		call_function1(self.data, params)
	
	if self.configfile.as_bool('do_funcion2'):
		params = config.parse_function2_params()
		call_function2(self.data2, params)

The functions themselves do not know about the superobject or about the 
configfile. They are "standalone" functions which take data and 
parameters as input and produce output with it. I thought that the 
standalone functions will be clearer and easier to maintain, since they 
do not rely on some external data structure such as the configobj or 
anything.

BUT, my "problem" is that several options really are "universal" options 
to the program, such as the output directory for example. This 
information (where to write their results) is given to most of the 
functions as parameter.

So I had the idea to define a super-object which parses the config file 
and input data and is given as a single parameter to the processing 
functions, and the functions take the information they need from it. 
This is tempting because there is no need for refactoring when I decide 
to change something in the config, but I am afraid that the program may 
become unmaintainable by someone else than myself. Another possibility 
would be at least to give all the functions access to the configfile.

To get to the point: is it good practice to give all elements of a 
program access to the configfile and if yes, how is it done "properly"?

I hope at least someone will understand what I mean ;-)

Cheers and thanks,

Fabien












More information about the Python-list mailing list