ConfigParser: Can I read(ConfigParser.get()) a configuration file and use it to call a funciton?

jamitwidme at gmail.com jamitwidme at gmail.com
Thu Jun 26 10:41:27 EDT 2008


Hello. I am a novice programmer and have a question

I have a configuration file(configuration.cfg)
I read this from reading.py using ConfigParser
When I use ConfigParser.get() function, it returns a string.
I want to call a function that has the same name as the string from
the configuration file.


configuration.cfg
---------------------------------------
[1234]
title: abcd
function: efgh
---------------------------------------


reading.py
--------------------------------------------------------
import ConfigParser

def efgh():
   print 'blah'

config = ConfigParser.ConfigParser()
config.read('configuration.cfg')

fcn = config.get('1234','function')
type(fcn)
print fcn
--------------------------------------------------------

<type 'str'>
efgh


Is there any way to call efgh() ?
One way I know is using if statement
if fcn == 'efgh':
   efgh()

But I am going to have many functions to call, so I want to avoid
this.


Thank you for your help



More information about the Python-list mailing list