Hello!
I would like to read in files, during run-time, which contain plain
Python function definitions, and then call those functions by their
string name. In other words, I'd like to read in arbitrary files with
function definitions, using a typical 'open()' call, but then have
those functions available for use.
The 'import' keyword is not appropriate, AFAIK, because I want to be
able to open any file, not one that I know ahead of time (and thus can
import at design-time).
I already have some code that I cobbled together from many newsgroup
posts, but I wonder if there's a cleaner/simpler way to do it.
The following is a toy example of what I'm doing so far. I have a file
called 'bar.txt' that contains two function definitions. I have a main
driver program called 'foo.py' which reads in 'bar.txt' (but should be
able to read any file it hasn't seen before), then calls the two
functions specified in 'bar.txt'.
===== [bar.txt] =====
def negate(x):
return -x
def square(x):
return x*x
===== [foo.py] =====
# open functions file
foo_file = open("bar.txt")
foo_lines = foo_file.readlines()
foo_file.close()
foo_str = "".join(foo_lines)
# compile code
foo_code = compile(foo_str, "<string>", "exec")
foo_ns = {}
exec(foo_code) in foo_ns
# use functions
k = 5
print foo_ns["negate"](k) // outputs -5
print foo_ns["square"](k) // outputs 25
I'm not sure exactly what happens below the surface, but I'm guessing
the 'compile()' and 'exec()' commands load in 'negate()' and 'square()'
as functions in the global scope of 'foo.py'. I find that when I run
'compile()' and 'exec()' from within a function, say 'f()', the
functions I read in from 'bar.txt' are no longer accessible since they
are in global scope, and not in the scope of 'f()'.
Any pointers would be very welcome.
Thanks!
Bryant 11 1950
On 2005-02-11, Bryant Huang <73****@gmail.com> wrote: I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I'd like to read in arbitrary files with function definitions, using a typical 'open()' call, but then have those functions available for use.
that's pretty simple:
$ cat foo.txt
def foo():
print "foo here"
def bar():
print "bar here"
$ cat foo.py
filename = 'foo.txt'
execfile(filename)
foo()
bar()
$ python foo.py
foo here
bar here
The 'import' keyword is not appropriate, AFAIK, because I want to be able to open any file, not one that I know ahead of time (and thus can import at design-time).
This will import a module name determined at run-time:
exec('import %s' % moduleName)
The following is a toy example of what I'm doing so far.
[...]
I'm not sure exactly what happens below the surface, but I'm guessing the 'compile()' and 'exec()' commands load in 'negate()' and 'square()' as functions in the global scope of 'foo.py'. I find that when I run 'compile()' and 'exec()' from within a function, say 'f()', the functions I read in from 'bar.txt' are no longer accessible since they are in global scope, and not in the scope of 'f()'.
Huh? I'm lost. What, exactly, are you trying to accomplish?
Did your example program do what you intended or not?
Is this what you're trying to do?
$ cat foo.txt
def foo():
print "foo here"
def bar():
print "bar here"
$ cat bar.py
def testing():
filename = 'foo.txt'
execfile(filename,globals())
foo()
bar()
testing()
foo()
bar()
$ python bar.py
foo here
bar here
foo here
bar here
--
Grant Edwards grante Yow! Clear the
at laundromat!! This
visi.com whirl-o-matic just had a
nuclear meltdown!!
Bryant Huang wrote: Hello!
I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I'd like to read in arbitrary files with function definitions, using a typical 'open()' call, but then have those functions available for use.
The 'import' keyword is not appropriate, AFAIK, because I want to be able to open any file, not one that I know ahead of time (and thus can import at design-time).
This usage is one of the reasons why the __import__ function exists (despite
what its docs say). It's right there at the top of the library reference's
'builtin functions' section: http://www.python.org/dev/doc/devel/...-in-funcs.html
Instead of writing "import foo as mod" you can write "mod = __import__('foo')".
The first is better when you do know the name of the module you want at coding
time, but the latter is handy when you want to be dynamic about it.
Cheers,
Nick.
--
Nick Coghlan | nc******@email.com | Brisbane, Australia
--------------------------------------------------------------- http://boredomandlaziness.skystorm.net
Ah, thanks a lot, Grant and Nick.
Let me try to clarify because I think I was unclear in specifying what
I want to do:
1. Read in a file containing a bunch of function definitions:
def f1(x):
...
def f2(x):
...
def f3(x):
...
def f4(x):
...
2. In wxPython, populate a CheckListBox with all the functions defined
in that file.
3. Allow the user to check some of the functions, say for example, f1()
and f3().
4. The program then executes f1() and f3() on some specified data.
The reason I asked these questions is because I don't know what
functions are contained in the function file ahead of time, but I still
want to be able to read those in, then based on which functions the
user selects, to run those accordingly, even though I still don't know,
at design-time, what functions are contained in the function file.
Does that make sense?
Thanks a lot!
Bryant
if the file you want to include is not txt, but instead py, it should
be easy.
for example you have fs.py
you just
-------------------------------------
fs=__import__("fs")
f=[a for a in dir(fs) if a[0:2]!='__']
#no you have f1(),f2(),f3() as f[0],f[1],f[2]
then excute desired function, for example f2():
exec("fs."+f[1]+"()")
if you have top level variable in that file, just modify this to test
typs.FunctionType(import types first)
Answer below...
"Bryant Huang" <73****@gmail.com> wrote in message news:<11*********************@g14g2000cwa.googlegr oups.com>... Hello!
I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I'd like to read in arbitrary files with function definitions, using a typical 'open()' call, but then have those functions available for use.
<snip> ===== [bar.txt] =====
def negate(x): return -x
def square(x): return x*x
===== [foo.py] =====
# open functions file foo_file = open("bar.txt") foo_lines = foo_file.readlines() foo_file.close() foo_str = "".join(foo_lines)
# compile code foo_code = compile(foo_str, "<string>", "exec") foo_ns = {} exec(foo_code) in foo_ns
# use functions k = 5 print foo_ns["negate"](k) // outputs -5 print foo_ns["square"](k) // outputs 25
I'm not sure exactly what happens below the surface, but I'm guessing the 'compile()' and 'exec()' commands load in 'negate()' and 'square()' as functions in the global scope of 'foo.py'. I find that when I run 'compile()' and 'exec()' from within a function, say 'f()', the functions I read in from 'bar.txt' are no longer accessible since they are in global scope, and not in the scope of 'f()'.
Any pointers would be very welcome.
Thanks! Bryant
You're actually very close here. The problem you are having is that
you are creating a local namespace (foo_ns) and executing that code
within that namespace. To "import" the functions into the global
namespace, exec them as:
exec foo_code in globals()
You will then be able to call the methods as if they had been declared
locally, ie. 'negate(5)' rather than 'foo_ns["negate"](5)'.
<plug type="shameless">
I've written a simple line editor intended to be used within the
Python interactive interpreter, based on the old DOS 'edlin' command.
This is how I enable the user to "import" the entered functions into
their local namespace. The actual "execution" of the code is actually
pretty simple, and not much more involved than what you see here. You
may be able to get some more ideas, however.
For more info (or comments, suggestions, or pointers), check out http://pyedlin.sourceforge.net).
</plug>
Here's a simple example that shows a "good" input loop:
====== Start exec example ======
if __name__ == '__main__':
funcdata = ['def func():\n', "\tprint 'Hello, world!'\n", '\n']
codeline = ''
for line in funcdata:
codeline += line
execline = codeline
if execline.endswith('\n'):
execline = execline[:-1]
try:
obj = compile(execline, '<string>', 'exec')
except SyntaxError:
pass
else:
exec obj in globals()
codeline = ''
func()
====== End exec sample ======
Executing this code will print 'Hello, world!' to the console.
Hope this helps!
T.J.
I have to say, I was skeptical about your execution method at first --
simply joining all of the lines and then compiling them. My
understanding of the compile method from the documentation led me to
believe that you needed to process a line at a time. ''.join ing them
all seems to work very well, however, and it's a lot easier!
My comments about 'exec obj in globals()' still stand, however. Give
it a try and let me know if it works the way you expect.
T.J.
#--- file bar.py
def negate(n):
return -n
def square(n):
return n*n
#--- end bar.py foo="bar" fs=__import__(foo) import types f=[a for a in dir(fs) if a[0:2]!='__' and
type(getattr(fs,a))==types.FunctionType] f
['negate', 'square'] n=5 exec("print fs."+f[0]+"(n)")
-5 exec("print fs."+f[1]+"(n)")
25
Isn't the problem solved?
or don't use exec():
f=[getattr(fs,a) for a in dir(fs) if a[0:2]!='__' and
type(getattr(fs,a))==types.FunctionType] f
[<function negate at 0x100a00>, <function square at 0x144038>] f[0](n)
-5 f[1](n)
25
Wensheng wrote: #--- file bar.py
<snip> foo="bar" fs=__import__(foo)
<snip>
Wensheng:
The problem with this is that it assumes the text file is a valid
python file, and that the extension is ".py". This may work for the
OP's situation; he would need to weigh in. 'exec'ing the functions
into the global namespace allows you to create functions from any
text-based source -- registry keys, ini files, user input, etc.
T.J.
f=open("bar.txt")
import imp
fs=imp.new_module("fs")
exec f in fs.__dict__
..... rests are the same
althought why use anything other than .py, when you import .py, it get
compiled into .pyc and it load faster next time
Ah, thank you, Wensheng and T.J.! I have drawn bits and pieces from
what you have suggested. Both of your solutions work well. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: OneSolution |
last post by:
Hi All,
Here's what I'm trying to do.
I have a diverse customer base, and as it grows, it's increasingly harder to
figure out what aspect of our web site is selling and what is not. So I've...
|
by: Cindy Lee |
last post by:
I need to insert rows into MySql database from oracle, which I imagine is a
rather common task. The path I decided to take is write a java procedure, as
recommended in the manual. However, java...
|
by: sneill |
last post by:
Using XMLHTTP and DOM I'm able to load new HTML page content.
I'd now like to load small snippets of javascript with the HTML markup
and have that <script> incorporated into the page. If any of...
|
by: Torrent |
last post by:
The Following is an Error I am getting using VS.NET 2003. When I bind the XML and XSLT Document together in Internet Explorer it works fine. However when I attempt to Load the File with the...
|
by: NGM |
last post by:
Hello All
I have a unmanaged C++ DLL, which has been wrapped up with a manged C++ DLL.
When i refer to this managed DLL in Windows form based applications it works out fine. But when i refer to...
| |
by: kwilder |
last post by:
This works, but it doesn't load the latest version of the xml if it was
just modified without closing and reopening the browser.
Here's the scenario:
I have an xml doc called results.xml. It...
|
by: R Reyes |
last post by:
Can someone please explain to me why I can't get the MS Word Interop assembly
to work in my VS2005 project? I'm trying to manipulate MS Word from my Web
Form application and I can't get passed...
|
by: WT |
last post by:
Hello,
I have copied my web site to a test server(W2K3), this web site is using a
reference to an assembly that wraps all the calls to 2 web services (MS CRM
3.0).
This runs perfectly on my dev...
|
by: Cenk Genisol |
last post by:
Hi NG,
I am relatively new to Web developement and have some questions to you.
I've got a Masterpage where some graphics are embedded by CSS. They format
the borders of tables, which store...
|
by: Nickolai Leschov |
last post by:
Hello all,
I am programming an embedded controller that has a 'C' library for using
its system functions (I/O, timers, all the specific devices). The
supplied library has .LIB and .H files.
...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |