-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
34 lines (30 loc) · 1.37 KB
/
Copy pathexceptions.py
File metadata and controls
34 lines (30 loc) · 1.37 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
from typing import Callable, Any, Dict, Type
class DataAnomaly(Exception):
"""Exception wrapper for corrupted or unexpected data structures."""
def __init__(self, message: str, payload: Any = None):
super().__init__(message)
self.payload = payload
class ExceptionHealer:
"""
Unusual control flow manager that uses exceptions to trigger
adaptive data cleaning and recovery actions dynamically.
"""
def __init__(self) -> None:
self.strategies: Dict[Type[BaseException], Callable[[Any], Any]] = {}
def register(self, exception_cls: Type[BaseException], recovery_fn: Callable[[Any], Any]) -> None:
"""Binds an exception type to a specific data-recovery function."""
self.strategies[exception_cls] = recovery_fn
def process(self, data: Any, handler: Callable[[Any], Any]) -> Any:
"""
Attempts to process data. If a registered exception occurs,
applies the recovery strategy and re-runs or returns healed results.
"""
try:
return handler(data)
except BaseException as exc:
for exc_type, recovery in self.strategies.items():
if isinstance(exc, exc_type):
payload = getattr(exc, 'payload', data)
healed_data = recovery(payload)
return handler(healed_data)
raise exc