469,352 Members | 1,677 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

interface boilerplate


In matplotlib, a plotting library with an OO API and a matlab-like
procedural interface, I have a lot of functions defined in the matlab
interface module that wrap similarly named class methods defined in
the Axes class of the axes module. Eg, the Axes class defines a plot
method and the matlab interface defines a plot function that gets the
current Axes instance, calls the plot method on that instance, and
does some error handling. Here is the matlab interface wrapper (gca()
returns the current Axes instance)

def plot(*args, **kwargs):
try:
ret = gca().plot(*args, **kwargs)
except ValueError, msg:
msg = raise_msg_to_str(msg)
error_msg(msg)
else:
draw_if_interactive()
return ret
plot.__doc__ = Axes.plot.__doc__

This is mostly boilerplate code that a lot of matlab interface
functions use, and I'd like to automatically generate it.

This appears to (mostly) work

def _wrap_axfunc(name):
def wrapper(*args, **kwargs):
try:
func = getattr(gca(), name)
ret = func(*args, **kwargs)
except ValueError, msg:
msg = raise_msg_to_str(msg)
error_msg(msg)
else:
draw_if_interactive()
return ret
wrapper.__doc__ = getattr(Axes, name).__doc__
#wrapper.__name__ = name
return wrapper

plot = _wrap_axfunc('plot')

The only problem I've seen so far is that the name of the function in
pydoc string is 'wrapper', and I want it to appear as "plot". I tried
setting the __name__ attribute, but it is read only.

Any suggestions on how to best define these matlab interface functions
by automatically wrapping the Axes instance functions? I'd like to
support python2.2 so python2.2 compliant solutions especially welcome.

JDH
Jul 18 '05 #1
4 1386
John Hunter <jd******@ace.bsd.uchicago.edu> wrote:
...
The only problem I've seen so far is that the name of the function in
pydoc string is 'wrapper', and I want it to appear as "plot". I tried
setting the __name__ attribute, but it is read only.
Yes, a 2.3 problem, solved in 2.4.
Any suggestions on how to best define these matlab interface functions
by automatically wrapping the Axes instance functions? I'd like to
support python2.2 so python2.2 compliant solutions especially welcome.


Then I guess upgrading to 2.4 is out of the question.

To make a function just like another but with a different name:

def changed_name_function(f, newname):
import new
return new.function(f.func_code, f.func_globals, newname,
f.func_defaults, f.func_closure)

I believe this should work in 2.2 as well (not tested).
Alex

Jul 18 '05 #2
>>>>> "Alex" == Alex Martelli <al*****@yahoo.com> writes:

Alex> To make a function just like another but with a different
Alex> name:

Alex> def changed_name_function(f, newname): import new return
Alex> new.function(f.func_code, f.func_globals, newname,
Alex> f.func_defaults, f.func_closure)

Alex> I believe this should work in 2.2 as well (not tested).

I tested this - the signature of new.function in 2.2 is a bit
different

function(...)
Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).

so it doesn't take the 5 arg version posted.

I am having a little trouble figuring out how to handle the call
signature for 2.2. I tried this modification (matplotlib._python23 is
a flag that returns True iff python version >=2.3
def changed_name_function(f, newname):
import new
if matplotlib._python23:
newf = new.function(f.func_code, f.func_globals, newname,
f.func_defaults, f.func_closure)
else:
if f.func_defaults is None:
argdefs = ()
else:
argdefs = f.func_defaults
newf = new.function(f.func_code, f.func_globals, newname,
argdefs)

newf.__doc__ = f.__doc__
return newf

I added the None check on f.func_defaults because I was getting the
error

TypeError: function() argument 4 must be tuple, not None

But this does not appear to be right either because I get a segfault
:-( Note that the suggestion works as advertised for python2.3.

Any ideas?

Thanks,
John Hunter
Jul 18 '05 #3
John Hunter <jd******@ace.bsd.uchicago.edu> wrote:
>> "Alex" == Alex Martelli <al*****@yahoo.com> writes:

Alex> To make a function just like another but with a different
Alex> name:

Alex> def changed_name_function(f, newname): import new return
Alex> new.function(f.func_code, f.func_globals, newname,
Alex> f.func_defaults, f.func_closure)

Alex> I believe this should work in 2.2 as well (not tested).

I tested this - the signature of new.function in 2.2 is a bit
different

function(...)
Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).

so it doesn't take the 5 arg version posted.


Ah, it didn't take a closure. Could be quite a problem...
I am having a little trouble figuring out how to handle the call
signature for 2.2. I tried this modification (matplotlib._python23 is
a flag that returns True iff python version >=2.3
def changed_name_function(f, newname):
import new
if matplotlib._python23:
newf = new.function(f.func_code, f.func_globals, newname,
f.func_defaults, f.func_closure)
else:
if f.func_defaults is None:
argdefs = ()
else:
argdefs = f.func_defaults
newf = new.function(f.func_code, f.func_globals, newname,
argdefs)

newf.__doc__ = f.__doc__
return newf

I added the None check on f.func_defaults because I was getting the
error

TypeError: function() argument 4 must be tuple, not None

But this does not appear to be right either because I get a segfault
:-( Note that the suggestion works as advertised for python2.3.

Any ideas?


Supporting old versions is never going to be easy -- I don't even have a
2.2 installation around to do such tests, any more. Perhaps for
versions < 2.3 you could simply degrade gracefully to perform no
renaming (and for versions >= 2.4 do the renaming the right way, by
assigning to f.func_name and returning f)... those who choose to stick
with 2.2 will just have to account that as one of the many limitations
and slow-downs their choice buys them...
Alex
Jul 18 '05 #4
>>>>> "Alex" == Alex Martelli <al*****@yahoo.com> writes:

Alex> Supporting old versions is never going to be easy -- I don't
Alex> even have a 2.2 installation around to do such tests, any
Alex> more. Perhaps for versions < 2.3 you could simply degrade
Alex> gracefully to perform no renaming (and for versions >= 2.4
Alex> do the renaming the right way, by assigning to f.func_name
Alex> and returning f)... those who choose to stick with 2.2 will
Alex> just have to account that as one of the many limitations and
Alex> slow-downs their choice buys them...

It is a pain -- for a fair number of linux boxes, though, 2.2 is still
the default. Perhaps a better alternative for 2.2 is to simply fall back on
exec

__fmt = """\
def %(name)s(*args, **kwargs):
try:
ret = gca().%(name)s(*args, **kwargs)
except ValueError, msg:
msg = raise_msg_to_str(msg)
error_msg(msg)
else:
draw_if_interactive()
return ret
%(name)s.__doc__ = Axes.%(name)s.__doc__
"""

for name in _methods:
exec(__fmt%{'name':name})
JDH
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Ralf W. Grosse-Kunstleve | last post: by
4 posts views Thread by Roy Pereira | last post: by
3 posts views Thread by zlst | last post: by
reply views Thread by YellowFin Announcements | last post: by
11 posts views Thread by Steven D'Aprano | last post: by
3 posts views Thread by n.torrey.pines | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.