mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
20 lines
557 B
Python
20 lines
557 B
Python
class Util:
|
|
|
|
@classmethod
|
|
def is_iterable(self, obj):
|
|
try:
|
|
iter(obj)
|
|
return True
|
|
except TypeError:
|
|
return False
|
|
|
|
@classmethod
|
|
def convert_to_list(self, obj):
|
|
"""Useful when writing functions that can accept multiple types of
|
|
input (list, tuple, ndarray, iterator). Checks if the object is a list.
|
|
If it is not a list, converts it to a list.
|
|
"""
|
|
if not isinstance(obj, list) and self.is_iterable(obj):
|
|
obj = list(obj)
|
|
return obj
|