Loading functions from a file during run-time
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, "", "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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Loading functions from a file during run-time
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Loading functions from a file during run-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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Iteration within re.sub()?
Ah beautiful, thank you both, Robert and Mark, for your instant and helpful responses. I understand, so the basic idea is to keep a variable that is globally accessible and call an external function to increment that variable... Thanks! Bryant -- http://mail.python.org/mailman/listinfo/python-list
Iteration within re.sub()?
Hi, Is it possible to perform iteration within the re.sub() function call? For example, if I have a string like: str = "abbababbabbaaa" and I want to replace all b's with an integer that increments from 0, could I do that with re.sub()? Replacing b's with 0's is trivial: i = 0 pat = re.compile("b") print pat.sub(`i`, str) Now, how can I increment i in each replacement? Is this possible? Like, using a lambda function, for example? Or do I use a different re function altogether? I use this trivial [ab]* language for simplicity, but I have a real problem where I need to match a more complex regex and replace it with an incrementing integer. Thanks so much! Bryant -- http://mail.python.org/mailman/listinfo/python-list