-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
29 lines (21 loc) · 1.27 KB
/
Copy pathhandler.py
File metadata and controls
29 lines (21 loc) · 1.27 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 Any, Dict, Optional, Callable
class DataHandler:
"""Utility class for processing dictionaries with functional hooks."""
def __init__(self, settings: Optional[Dict[str, Any]] = None) -> None:
"""Initialize handler with optional settings mapping."""
self.settings = settings or {}
def execute(self, payload: Dict[str, Any], callback: Optional[Callable[[Any], Any]] = None) -> Dict[str, Any]:
"""Apply processing logic to payload and optional callback transformation."""
processed_data: Dict[str, Any] = payload.copy()
# Apply core transformations based on settings
if self.settings.get("strip_whitespace", False):
processed_data = {k: str(v).strip() for k, v in processed_data.items()}
if callback:
return {k: callback(v) for k, v in processed_data.items()}
return processed_data
def update_settings(self, key: str, value: Any) -> None:
"""Update specific internal configuration key-value pairs."""
self.settings[key] = value
def create_handler(config: Optional[Dict[str, Any]] = None) -> DataHandler:
"""Factory function for creating pre-configured DataHandler instances."""
return DataHandler(settings=config)