Re: how to get function names from the file
"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() -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get function names from the file
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. [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get function names from the file
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get function names from the file
"Petr Jakes" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >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 -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get function names from the file
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get function names from the file
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get function names from the file
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) -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list