473,396 Members | 2,090 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,396 software developers and data experts.

filtering keyword arguments

How do you filter keyword arguments before passing them to a function?

For example:

def f(x=1): return x

def g(a, **kwargs): print a, f(**kwargs)

In [5]: g(1, x=3)
1 3

In [6]: g(1, x=3, y=4)
TypeError: f() got an unexpected keyword argument 'y'

Is there a way to do something like:

def g(a, **kwargs): print a, f(filter_rules(f, **kwargs))

so only {'x': 3} is passed to f?

I was hoping for a pythonic way of doing what in Mathematica is done
by FilterRules:

http://reference.wolfram.com/mathema...lterRules.html

Jul 13 '08 #1
3 1417
On Jul 12, 10:44*pm, Amir <amirn...@gmail.comwrote:
How do you filter keyword arguments before passing them to a function?

For example:

def f(x=1): return x

def g(a, **kwargs): print a, f(**kwargs)

In [5]: g(1, x=3)
1 3

In [6]: g(1, x=3, y=4)
TypeError: f() got an unexpected keyword argument 'y'

Is there a way to do something like:

def g(a, **kwargs): print a, f(filter_rules(f, **kwargs))

so only {'x': 3} is passed to f?

I was hoping for a pythonic way of doing what in Mathematica is done
by FilterRules:

http://reference.wolfram.com/mathema...lterRules.html
Sure, you could do:

def call_with_relevant_args(func, kwargs):
kwargs = dict([(k, v) for k, v in kwargs.iteritems() if k in
func.func_code.co_varnames])
return func(**kwargs)
Jul 13 '08 #2
Amir wrote:
How do you filter keyword arguments before passing them to a function?

For example:

def f(x=1): return x

def g(a, **kwargs): print a, f(**kwargs)

In [5]: g(1, x=3)
1 3

In [6]: g(1, x=3, y=4)
TypeError: f() got an unexpected keyword argument 'y'

Is there a way to do something like:

def g(a, **kwargs): print a, f(filter_rules(f, **kwargs))

so only {'x': 3} is passed to f?
I think you have to come up with something yourself. Here's a start:

import inspect

def filter_kw(f, **kw):
names = inspect.getargspec(f)[0]
return dict((n, kw[n]) for n in names if n in kw)

Peter
Jul 13 '08 #3
On Jul 12, 8:44 pm, Amir <amirn...@gmail.comwrote:
How do you filter keyword arguments before passing them to a function?

For example:

def f(x=1): return x

def g(a, **kwargs): print a, f(**kwargs)

In [5]: g(1, x=3)
1 3

In [6]: g(1, x=3, y=4)
TypeError: f() got an unexpected keyword argument 'y'

Is there a way to do something like:

def g(a, **kwargs): print a, f(filter_rules(f, **kwargs))

so only {'x': 3} is passed to f?

I was hoping for a pythonic way of doing what in Mathematica is done
by FilterRules:

http://reference.wolfram.com/mathema...lterRules.html
Here it is as a decorator:
def filter_arguments(func):
def func2(*args, **kwargs):
import inspect
arglist, vararg, kwarg, defaults = inspect.getargspec(func)
for k in kwargs.copy():
if k not in arglist: del kwargs[k]
return func(*args, **kwargs)
return func2
@filter_arguments
def func(a=1, b=2): return a+b
print func()
print func(c=3)
print func(a=3,b=4)
Jul 13 '08 #4

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

Similar topics

6
by: Roy Smith | last post by:
I've got a function that takes a couple of optional keyword arguments. I want to check to make sure I didn't get passed an argument I didn't expect. Right now I'm doing: conversion = None drop...
14
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def...
2
by: Fuzzyman | last post by:
Sorry if this is a duplicate - I use the google interface and sometiems it screws up (not showing stuff you've posted *or* not posting it). Before you ask it's because at work I have no NNTP and...
20
by: talin at acm dot org | last post by:
Although I realize the perils of even suggesting polluting the Python namespace with a new keyword, I often think that it would be useful to consider defining an operator for testing whether or not...
1
by: sp | last post by:
<files> <file id="1" type="txt"> <indicies index="A"> <keyword>key1</keyword> </indices> <indicies index="B"> <keyword>key2</keyword> </indices> <indicies index="A"> <keyword>key3</keyword>
19
by: Chantelle | last post by:
I've got this A2K DB that has a continuous form that lists Suppliers and their details. The form has a field for each supplier that holds several Keywords that reflect the suppliers products or...
10
by: Armando Serrano Lombillo | last post by:
Why does Python give an error when I try to do this: Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> len(object=) TypeError: len() takes no keyword arguments but...
3
by: Sean DiZazzo | last post by:
Why is the following not working? Is there any way to get keyword arguments working with exposed XMLRPC functions? ~~~~~~~~~~~~~~~~ server.py import SocketServer from SimpleXMLRPCServer import...
0
by: Heshan Suri | last post by:
Hi, I have written a the following python code to analyse a function or a method. I am having a script which is having a python function (add) and a class (MyClass) with a method (multiply).When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.