473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
3 1515
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alexander Kienzle | last post by:
I'm new to Java programming. I'm developing a Servlet for tomcat which needs an external configuration file. With external I mean a file (in XML format) which is customizable and not contained in...
2
by: Al Repasky | last post by:
The program I have written with VB/NET 2003 has a Myapp.exe.config file that I did not know I would be creating. My program using an Access db and can not find the DB file after building the...
1
by: Paul Fi | last post by:
I have this problem with .NET remoting: my remote class is called RemoteHandler which implements an interface called IEazyRemoting which has only one method to be implemented which is my server...
2
by: Peter | last post by:
Hello, Thanks for reviewing my question. I am trying to understand how to use a configuration file for my application, so I can turn debug or trace with a switch. The following is what I put...
7
by: serge calderara | last post by:
Dear all, I am building an ASP 1.1 application based on a n-Tier architecture. I have my datalayer class that need to read the connection string of my sql server database from a configuration...
2
by: genojoe | last post by:
http://msdn2.microsoft.com/en-us/library/system.configuration.configuration This site contains sample code that does not compile for me. The line of code is: ' Get the current configuration...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
3
by: guybenron | last post by:
Hey, I have a sort of petty-neurotic question, I'm kinda pedantic and like to keep my log directories clean, and this thing is bothering me to the point of actually posting a question (couldn't...
5
by: Author | last post by:
This may sound like a silly question. I rarely do console or windows form applications. In a console application, I have an XML configuration file. I place it under my application root. In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.