473,785 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=linew idth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linew idth)
else:
plot(xvalues, yvalues)

Stephen

Aug 5 '05 #1
5 1330
st*****@theboul ets.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.pyla b
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*****@theboul ets.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=linew idth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linew idth)
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*****@thebou lets.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.linewidt h']
plot(xvalues, yvalues, color=linecolor , linewidth=linew idth)

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*****@theboul ets.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=linew idth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linew idth)
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
34378
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
3
14952
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
39
7664
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
17
3604
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
7
5472
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) here's the function : public void myfunc(ref object foo)
6
3954
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 have some public functions and private variables. Inside the class I also have a declaration of a new form object. One of the functions of the class takes that form object, shows it with showdialog and the basically passes the control to the form...
1
3269
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 irrelavent. My syntax is surely where I am going wrong. I'd like to be able to call this routine to read different values from another device. This routine would be called quite simply as follows: void main() {
2
3332
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 simple example, def fun1(**kwargs): print kwargs.keys() def fun2(**kwargs):
9
5285
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 defined and depends on a function property named arguments to process its input. I poked around and found this is deprecated. How do you pass an unknown number of arguments to a function? Put them in an array?
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.