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

J

jamitwidme

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
 
C

Cédric Lucantis

Le Thursday 26 June 2008 16:41:27 (e-mail address removed), 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.
 
M

Matimus

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]()
 
J

jamitwidme

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top