Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ConfigParser: Can I read(ConfigParser.get()) a configuration file anduse it to call a funciton?

Reply
Thread Tools

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

 
 
jamitwidme@gmail.com
Guest
Posts: n/a
 
      06-26-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
 
Reply With Quote
 
 
 
 
Cédric Lucantis
Guest
Posts: n/a
 
      06-26-2008
Le Thursday 26 June 2008 16:41:27 , 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
 
Reply With Quote
 
 
 
 
Matimus
Guest
Posts: n/a
 
      06-26-2008
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]()
 
Reply With Quote
 
jamitwidme@gmail.com
Guest
Posts: n/a
 
      06-26-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicK Mike ASP .Net 5 08-15-2007 08:57 AM
Enterprise Library exception: The configuration section securityCryptographyConfiguration is not defined in the current configuration for the AppDomain. Jess Chadwick ASP .Net 1 09-21-2006 09:18 AM
Configuration reverted to previous configuration after power loss ward@sciinc.com Cisco 0 03-03-2006 04:14 PM
Microsoft Configuration Block and Enterprise library configuration tool Mark ASP .Net 0 02-15-2006 11:27 PM
PEAP Configuration Woes - PEAP configuration help jester Cisco 1 12-20-2005 02:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57