Simplify `if 'foo' in kwargs and kwargs['foo'] is True:` - python

Is it possible to simplify the boolean check of a kwargs option?
For example in foo I have to check lots of options:
def foo(*args, **kwargs):
if 'foo' in kwargs and kwargs['foo'] is True:
do_something()
if 'bar' in kwargs and kwargs['bar'] is True:
do_something_else()
...
One possible workaroud is to hide some complexity by adding more complexity...
def parse_kwargs(kwords, **kwargs):
keywords = {}
for kw in kwords:
keywords[kw] = True if kw in kwargs and kwargs['kw'] is True else False
return keywords
Then in my main function:
def foo(*args, **kwargs):
kw = parse_kwargs(**kwargs)
if kw['foo']:
do_something()
if kw['bar']:
do_something_else()
...
I would like to know if I can use a simpler method less boilerplate...

dict.get is useful to avoid the KeyError when accessing a non-existent key:
if kwargs.get('foo'):
Or
if kwargs.get('foo', False):

How about this?
def foo(*args, **kwargs):
keywords = {'foo': do_foo_something,
'bar': do_bar_something,
'frug': do_frug_someting,
...}
for k in keywords:
if kwargs.get(k, False):
keywords[k]()
def do_foo_something():
do stuff
def do_bar_something():
do stuff
def do_frug_something():
do stuff

The way to check for a value that might not be set is with get(), which returns None on missing keys instead of raising an error. But you should also change the is True part:
A boolean test comes out true if you just check the value itself; so it's simpler (and proper python style) to write
if kwargs.get("foo"):
...
is True is not only redundant, but incorrect: is does not check for value equality, but for identity. Any non-zero value counts as true in python, but e.g. 1 is True comes out as false! 1 == True checks for "truthiness", and is what you should have used (if anything). Even if this function will only receive True booleans, it's a bad idea to burden your code with needlessly strong assumptions about what it will see.

Related

Making kwarg not be passed to function if it is None

In my python, I am writing a function that takes **kwargs. I am running into the issue where I am often passing None as a keyword value, and this feels like bad style. For example:
def foo(**kwargs):
if 'bar' in kwargs:
return kwargs['bar']
def baz(x=None):
print(foo(bar=x))
The problem here is that if xis None, then I would like it just to print nothing. How do you make the keyword argument not be added to kwargsif it is None?
If None is not an acceptable value, test for that. Either don't pass on x in baz() if it is None, or explicitly test for None in foo. You can combine testing for the key and for the value not being None, by using dict.get():
def foo(**kwargs):
bar = kwargs.get('bar')
if bar is not None:
return bar
# return something else
or just use a conditional expression to return a default, like an empty string:
def foo(**kwargs):
bar = kwargs.get('bar')
return bar if bar is not None else ''
or return the default for any false-y value (None, empty list, 0, etc.):
def foo(**kwargs):
return kwargs.get('bar') or ''
As pointed out, you will need to test for None if it is not an acceptable value. But, keep in mind that a function always returns. If no return statement is executed... None is returned.
So you actually need to check if the output is None if you want to avoid printing in that case.
def baz(x=None)
out = foo(bar=x)
if out is not None:
print(out)
Alternatively you can make sure foo always explicitly returns, which might be better for reusability, but will make baz print something_else instead of not printing at all.
def foo(**kwargs):
if 'bar' in kwargs:
return kwargs['bar']
else:
return something_else

Rewriting Python switch into a more compact way [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replacements for switch statement in python?
Suppose I have a list in Python:
list = ('ADD', 'SUB', 'PUSH', 'POP')
I want to run a function depending on input, and that input can be any value in the list.
Instead of writing a switch case statement for each element in list, is there a more compact way of writing it?
My reasoning is for the case of the list growing in the future.
Well, there is no switch/case statement in Python.
For a small list, you want to use if/elif:
def do_stuff(x, *args):
if x == 'ADD':
return do_add(*args)
elif x == 'SUB':
return do_sub(*args)
# …
else:
raise RuntimeError('Never heard of {}'.format(x))
For a larger list, you want to make sure each case is a function (I already assumed that above, but if you had code like return args[0] + args[1], you'd have to change that into a do_add function), and create a dict mapping names to functions:
func_map = {'ADD': do_add, 'SUB': do_sub, … }
def do_stuff(x, *args):
try:
return func_map[x](*args)
except KeyError:
raise RuntimeError('Never heard of {}'.format(x))
This works because in Python, functions are normal objects that you can pass around like any other objects. So, you can store them in a dict, retrieve them from the dict, and still call them.
By the way, this is all explained in the FAQ, along with a bit of extra fanciness.
If you have some default function you'd like to call instead of raising an error, it's obvious how to do that with the if/elif/else chain, but how do you do it with the dict map? You could do it by putting the default function into the except block, but there's an easier way: just use the dict.get method:
def do_stuff(x, *args):
return func_map.get(x, do_default)(*args)
You could also use a pattern such as this (in a hurry so can't clean it up atm):
>>> class Test(object):
... def test_FOO(self):
... print 'foo'
...
... def test_BAR(self):
... print 'bar'
...
>>> def run_on(cls, name):
... getattr(cls, 'test_%s' % name)()
...
>>> run_on(Test(), 'FOO')
foo

inserting into python dictionary

The default behavior for python dictionary is to create a new key in the dictionary if that key does not already exist. For example:
d = {}
d['did not exist before'] = 'now it does'
this is all well and good for most purposes, but what if I'd like python to do nothing if the key isn't already in the dictionary. In my situation:
for x in exceptions:
if masterlist.has_key(x):
masterlist[x] = False
in other words, i don't want some incorrect elements in exceptions to corrupt my masterlist. Is this as simple as it gets? it FEELS like I should be able to do this in one line inside the for loop (i.e., without explicitly checking that x is a key of masterlist)
UPDATE:
To me, my question is asking about the lack of a parallel between a list and a dict. For example:
l = []
l[0] = 2 #fails
l.append(2) #works
with the subclassing answer, you could modify the dictionary (maybe "safe_dict" or "explicit_dict" to do something similar:
d = {}
d['a'] = '1' #would fail in my world
d.insert('a','1') #what my world is missing
You could use .update:
masterlist.update((x, False) for x in exceptions if masterlist.has_key(x))
You can inherit a dict class, override it's __setitem__ to check for existance of key (or do the same with monkey-patching only one instance).
Sample class:
class a(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
dict.__setitem__(self, 'a', 'b')
def __setitem__(self, key, value):
if self.has_key(key):
dict.__setitem__(self, key, value)
a = a()
print a['a'] # prints 'b'
a['c'] = 'd'
# print a['c'] - would fail
a['a'] = 'e'
print a['a'] # prints 'e'
You could also use some function to make setting values without checking for existence simpler.
However, I though it would be shorter... Don't use it unless you need it in many places.
You can also use in instead of has_key, which is a little nicer.
for x in exceptions:
if x in masterlist:
masterlist[x] = False
But I don't see the issue with having an if statement for this purpose.
For long lists try to use the & operator with set() function embraced with ():
for x in (set(exceptions) & set(masterlist)):
masterlist[x] = False
#or masterlist[x] = exceptions[x]
It'll improve the reading and the iterations at the same time by reading the masterlist's keys only once.

Preserving argument default values while method chaining

If I have to wrap an existing method, let us say wrapee() from a new method, say wrapper(), and the wrapee() provides default values for some arguments, how do I preserve its semantics without introducing unnecessary dependencies and maintenance? Let us say, the goal is to be able to use wrapper() in place of wrapee() without having to change the client code. E.g., if wrapee() is defined as:
def wrapee(param1, param2="Some Value"):
# Do something
Then, one way to define wrapper() is:
def wrapper(param1, param2="Some Value"):
# Do something
wrapee(param1, param2)
# Do something else.
However, wrapper() has to make assumptions on the default value for param2 which I don't like. If I have the control on wrapee(), I would define it like this:
def wrapee(param1, param2=None):
param2 = param2 or "Some Value"
# Do something
Then, wrapper() would change to:
def wrapper(param1, param2=None):
# Do something
wrapee(param1, param2)
# Do something else.
If I don't have control on how wrapee() is defined, how best to define wrapper()? One option that comes into mind is to use to create a dict with non-None arguments and pass it as dictionary arguments, but it seems unnecessarily tedious.
Update:
The solution is to use both the list and dictionary arguments like this:
def wrapper(param1, *args, **argv):
# Do something
wrapee(param1, *args, **argv)
# Do something else.
All the following calls are then valid:
wrapper('test1')
wrapper('test1', 'test2')
wrapper('test1', param2='test2')
wrapper(param2='test2', param1='test1')
Check out argument lists in the Python docs.
>>> def wrapper(param1, *stuff, **kargs):
... print(param1)
... print(stuff)
... print(args)
...
>>> wrapper(3, 4, 5, foo=2)
3
(4, 5)
{'foo': 2}
Then to pass the args along:
wrapee(param1, *stuff, **kargs)
The *stuff is a variable number of non-named arguments, and the **kargs is a variable number of named arguments.
I'd hardly say that it isn't tedious, but the only approach that I can think of is to introspect the function that you are wrapping to determine if any of its parameters have default values. You can get the list of parameters and then determine which one is the first that has default values:
from inspect import getargspec
method_signature = getargspec(method)
param_names = method_signature[0]
default_values = method_signature[3]
params = []
# If any of method's parameters has default values, we need
# to know the index of the first one that does.
param_with_default_loc = -1
if default_values is not None and len(default_values) > 0:
param_slice_index = len(default_values) * -1
param_with_default = param_names[param_slice_index:][0]
param_with_default_loc = param_names.index(param_with_default)
At that point, you can iterate over param_names, copying into the dict that is passed to wrappee. Once your index >= param_with_default_loc, you can obtain the default values by looking in the default_values list with an index of your index - param_with_default_loc.
Does that make any sesne?
Of course, to make this generic, you would to define it as a wrapper function, adding yet another layer of wrapping.
def wrapper(param1, param2=None):
if param2:
wrapee(param1, param2)
else:
wrapee(param1)
is this what you want?
#!/usr/bin/python
from functools import wraps
def my_decorator(f):
#wraps(f)
def wrapper(*args, **kwds):
print 'Calling decorated function'
return f(*args, **kwds)
return wrapper
def f1(x, y):
print x, y
def f2(x, y="ok"):
print x, y
my_decorator(f1)(1,2)
my_decorator(f2)(1,2)
my_decorator(f2)(1)
adapted from http://koala/doc/python2.6-doc/html/library/functools.html#module-functools

Python - Passing a function into another function [duplicate]

This question already has answers here:
Python function as a function argument?
(10 answers)
Closed last month.
I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in Python?
Example
def Game(listA, listB, rules):
if rules == True:
do...
else:
do...
def Rule1(v):
if "variable_name1" in v:
return False
elif "variable_name2" in v:
return False
else:
return True
def Rule2(v):
if "variable_name3" and "variable_name4" in v:
return False
elif "variable_name4" and variable_name1 in v:
return False
else:
return True
This is just a pseudo code and therefore not specific but I get the code to compile but I need to know how to call the function Game and whether it's correctly defined since rules will be switched for either Rule1(v) or Rule2(v).
Just pass it in like any other parameter:
def a(x):
return "a(%s)" % (x,)
def b(f,x):
return f(x)
print b(a,10)
Treat function as variable in your program so you can just pass them to other functions easily:
def test ():
print "test was invoked"
def invoker(func):
func()
invoker(test) # prints test was invoked
For passing both a function, and any arguments to the function:
from typing import Callable
def looper(fn: Callable, n:int, *args, **kwargs):
"""
Call a function `n` times
Parameters
----------
fn: Callable
Function to be called.
n: int
Number of times to call `func`.
*args
Positional arguments to be passed to `func`.
**kwargs
Keyword arguments to be passed to `func`.
Example
-------
>>> def foo(a:Union[float, int], b:Union[float, int]):
... '''The function to pass'''
... print(a+b)
>>> looper(foo, 3, 2, b=4)
6
6
6
"""
for i in range(n):
fn(*args, **kwargs)
Depending on what you are doing, it could make sense to define a decorator, or perhaps use functools.partial.
Just pass it in, like this:
Game(list_a, list_b, Rule1)
and then your Game function could look something like this (still pseudocode):
def Game(listA, listB, rules=None):
if rules:
# do something useful
# ...
result = rules(variable) # this is how you can call your rule
else:
# do something useful without rules
A function name can become a variable name (and thus be passed as an argument) by dropping the parentheses. A variable name can become a function name by adding the parentheses.
In your example, equate the variable rules to one of your functions, leaving off the parentheses and the mention of the argument. Then in your game() function, invoke rules( v ) with the parentheses and the v parameter.
if puzzle == type1:
rules = Rule1
else:
rules = Rule2
def Game(listA, listB, rules):
if rules( v ) == True:
do...
else:
do...

Categories

Resources