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

Matimus mccredie at gmail.com
Thu Jun 26 17:19:29 CEST 2008


On Jun 26, 7:41 am, jamitwi... at gmail.com wrote:
> 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

Something like this:
globals()[fcn]()



More information about the Python-list mailing list