-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefractored_function.py
More file actions
63 lines (53 loc) · 2.4 KB
/
Copy pathrefractored_function.py
File metadata and controls
63 lines (53 loc) · 2.4 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
def export_customer_data(self, output_file: str, file_format: str = "csv") -> bool:
"""Export customer data in the specified format (csv or json)."""
if not self.customers:
logger.error("No customer data available to export")
return False
try:
if file_format == "csv":
# Determine fieldnames from the first valid customer record
first_record = next(
(v for v in self.customers.values() if isinstance(v, dict)), None
)
if first_record is None:
logger.error("No valid customer records to export")
return False
fieldnames = ["customer_id"] + list(first_record.keys())
with open(output_file, "w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for customer_id, data in self.customers.items():
if not isinstance(data, dict):
logger.warning(
"Skipping non-dict customer record for id %r: %r",
customer_id,
data,
)
continue
row = {"customer_id": customer_id, **data}
writer.writerow(row)
elif file_format == "json":
# Build a clean, serializable dict of only valid customer records
clean_customers = {}
for customer_id, data in self.customers.items():
if not isinstance(data, dict):
logger.warning(
"Skipping non-dict customer record for id %r: %r",
customer_id,
data,
)
continue
clean_customers[customer_id] = data
if not clean_customers:
logger.error("No valid customer records to export to JSON")
return False
with open(output_file, "w") as file:
json.dump(clean_customers, file, indent=2)
else:
logger.error("Unsupported format: %s", file_format)
return False
logger.info("Exported customer data to %s", output_file)
return True
except (OSError, TypeError, ValueError) as e:
logger.error("Error exporting data: %s", e)
return False