-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
33 lines (26 loc) · 960 Bytes
/
Copy pathprocessor.py
File metadata and controls
33 lines (26 loc) · 960 Bytes
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
import json
from datetime import datetime
class DataProcessor:
def __init__(self, data):
self.data = data
def to_json(self):
try:
return json.dumps(self.data)
except (TypeError, OverflowError) as e:
return f"Error converting to JSON: {e}"
def from_json(self, json_str):
try:
self.data = json.loads(json_str)
return self.data
except json.JSONDecodeError as e:
return f"Error decoding JSON: {e}"
def add_timestamp(self):
self.data['timestamp'] = datetime.now().isoformat()
def normalize_keys(self):
self.data = {k.lower().replace(' ', '_'): v for k, v in self.data.items()}
def get_summary(self):
return {k: v for k, v in self.data.items() if isinstance(v, (int, float))}
# Example usage:
# processor = DataProcessor({'Name': 'Example', 'Value': 42})
# processor.add_timestamp()
# print(processor.to_json())