By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,767 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,767 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
Le Thursday 26 June 2008 16:41:27 ja********@gmail.com, vous avez écrit*:
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.

You can find the function in the global dictionary (returned by globals()):

globs = globals()
func_name = config.read('1234', 'function')
func = globs[func_name]

# and then call it
func()

But a safer way would be to use a class with some static methods:

class Functions (object) :

@staticmethod
def efgh () :
blah blah...

and then find the function in the class dict:

func = getattr(Functions, func_name)
func()

this way you can restrict the set of functions the user can give, excluding
those which are not supposed to be called this way.

--
Cédric Lucantis
Jun 27 '08 #2

P: n/a
On Jun 26, 7:41*am, jamitwi...@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]()
Jun 27 '08 #3

P: n/a
Thank you for the answers.
Now I understood how to call a function, let me ask you another
question.

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

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

class Functions:
def efgh(self):
print 'blah'

fcn = Functions()

config = ConfigParser.ConfigParser()
config.read('configuration.cfg')
title = config.get('1234','title') # number 1
function_name = config.get('1234','function')

title = getattr(fcn, function_name)
title()
--------------------------------------------------------

instead of assigning string value('abcd') to title at number 1

I want to assign this function(fcn.efgh()) to abcd and make abcd a
FunctionType.
so later on, I want to call it by abcd(), not title().
The reason is I will have a loop reading from configuration file, so I
need to have different names for each function.
abcd is a string I read got it from config.get('1234','title')

Thanks again.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.