Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@PaulMcMillan
Created September 26, 2012 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulMcMillan/3790156 to your computer and use it in GitHub Desktop.
Save PaulMcMillan/3790156 to your computer and use it in GitHub Desktop.
iterit
def iterit(*args, **kwargs):
"""
This takes some input (int, string, list, iterable, whatever) and
makes sure it is an iterable, making it a single item list if not.
Importantly, it does rational things with strings.
You can pass it more than one item. Cast is optional.
def foo(offsets=10):
offsets = iterit(offsets, cast=int)
for f in offsets:
print "Value %s" % (10 + f)
>>> foo()
Value 20
>>> foo(3)
Value 13
>>> foo([1,2])
Value 11
Value 12
>>> foo('3')
Value 13
>>> foo(('3', 4))
Value 13
Value 14
Also useful this way:
foo, bar = iterit(foo, bar)
"""
if len(args) > 1:
return map(lambda arg: iterit(arg, **kwargs), args)
return map(kwargs.get('cast', None),
args[0] if hasattr(args[0], '__iter__') else [args[0],])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment