Dynamically defined functions via exec in imported module
Hello all,
I'm trying to write a function that will dynamically generate other
functions via exec. I then want to be able to import the file (module)
containing this function and use it in other modules, but for some
reason it only works using the "import " syntax, and not "from
import *" syntax... i.e. in the latter case, the function is
dynamically generated, but not accessible from the importing module.
Any ideas on what I can do to be able to retain the second form of
import and still have the exec'd functions visible?
Here's the code... I have three files:
###
# modA.py
def dynamicdef(name, amt):
'''Dynamically defines a new function with the given name that
adds
the given amt to its argument and returns the result.'''
stm = "def %s(x):\n\treturn x + %d" % (name, amt)
print stm
# exec stm # --- with this, 'name' is only accessible within
this fn
exec stm in globals() # --- this makes it global within this
module...
print eval(name)
dynamicdef('plus5', 5)
print plus5(7)
###
# modB.py
# This uses the dynamicdef to dynamically define a new function, and
it
# works fine, with the newly defined function being accessible thru
the modA
# module...
import modA
modA.dynamicdef('plus10', 10)
print help(modA.plus5)
print help(modA.plus10)
print modA.plus5(20)
print modA.plus10(20)
###
# modC.py
# This uses the dynamicdef to dynamically define a new function, but
it
# doesn't work; seems like knowledge of the newly defined function in
the
# modA module is not propagated back to this context or something...?
from modA import *
dynamicdef('plus10', 10)
print help(plus5)
#print help(plus10)# !!! Fails: NameError: name 'plus10' is not
defined
print plus5(20)
print plus10(20)
###
Thanks for any help/suggestions,
---nadeem
--
http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically defined functions via exec in imported module
I understand the 99% rule... the example I gave was to simplify the
issue. The full thing I'm working on is a library for an introductory
CS class I'm teaching. I'm trying, essentially, to build a library of
macros for students to use allowing them to define records (like
structs in C) with selector functions. In particular, I'm trying to
replicate some of the Scheme stuff from the HtDP project in Python
(http://www.htdp.org/2003-09-26/Book/curriculum-Z-
H-9.html#node_sec_6.3). I want to provide a function, called
defineStruct that is called like this:
defineStruct('pos', 'x', 'y')
The effect of this function will be to dynamically define several new
functions for working with structures:
makePos(x, y)
posX(p)
posY(p)
isPos(p)
I understand that all this can be done with classes and OO
programming, but the whole point of the HtDP curriculum is to
introduce students to programming in a pedagogically-effective way
using a functional approach instead of OO-first. They do it in Scheme,
which is primarily a f.p. language, and I'm trying to replicate a
similar approach in Python. The defineStruct thing is basically meant
to be a macro that introduces a set of functions for whatever
structure definition is needed.
So, for these reasons, I don't believe the closure example above is
helpful. I don't want to have to tell students anything about
closures, and certainly have them worrying about functions returning
functions, and function pointers, etc. I'm trying to bundle all that
up behind the scenes.
So, thinking about my problem again, an alternate question may be: Is
it possible, in a function called in a module, to access and update
the global definitions (dictionary or whatever) in the caller module.
--- nadeem
--
http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically defined functions via exec in imported module
Well, I found one hack that seems to achieve this by accessing the globals() dictionary of the outermost stack frame and adding an entry to it for the newly created functions: import inspect def dynamicdef(name, amt): '''Dynamically defines a new function with the given name that adds the given amt to its argument and returns the result.''' stm = "def %s(x):\n\treturn x + %d" % (name, amt) print stm exec stm in globals() ## ADDED THIS TO EXPORT THE NEW FUNCTION NAME TO THE TOP LEVEL... inspect.stack()[-1][0].f_globals[name] = eval(name) I guess I'll go with this unless someone suggests an alternate. Thanks anyway, :) --- nadeem -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically defined functions via exec in imported module
That's a really neat way of doing it, thanks a lot! I hadn't realized how accessible all those globals() dictionaries were. Guess my example still falls in the 99%... :) --- nadeem > > def defineStruct(name, *parameters): > class _struct: > def __init__(self, *init_parameters): > for pname, pvalue in zip(parameters, init_parameters): > setattr(self, pname, pvalue) > globals()["make" + name] = _struct > for parameter in parameters: > def getter(o, parameter=parameter): > return getattr(o, parameter) > globals()[name + parameter] = getter > globals()["is" + name] = lambda o: isinstance(o, _struct) > > You might do other things, of course, like stepping up the frames from > sys._getframe() to inject the functions into the callers global scope. > There are some obvious optimizations you could make, too, as well as > other "methods" you might want to add. > > > > > I understand that all this can be done with classes and OO > > programming, but the whole point of the HtDP curriculum is to > > introduce students to programming in a pedagogically-effective way > > using a functional approach instead of OO-first. They do it in Scheme, > > which is primarily a f.p. language, and I'm trying to replicate a > > similar approach in Python. The defineStruct thing is basically meant > > to be a macro that introduces a set of functions for whatever > > structure definition is needed. > > > So, for these reasons, I don't believe the closure example above is > > helpful. I don't want to have to tell students anything about > > closures, and certainly have them worrying about functions returning > > functions, and function pointers, etc. I'm trying to bundle all that > > up behind the scenes. > > > So, thinking about my problem again, an alternate question may be: Is > > it possible, in a function called in a module, to access and update > > the global definitions (dictionary or whatever) in the caller module. > > > --- nadeem > > -- > >http://mail.python.org/mailman/listinfo/python-list > > -- > Read my blog! I depend on your acceptance of my opinion! I am > interesting!http://techblog.ironfroggy.com/ > Follow me if you're into that sort of thing:http://www.twitter.com/ironfroggy -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3.3 bz2 decompression testing results
Hi Pauli, Thank you for your interest in improving the bz2 module. However, I'm not sure of what you are saying in your email. If you believe you have found a bug in the module, then please provide clear instructions on how to reproduce the error(s), preferably using just one data file that triggers the problem. About Valgrind and Linux, I would be happy to fix up any memory leaks you find. However, bear in mind that running CPython under Valgrind produces quite a number of warnings even without doing anything with the bz2 module, so interpreting its output will be take a substantial amount of effort. Regards, Nadeem -- http://mail.python.org/mailman/listinfo/python-list
IDLE in Jython
Me and a couple of friends have been thinking of doing something involving Python for our final year undergrad project. We're considering the first idea mentioned on this page: http://wiki.python.org/moin/JythonProjects. Unfortunately, their statement is a little terse, so I was hoping someone could give us a better idea of what they wanted. As far as I can see, they want us to build an integrated development environment for Jython along the lines of Python's traditional IDLE. Have I gotten confused, or this really the gist of the idea? If I've got it right, I'd appreciate some help on the following issues: 1. The broad outlines of what we'll have to do - how much Java and how much Python? 2. Any Jython specific issues I need to be aware of? How big a part will Jython play in development? 3. Some idea of the scope of this project and the time it might take a team of three to get it up and running. We're looking for something fairly challenging, but not so difficult that we won't be able to finish it off in time. We have about 7-8 months to pull it off, but we'll have a bunch of pretty hectic exams, assignments, entrance tests, grad school apps and stuff like that for a pretty significant proportion of that time. The 'powers that be' are officially giving us one day a week for it, though I'm pretty certain we can increase that, and we'll have about a month of free time at the end of this semester, and less hectic schedules in the next. I'm pretty certain it's possible, but better heads than mine have been wrong...If anyone has any ideas for other interesting projects involving Python, we'd like to hear them too. At present I'm the only Python coder in the group, but all three of us are pretty good with Java, and we have a fair amount of experience doing GUIs in Swing. Hope you guys can help us out. Nadeem. -- http://mail.python.org/mailman/listinfo/python-list
