Ah, thank you, Wensheng and T.J.! I have drawn bits and pieces from
what you have suggested. Both of your solutions work well.
--
http://mail.python.org/mailman/listinfo/python-list
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
Wensheng wrote:
> #--- file bar.py
> >>> foo="bar"
> >>> fs=__import__(foo)
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
int
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
[, ]
>>> f[0](n)
-5
>>> f[1](n)
25
--
http://mail.python.org/mailman/listinfo/python-list
#--- 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."+
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
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
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 exampl
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
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
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
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 av
12 matches
Mail list logo