ConfigParser: Can I read(ConfigParser.get()) a configuration file and use it to call a funciton?
Cédric Lucantis
omer at no-log.org
Thu Jun 26 10:59:06 EDT 2008
Le Thursday 26 June 2008 16:41:27 jamitwidme at 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
More information about the Python-list
mailing list