data_processor.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import pandas as pd
  2. import json
  3. import yaml
  4. import csv
  5. from pathlib import Path
  6. from core.config.config_manager import ConfigManager
  7. from core.utils.logger import Logger
  8. from core.singleton import Singleton
  9. class DataProcessor(metaclass=Singleton):
  10. def __init__(self):
  11. self.config = ConfigManager()
  12. self.logger = Logger.get_logger()
  13. self.data_dir = Path(__file__).parent.parent.parent / 'resources' / 'test_data'
  14. def read_json(self, file_path):
  15. """读取JSON文件"""
  16. try:
  17. with open(file_path, 'r', encoding='utf-8') as f:
  18. return json.load(f)
  19. except Exception as e:
  20. self.logger.error(f"读取JSON文件失败: {file_path}, 错误: {str(e)}")
  21. raise
  22. def write_json(self, data, file_path):
  23. """写入JSON文件"""
  24. try:
  25. with open(file_path, 'w', encoding='utf-8') as f:
  26. json.dump(data, f, indent=4, ensure_ascii=False)
  27. self.logger.info(f"JSON文件已写入: {file_path}")
  28. except Exception as e:
  29. self.logger.error(f"写入JSON文件失败: {file_path}, 错误: {str(e)}")
  30. raise
  31. def read_yaml(self, file_path):
  32. """读取YAML文件"""
  33. try:
  34. with open(file_path, 'r', encoding='utf-8') as f:
  35. return yaml.safe_load(f)
  36. except Exception as e:
  37. self.logger.error(f"读取YAML文件失败: {file_path}, 错误: {str(e)}")
  38. raise
  39. def write_yaml(self, data, file_path):
  40. """写入YAML文件"""
  41. try:
  42. with open(file_path, 'w', encoding='utf-8') as f:
  43. yaml.dump(data, f, allow_unicode=True)
  44. self.logger.info(f"YAML文件已写入: {file_path}")
  45. except Exception as e:
  46. self.logger.error(f"写入YAML文件失败: {file_path}, 错误: {str(e)}")
  47. raise
  48. def read_csv(self, file_path, **kwargs):
  49. """读取CSV文件"""
  50. try:
  51. return pd.read_csv(file_path, **kwargs)
  52. except Exception as e:
  53. self.logger.error(f"读取CSV文件失败: {file_path}, 错误: {str(e)}")
  54. raise
  55. def write_csv(self, data, file_path, **kwargs):
  56. """写入CSV文件"""
  57. try:
  58. if isinstance(data, pd.DataFrame):
  59. data.to_csv(file_path, **kwargs)
  60. else:
  61. with open(file_path, 'w', newline='', encoding='utf-8') as f:
  62. writer = csv.writer(f)
  63. writer.writerows(data)
  64. self.logger.info(f"CSV文件已写入: {file_path}")
  65. except Exception as e:
  66. self.logger.error(f"写入CSV文件失败: {file_path}, 错误: {str(e)}")
  67. raise
  68. def read_excel(self, file_path, **kwargs):
  69. """读取Excel文件"""
  70. try:
  71. return pd.read_excel(file_path, **kwargs)
  72. except Exception as e:
  73. self.logger.error(f"读取Excel文件失败: {file_path}, 错误: {str(e)}")
  74. raise
  75. def write_excel(self, data, file_path, **kwargs):
  76. """写入Excel文件"""
  77. try:
  78. if isinstance(data, pd.DataFrame):
  79. data.to_excel(file_path, **kwargs)
  80. else:
  81. df = pd.DataFrame(data)
  82. df.to_excel(file_path, **kwargs)
  83. self.logger.info(f"Excel文件已写入: {file_path}")
  84. except Exception as e:
  85. self.logger.error(f"写入Excel文件失败: {file_path}, 错误: {str(e)}")
  86. raise
  87. def get_test_data(self, key, default=None):
  88. """获取测试数据"""
  89. try:
  90. # 从配置中获取测试数据
  91. test_data = self.config.get('test_data', {})
  92. # 按点号分隔的键路径查找
  93. keys = key.split('.')
  94. value = test_data
  95. for k in keys:
  96. if isinstance(value, dict) and k in value:
  97. value = value[k]
  98. else:
  99. return default
  100. return value
  101. except Exception as e:
  102. self.logger.error(f"获取测试数据失败: {key}, 错误: {str(e)}")
  103. return default
  104. def generate_test_data(self, template, count=1, **kwargs):
  105. """生成测试数据"""
  106. # 在实际应用中,可以实现根据模板生成测试数据的逻辑
  107. # 这里返回模拟数据
  108. if count == 1:
  109. return {
  110. 'name': '测试用户',
  111. 'email': 'test@example.com',
  112. 'age': 30,
  113. 'city': '测试城市'
  114. }
  115. else:
  116. return [
  117. {
  118. 'name': f'测试用户{i}',
  119. 'email': f'test{i}@example.com',
  120. 'age': 20 + i,
  121. 'city': f'测试城市{i}'
  122. }
  123. for i in range(1, count + 1)
  124. ]
  125. def validate_data(self, data, schema):
  126. """验证数据是否符合模式"""
  127. # 在实际应用中,可以实现数据验证逻辑
  128. # 这里返回模拟验证结果
  129. return {
  130. 'valid': True,
  131. 'errors': []
  132. }