Last active
March 10, 2016 21:49
-
-
Save asalt/6330f790953fd4c144e9 to your computer and use it in GitHub Desktop.
Decorator to make a specific argument an iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
def make_iterable(argn=0, exceptions=[]): | |
"""Check to make sure an argument is an iterator. | |
Default argument is args[0] | |
Add exceptions that you wish to enclose in an iterator by providing an | |
iterator of their type. | |
Ironically, exceptions must be encapsulated in an iterator""" | |
def decorate(func): | |
@wraps(func) | |
def func_wrapper(*args, **kwargs): | |
"""Make a function iterable if it is not by putting it in a list. | |
Note also converts a string to a list with the string inside""" | |
maybe_iter = args[argn] | |
if not hasattr(maybe_iter, '__iter__') or any(isinstance(maybe_iter, x) for x in exceptions): | |
maybe_iter = [maybe_iter] | |
args = list(args) | |
args[argn] = maybe_iter | |
return func(*args, **kwargs) | |
return func_wrapper | |
return decorate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lets you do this, ensuring that each specified argument gets packaged into a list: