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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
from typing import cast, overload, Optional, List, Set, Any, TypeVar
from typing import Generator, Sequence, Iterable
T = TypeVar("T")
def is_sequence(obj: Any) -> bool:
return isinstance(obj, Sequence) and not isinstance(obj, (str, bytes))
def is_listish(obj: Any) -> bool:
"""Check if something quacks like a list."""
if isinstance(obj, (list, tuple, set)):
return True
return is_sequence(obj)
def unique_list(lst: Sequence[T]) -> List[T]:
"""Make a list unique, retaining order of initial appearance."""
uniq = []
for item in lst:
if item not in uniq:
uniq.append(item)
return uniq
@overload
def ensure_list(obj: None) -> List[None]:
pass
@overload
def ensure_list(obj: Iterable[T]) -> List[T]:
pass
@overload
def ensure_list(obj: T) -> List[T]:
pass
def ensure_list(obj: Any) -> List[T]:
"""Make the returned object a list, otherwise wrap as single item."""
if obj is None:
return []
if is_listish(obj):
return [o for o in cast(Sequence[T], obj)]
return [cast(T, obj)]
def chunked_iter(
iterable: Iterable[T], batch_size: int = 500
) -> Generator[List[T], None, None]:
"""Pick `batch_size` items from an iterable and treat them as a batch list."""
batch = list()
for item in iterable:
batch.append(item)
if len(batch) >= batch_size:
yield batch
batch = list()
if len(batch) > 0:
yield batch
def chunked_iter_sets(
iterable: Iterable[T], batch_size: int = 500
) -> Generator[Set[T], None, None]:
"""Pick `batch_size` items from an iterable and treat them as a batch set."""
batch = set()
for item in iterable:
batch.add(item)
if len(batch) >= batch_size:
yield batch
batch = set()
if len(batch) > 0:
yield batch
def first(lst: Sequence[T]) -> Optional[T]:
"""Return the first non-null element in the list, or None."""
item: T
for item in ensure_list(lst):
if item is not None:
return item
return None
|