-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
50 lines (40 loc) · 1.69 KB
/
Copy pathprocessor.py
File metadata and controls
50 lines (40 loc) · 1.69 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import inspect
from typing import Any, Callable, Generator, Generic, Iterable, TypeVar, Union
T = TypeVar("T")
R = TypeVar("R")
class PipeProcessor(Generic[T]):
"""An unconventional stream processor that allows piping with the OR operator.
Utilizes generator-based pipeline execution and automatic argument unpacking
via function signature introspection.
"""
def __init__(self, iterable: Iterable[T]) -> None:
self.stream: Iterable[T] = iterable
def __or__(self, func: Callable[..., R]) -> "PipeProcessor[R]":
"""Pipes the current stream elements through the provided function.
Allows seamless cascading using the bitwise OR operator.
"""
return PipeProcessor(self._apply(func))
def _apply(self, func: Callable[..., R]) -> Generator[R, None, None]:
"""Generator applying the callable, automatically unpacking iterables if needed."""
try:
sig = inspect.signature(func)
req_params = sum(
1
for p in sig.parameters.values()
if p.default == inspect.Parameter.empty
and p.kind
not in (
inspect.Parameter.VAR_POSITIONAL,
inspect.Parameter.VAR_KEYWORD,
)
)
except (ValueError, TypeError):
req_params = 1
for item in self.stream:
if req_params > 1 and isinstance(item, (tuple, list)):
yield func(*item) # type: ignore
else:
yield func(item)
def consume(self) -> list[T]:
"""Consumes the underlying iterator and returns all elements as a list."""
return list(self.stream)