-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
33 lines (29 loc) · 885 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Utility functions."""
from typing import List, Any
def flatten(items: Any
) -> List[Any]:
"""Flatten anything into a 1-D list."""
if hasattr(items, '__iter__') and not isinstance(items, str):
ret = []
for item in items:
ret += flatten(item)
else:
ret = [items]
return ret
def list_len(items: Any,
) -> List[int]:
"""Get length of elements in first dimension."""
if hasattr(items, '__iter__') and not isinstance(items, str):
ret = []
is_nested_list = False
for item in items:
if hasattr(items, '__iter__') and not isinstance(item, str):
ret += [len(item)]
is_nested_list = True
else:
ret += [1]
if not is_nested_list:
ret = [sum(ret)]
else:
ret = [1]
return ret