Repository navigation
Expand file tree
/
Copy pathprocessor.py
More file actions
55 lines (43 loc) · 2.12 KB
/
Copy pathprocessor.py
File metadata and controls
55 lines (43 loc) · 2.12 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
51
52
53
54
55
import logging
from typing import Any, Dict, List, Optional
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DataProcessor:
"""Processes data batches with input validation in the main loop."""
def __init__(self, required_keys: Optional[List[str]] = None) -> None:
self.required_keys = required_keys or ["id", "payload", "timestamp"]
self.processed_count = 0
self.failed_count = 0
def validate_item(self, item: Any) -> Dict[str, Any]:
"""Validate the structure and types of a single item."""
if not isinstance(item, dict):
raise ValueError(f"Expected dict, got {type(item).__name__}")
missing = [k for k in self.required_keys if k not in item]
if missing:
raise ValueError(f"Missing required keys: {', '.join(missing)}")
if not isinstance(item["id"], (int, str)) or not str(item["id"]).strip():
raise ValueError("Key 'id' must be a non-empty string or integer")
if not isinstance(item["timestamp"], (int, float)) or item["timestamp"] <= 0:
raise ValueError("Key 'timestamp' must be a positive number")
return item
def process_batch(self, items: List[Any]) -> List[Dict[str, Any]]:
"""Main loop validating and converting input payload records."""
results = []
for index, item in enumerate(items):
try:
valid_item = self.validate_item(item)
processed = {
"id": str(valid_item["id"]),
"payload": str(valid_item["payload"]).strip(),
"processed_at": valid_item["timestamp"],
"status": "completed",
}
results.append(processed)
self.processed_count += 1
except ValueError as err:
logger.warning("Skipping invalid item at index %d: %s", index, err)
self.failed_count += 1
except Exception as err:
logger.error("Unexpected error at index %d: %s", index, err)
self.failed_count += 1
return results