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 4 1499
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
>>>>> "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
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
>>>>> "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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ralf W. Grosse-Kunstleve |
last post by:
******************************************************************************
This posting is also available in HTML format:
http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
|
by: Roy Pereira |
last post by:
I have an application that is composed of a set
of "Content" dlls and a viewer application. The viewer
calls a standard set of functions that are present in all
the dlls. I maintain this by...
|
by: redog6 |
last post by:
When I save my XML file as data the resulting XML includes the
boilerplate text of my template. Is the approach to change my custom
schema to allow mixed content or is there a way to exclude the...
|
by: zlst |
last post by:
Many technological innovations rely upon User Interface Design to elevate
their technical complexity to a usable product. Technology alone may not win
user acceptance and subsequent marketability....
|
by: dwok |
last post by:
Does anyone know of any "boilerplate" template that can be utilized to
add/edit/delete information from a database? I am working on an
application that does a whole lot of adds, edits, and updates...
|
by: YellowFin Announcements |
last post by:
Introduction
Usability and relevance have been identified as the major factors
preventing mass adoption of
Business Intelligence applications. What we have today are traditional
BI tools that...
|
by: Steven D'Aprano |
last post by:
I'm writing a class that implements rich comparisons, and I find myself
writing a lot of very similar code. If the calculation is short and
simple, I do something like this:
class Parrot:
def...
|
by: Xah Lee |
last post by:
On Java's Interface
Xah Lee, 20050223
In Java the language, there's this a keyword “interface”.
In a functional language, a function can be specified by its name and
parameter specs....
|
by: n.torrey.pines |
last post by:
Hi
I work with a nested tree-like data structure template (that happens
to be a tuple of vectors of tuples of trees of something, with
different tuple elements having different types)
I need...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |