473,383 Members | 1,870 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,383 software developers and data experts.

how to get function names from the file

I have got names of functions stored in the file. For the simplicity
expect one row only with two function names: printFoo, printFOO
In my code I would like to define functions and then to read function
names from the file, so the functions can be executed in the order the
function names are stored in a file.

While trying to read the names from the file I am getting always
"strings" and I am not able to execute them.

I would like to write my code so it will look something like:

def printFoo():
print "foo"

def printFOO():
print "FOO"

# here I would like to read the file with the function names sequences
# and to create tuple which will contain the function names.
# After that I would like to call functions from the tuple:

functions=(printFoo, printFOO)
for function in functions:
function()

Thanks for your postings
Petr Jakes

Feb 15 '06 #1
7 2099
Try the following:

def printFoo():
print "Foo"

def printFOO():
print "FOO"

functions = ("printFoo", "printFOO") # list or tuple of strings from
file, or wherever
for function in functions:
call = function + "()"
eval(call)

Feb 15 '06 #2
Petr Jakes wrote:
I have got names of functions stored in the file. For the simplicity
expect one row only with two function names: printFoo, printFOO
In my code I would like to define functions and then to read function
names from the file, so the functions can be executed in the order the
function names are stored in a file.

While trying to read the names from the file I am getting always
"strings" and I am not able to execute them.

I would like to write my code so it will look something like:

def printFoo():
print "foo"

def printFOO():
print "FOO"

# here I would like to read the file with the function names sequences
# and to create tuple which will contain the function names.


If the functions are in the same module as the calling code:
functions=('printFoo', 'printFOO')
for function in functions:
globals()[function]()

If the functions are in a 'functions' module:
funcs=('printFoo', 'printFOO')
for function in funcs:
getattr(functions, function)()

Kent
Feb 15 '06 #3
The following will return a dictionary containing the names and
functions of all the public functions in the current module. If a
function starts with an underscore _, it is considered private and not
listed.

def _ListFunctions():
import sys
import types
d = {}
module = sys.modules[__name__]
for key, value in module.__dict__.items():
if type(value) is types.FunctionType:
fnname = value.__name__
if fnname[0] != '_':
d[value.__name__] = value
return d

Feb 15 '06 #4

"Petr Jakes" <mc********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I have got names of functions stored in the file. For the simplicity
expect one row only with two function names: printFoo, printFOO
In my code I would like to define functions and then to read function
names from the file, so the functions can be executed in the order the
function names are stored in a file.

While trying to read the names from the file I am getting always
"strings" and I am not able to execute them.

I would like to write my code so it will look something like:

def printFoo():
print "foo"

def printFOO():
print "FOO"
Make a dict mapping names to functions:

funs = {'printFoo':printFoo, 'printFOO':printFOO}
# here I would like to read the file with the function names sequences
# and to create tuple which will contain the function names.
# After that I would like to call functions from the tuple:


Actually, str.split, the easiest way to separate the multiple names on a
line, gives you a list. Same difference to 'for'.

funnames=('printFoo', 'printFOO')
for fname in funnames:
funs[fname]()

Terry Jan Reedy

Feb 15 '06 #5
Petr Jakes wrote:
I have got names of functions stored in the file. For the simplicity
expect one row only with two function names: printFoo, printFOO
In my code I would like to define functions and then to read function
names from the file, so the functions can be executed in the order the
function names are stored in a file.

While trying to read the names from the file I am getting always
"strings" and I am not able to execute them.

I would like to write my code so it will look something like:

def printFoo():
print "foo"

def printFOO():
print "FOO"

# here I would like to read the file with the function names sequences
# and to create tuple which will contain the function names.
# After that I would like to call functions from the tuple:

functions=(printFoo, printFOO)
for function in functions:
function()

Thanks for your postings
Petr Jakes

I would do this as follows:

Create dictionary with the function names as keys and the pointer to
function definition as value:

fdict={'printFoo': printFoo, 'printFOO': printFOO}
functions=('printFoo', 'printFOO')
for function in function:
if fdict.has_key(function: fdict[function]()
else:
print "No function named=%s defined" % function

-Larry Bates

Feb 16 '06 #6
Petr Jakes wrote:
I have got names of functions stored in the file. For the simplicity
expect one row only with two function names: printFoo, printFOO
In my code I would like to define functions and then to read function
names from the file, so the functions can be executed in the order the
function names are stored in a file.
Somehow, when people invent little languages like you are doing now,
the languages tend to grow over time...until you realize that you
should have written the scripts in Python... It's all up to you of
course, but making your code contain proper Python code might be
something to consider. Little languages are sometimes useful.

lu*************@gmail.com wrote: functions = ("printFoo", "printFOO") # list or tuple of strings from
file, or wherever
for function in functions:
call = function + "()"
eval(call)
I wouldn't do this. eval has security issues, and it's
overkill for simply finding names in a namespace as you
saw in the other replies.

Kent Johnson wrote: If the functions are in the same module as the calling code:
functions=('printFoo', 'printFOO')
for function in functions:
globals()[function]()
and Larry Bates wrote (slightly corrected): Create dictionary with the function names as keys and the pointer to
function definition as value:

fdict={'printFoo': printFoo, 'printFOO': printFOO}
functions=('printFoo', 'printFOO')
for function in functions:
if fdict.has_key(function): fdict[function]()
else:
print "No function named=%s defined" % function


These two options are basically the same. The
difference is that Kent suggest that you use a
mapping of names to functions provided by Python,
while Larry suggests that you make one yourself.
(BTW, instead of globals() you might want locals()
depending on what scope your functions are defined
in.)

While Kent's suggestion is a little less work, Larry's
suggestion buys you some more benfits:

- You can use other names than the actual function names
as keys in the dict. This means that:
-You can use reserved words (in, while etc) in your little
script
-You can rename functions and reorganize your code without
breaking your scripts.
-You can have command names in your script that contain
national characters, spaces, punctuation, start with digits,
etc (won't, stop!, 1st time, 1.2.45.start etc).

- It's safer: You control exactly what functions the
script might call. Those who write code you run eval
on can basically get arbitrary code executed.

$ cat > evil.py
print "Gotcha"
$ python
[snip]
def x(): print 'Ok' .... eval('x'+'()') Ok eval('__import__("evil") and x'+'()')

Gotcha
Ok

If you want more than just function names in your minilanguage,
you might want to have a look at the shlex module.
Feb 17 '06 #7
"eval" is not necessary in this case.
If you have a tuple with function names such as this: x=(printFoo,
printFOO)

you can execute them this way:
for f in x:

f()

Feb 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: drhowarddrfinedrhoward | last post by:
I see a number of pages with functions like MM_somefunction(). Where does the MM_ come from? I don't see it in any books I'm studying.
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
12
by: Eric | last post by:
I've got a pretty large C program with global variables and function names strewn about (i.e. no "static" declarations in front of them). Now I want to expose the ability for user's to supply their...
3
by: Luke | last post by:
I'm pretty stuck at the moment and wondering if anyone can spot the problem. Trying to create a function that will read a text file into a list and return that list. I wrote the following...
3
by: cybernerdsx2 | last post by:
Hi, I notice a function prototype being declared as following: FileStream.h ========= extern void openFile(char *__ident, int __option); But, in the function declaration part shown as...
3
by: Rico | last post by:
Hello, I have a generic process that logs errors from different sources. When I call this code, I'd like to also submit the name of the function or sub that is raising the error without having...
13
by: Jim Mackellan | last post by:
In my opinion, C not mungling its function names imposes unnecessary complexity on C++, requiring extern 'C' { ... } everywhere in headers. I believe that in the next version of the ISO standard,...
16
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.