Last active
March 21, 2019 16:44
-
-
Save tigerhawkvok/3f818cf282a9c0fe30186734d3ed9c7b to your computer and use it in GitHub Desktop.
Make things a list
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
######### | |
# 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