-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
36 lines (29 loc) · 1.17 KB
/
Copy pathprocessor.py
File metadata and controls
36 lines (29 loc) · 1.17 KB
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
from typing import Iterable, Generator, Any, Type, TypeVar, List
T = TypeVar('T')
def chunk_iterable(iterable: Iterable[T], chunk_size: int) -> Generator[List[T], None, None]:
"""Yield successive chunks of size chunk_size from the given iterable."""
if chunk_size <= 0:
raise ValueError("Chunk size must be greater than zero.")
chunk = []
for item in iterable:
chunk.append(item)
if len(chunk) == chunk_size:
yield chunk
chunk = []
if chunk:
yield chunk
def safe_cast(value: Any, to_type: Type[T], default: T) -> T:
"""Safely cast a value to a given type, returning the default if casting fails."""
try:
if value is None:
return default
return to_type(value)
except (ValueError, TypeError):
return default
def deep_flatten(nested_iterable: Iterable[Any]) -> Generator[Any, None, None]:
"""Flatten a nested iterable of arbitrary depth, ignoring strings as iterables."""
for item in nested_iterable:
if isinstance(item, Iterable) and not isinstance(item, (str, bytes)):
yield from deep_flatten(item)
else:
yield item