Re: Loading functions from a file during run-time

2005-02-17 Thread Wensheng
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Loading functions from a file during run-time

2005-02-16 Thread Wensheng
#--- 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

Re: Loading functions from a file during run-time

2005-02-16 Thread Wensheng
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Loading functions from a file during run-time

2005-02-16 Thread tjprojects_usenet
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

Re: Loading functions from a file during run-time

2005-02-15 Thread TJ's Projects
Answer below... Bryant Huang [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... 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

Re: Loading functions from a file during run-time

2005-02-15 Thread tjprojects_usenet
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

Re: Loading functions from a file during run-time

2005-02-12 Thread Wensheng
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

Re: Loading functions from a file during run-time

2005-02-11 Thread Nick Coghlan
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

Re: Loading functions from a file during run-time

2005-02-11 Thread Bryant Huang
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

Re: Loading functions from a file during run-time

2005-02-10 Thread Grant Edwards
On 2005-02-11, Bryant Huang [EMAIL PROTECTED] 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