forked from codedog-ai/codedog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_complex.py
More file actions
190 lines (153 loc) · 5.91 KB
/
test_complex.py
File metadata and controls
190 lines (153 loc) · 5.91 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import sys
import json
import logging
from typing import Dict, List, Optional, Union, Any
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("complex_test.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class ComplexExample:
"""
A complex example class to demonstrate various Python features.
This class includes multiple methods, error handling, and documentation
to serve as a more complex test case for code evaluation.
"""
def __init__(self, name: str, config: Optional[Dict[str, Any]] = None):
"""
Initialize the ComplexExample class.
Args:
name: The name of this example
config: Optional configuration dictionary
"""
self.name = name
self.config = config or {}
self.data = []
logger.info(f"Initialized ComplexExample with name: {name}")
def process_data(self, input_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Process a list of data dictionaries.
Args:
input_data: List of dictionaries to process
Returns:
Processed data as a list of dictionaries
Raises:
ValueError: If input_data is not a list
"""
if not isinstance(input_data, list):
logger.error("Input data must be a list")
raise ValueError("Input data must be a list")
result = []
for item in input_data:
try:
processed_item = self._transform_item(item)
result.append(processed_item)
logger.debug(f"Processed item: {processed_item}")
except Exception as e:
logger.warning(f"Error processing item {item}: {str(e)}")
# Skip this item and continue with the next
continue
self.data.extend(result)
return result
def _transform_item(self, item: Dict[str, Any]) -> Dict[str, Any]:
"""
Internal method to transform a single data item.
Args:
item: Dictionary to transform
Returns:
Transformed dictionary
"""
if not isinstance(item, dict):
raise TypeError("Item must be a dictionary")
# Apply transformations based on config
result = item.copy()
# Apply custom transformations if specified in config
if "transformations" in self.config:
for field, transform in self.config["transformations"].items():
if field in result:
if transform == "uppercase" and isinstance(result[field], str):
result[field] = result[field].upper()
elif transform == "lowercase" and isinstance(result[field], str):
result[field] = result[field].lower()
elif transform == "double" and isinstance(result[field], (int, float)):
result[field] *= 2
# Add metadata
result["processed_by"] = self.name
result["timestamp"] = self._get_timestamp()
return result
def _get_timestamp(self) -> str:
"""Get current timestamp in ISO format."""
from datetime import datetime
return datetime.now().isoformat()
def save_results(self, filename: str) -> bool:
"""
Save processed data to a JSON file.
Args:
filename: Path to save the JSON file
Returns:
True if successful, False otherwise
"""
try:
with open(filename, 'w') as f:
json.dump(self.data, f, indent=2)
logger.info(f"Saved results to {filename}")
return True
except Exception as e:
logger.error(f"Error saving results to {filename}: {str(e)}")
return False
def load_from_file(self, filename: str) -> bool:
"""
Load data from a JSON file.
Args:
filename: Path to the JSON file
Returns:
True if successful, False otherwise
"""
try:
if not os.path.exists(filename):
logger.error(f"File not found: {filename}")
return False
with open(filename, 'r') as f:
data = json.load(f)
if not isinstance(data, list):
logger.error(f"Invalid data format in {filename}")
return False
self.data = data
logger.info(f"Loaded {len(data)} items from {filename}")
return True
except Exception as e:
logger.error(f"Error loading data from {filename}: {str(e)}")
return False
def main():
"""Main function to demonstrate the ComplexExample class."""
# Create an instance with configuration
config = {
"transformations": {
"name": "uppercase",
"value": "double"
}
}
processor = ComplexExample("TestProcessor", config)
# Sample data
sample_data = [
{"name": "item1", "value": 10, "category": "A"},
{"name": "item2", "value": 20, "category": "B"},
{"name": "item3", "value": 30, "category": "A"}
]
# Process the data
processed_data = processor.process_data(sample_data)
# Print results
print(f"Processed {len(processed_data)} items:")
for item in processed_data:
print(f" - {item['name']}: {item['value']} ({item['category']})")
# Save to file
processor.save_results("processed_data.json")
print("Processing complete!")
if __name__ == "__main__":
main()