forked from donnemartin/data-science-ipython-notebooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_util.py
More file actions
24 lines (21 loc) · 680 Bytes
/
type_util.py
File metadata and controls
24 lines (21 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class TypeUtil:
@classmethod
def is_iterable(cls, obj):
"""Determines if obj is iterable.
Useful when writing functions that can accept multiple types of
input (list, tuple, ndarray, iterator). Pairs well with
convert_to_list.
"""
try:
iter(obj)
return True
except TypeError:
return False
@classmethod
def convert_to_list(cls, obj):
"""Converts obj to a list if it is not a list and it is iterable,
else returns the original obj.
"""
if not isinstance(obj, list) and cls.is_iterable(obj):
obj = list(obj)
return obj