Skip to content

Instantly share code, notes, and snippets.

@tigerhawkvok
Last active March 21, 2019 16:44
Show Gist options
  • Save tigerhawkvok/3f818cf282a9c0fe30186734d3ed9c7b to your computer and use it in GitHub Desktop.
Save tigerhawkvok/3f818cf282a9c0fe30186734d3ed9c7b to your computer and use it in GitHub Desktop.
Make things a list
#########
# Casting Helper
#########
def isSimpleIterable(thing) -> bool:
"""
Check if the thing is a basic iterable type
"""
return isinstance(thing, (list, set, tuple, frozenset))
def listify(thing, otherTypes:tuple= None) -> list:
"""
Cast the provided thing into a list
"""
if isinstance(otherTypes, tuple):
if not isSimpleIterable(thing) and isinstance(thing, otherTypes):
return [thing]
elif isSimpleIterable(thing):
return list(thing)
else:
raise TypeError("Invalid datatype")
else:
if otherTypes is not None:
raise ValueError("Invalid value for argument `otherTypes`")
if not isSimpleIterable(thing):
return [thing]
return list(thing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment