-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
29 lines (23 loc) · 1.17 KB
/
Copy pathhandler.py
File metadata and controls
29 lines (23 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
from typing import Callable, Any, Dict, Optional
import functools
class DataHandler:
"""An unconventional handler that treats data as callable flow."""
def __init__(self, processors: Optional[Dict[str, Callable[[Any], Any]]] = None) -> None:
self._processors: Dict[str, Callable[[Any], Any]] = processors or {}
def __call__(self, key: str, value: Any) -> Any:
"""Process data using a registered pipeline stage."""
processor = self._processors.get(key, lambda x: x)
return processor(value)
def register(self, key: str) -> Callable[[Callable[[Any], Any]], Callable[[Any], Any]]:
"""Decorator registration for custom pipeline logic."""
def decorator(func: Callable[[Any], Any]) -> Callable[[Any], Any]:
self._processors[key] = func
return func
return decorator
def pipeline(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Apply processing pipeline to entire data set."""
return {k: self(k, v) for k, v in data.items()}
@functools.lru_cache(maxsize=32)
def get_default_handler() -> DataHandler:
"""Factory for standardized data handling instances."""
return DataHandler()