473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing a variable number of arguments to a wrapped function.

Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):

def makeplot(self, xvalues, yvalues, linecolor='', linewidth=''):
if linecolor and linewidth:
plot(xvalues, yvalues, linecolor, linewidth=linewidth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linewidth)
else:
plot(xvalues, yvalues)

Stephen

Aug 5 '05 #1
5 1310
st*****@theboulets.net wrote:
Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):


Usually, you would just make the defaults for linecolor and linewidth
the same as the defaults for the underlying function. matplotlib.pylab
functions, however, have too much intelligence coded in them for this to
be easy.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 5 '05 #2
On 5 Aug 2005 08:34:32 -0700
st*****@theboulets.net wrote:
Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):

def makeplot(self, xvalues, yvalues, linecolor='', linewidth=''):
if linecolor and linewidth:
plot(xvalues, yvalues, linecolor, linewidth=linewidth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linewidth)
else:
plot(xvalues, yvalues)


What's wrong with:

def makeplot(self, xvalues, yvalues, **kwargs):
plot(xvalues, yvalues, **kwargs)

or even:

def makeplot(self, *a, **ka):
plot(*a, **ka)

?

--
jk
Aug 5 '05 #3
>>>>> "stephen" == stephen <st*****@theboulets.net> writes:

stephen> Is there a better way of doing this so that I don't have
stephen> to go through every permutation of possible arguments
stephen> (the example here from the matplotlib 'plot' function):

You can make linecolor=None and linewidth=None, and then use
matplotlib's rc defaults

from matplotlib import rcParams
def makeplot(self, xvalues, yvalues, linecolor=None, linewidth=None):
if linecolor is None: linecolor = rcParams['lines.color']
if linewidth is None: linewidth = rcParams['lines.linewidth']
plot(xvalues, yvalues, color=linecolor, linewidth=linewidth)

Then you can customize the defaults in the rc file
(http://matplotlib.sf.net/matplotlibrc) any way you want.

Alternatively, you can also set the defaults in your script

from matplotlib import rc
rc('lines', linewidth=1.0, color='red')

JDH
Aug 5 '05 #4
On 5 Aug 2005 08:34:32 -0700, st*****@theboulets.net wrote:
Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):

def makeplot(self, xvalues, yvalues, linecolor='', linewidth=''):
if linecolor and linewidth:
plot(xvalues, yvalues, linecolor, linewidth=linewidth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linewidth)
else:
plot(xvalues, yvalues)


It seems like you are not changing the order or values of arguments,
so I'm wondering if this would work for you:

def makeplot(self, *args, **kwargs):
plot(*args, **kwargs)

and if not, why not.

Regards,
Bengt Richter
Aug 5 '05 #5
Using 'plot(*args, **kwargs)' does seem to work just fine.

Thanks to all for their suggestions.

Stephen

Aug 5 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
7
by: Robert Lario | last post by:
For examples sake I have made up a very simple example. I have an object called foo1 which is of type foo. I want to be able to call a funtion called myfunc as follows: myfunc(ref foo1) ...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
2
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this...
9
by: oldyork90 | last post by:
I'm going thru code and have never seen this before http://www.webreference.com/programming/javascript/mk/column2/3.html Look at function CreateDragContainer() on line 25. It has no arguments...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.