优化代码

This commit is contained in:
2026-06-04 15:28:53 +08:00
parent 83a425c235
commit d3af2309dc
7 changed files with 55 additions and 35 deletions
+2 -2
View File
@@ -11,7 +11,7 @@
import logging
from .base import DataLog, SignalData, ExcelDataResult
from .mil_read_data_excel import read_excel_data, get_data_log, ExcelReaderConfig
from .mil_read_data_excel import read_excel_data, ExcelReaderConfig
from .mil_read_case_excel import read_excel_case
from .mil_create_data_excel import create_excel_case
from .mil_update_excel import update_case_excel
@@ -31,9 +31,9 @@ __all__ = [
"ExcelDataResult",
"ExcelReaderConfig",
"read_excel_data",
"get_data_log",
"read_excel_case",
"create_excel_case",
"update_case_excel",
"MILSDKError",
"ExcelReadError",
"ExcelFormatError",
+4 -2
View File
@@ -24,10 +24,12 @@ class SignalData:
column: 列索引
datalog: 数据日志列表
"""
signal_type: str | None = None
# signal_type: str | None = None
column: int = 0
attributes: dict[str, str] = field(default_factory=dict)
datalog: list[DataLog] = field(default_factory=list)
def to_dict(self) -> dict[str, Any]:
"""转换为字典格式
@@ -35,8 +37,8 @@ class SignalData:
包含 signal_type、column、datalog 的字典
"""
return {
"type": self.signal_type,
"column": self.column,
"attributes": self.attributes,
"datalog": self.datalog
}
+9 -1
View File
@@ -59,13 +59,21 @@ def create_excel_case(
generate_path = f"{excel_path}/{name}.xlsx"
_write_excel_data(sheet, datas_dict[name], source_row)
wb.save(generate_path)
logger.info(f"已生成用例: {name},路径: {generate_path}")
logger.info(f"测试用例生成完成,共 {case_count} 个用例")
def _write_excel_data(sheet: Worksheet, data_dict: dict, source_row: int) -> None:
"""写入 Excel 数据"""
"""写入 Excel 数据
Args:
sheet: Worksheet 对象
data_dict: 数据字典
source_row: Source: Input 所在行号
"""
for name in data_dict.keys():
column = data_dict[name]["column"]
datalog = data_dict[name]["datalog"]
+27 -5
View File
@@ -40,6 +40,7 @@ class ExcelReaderConfig:
time_column: int = 1
header_row: int = 1
type_row: int = 3
interp_row: int = 6
data_start_row_offset: int = 1
output_header: str = "Source: Output"
@@ -124,12 +125,13 @@ def read_excel_data(
if name in ("Parameter:", "Value", "BlockPath"):
column += 1
continue
sig_type = sheet.cell(config.type_row, column).value
attributes = __get_signal_attributes(sheet, column, source_row)
if attributes is None:
break
signals[name] = SignalData(
signal_type=sig_type,
column=column,
datalog=get_data_log(sheet, source_row + config.data_start_row_offset, column, config.time_column)
attributes=attributes,
datalog=__get_data_log(sheet, source_row + config.data_start_row_offset, column, config.time_column)
)
logger.debug(f"解析信号: {name}, 数据点: {len(signals[name].datalog)}")
else:
@@ -157,7 +159,27 @@ def read_excel_data(
return data
def get_data_log(
def __get_signal_attributes(
sheet: Worksheet,
column: int,
max_row: int
) -> dict[int, str]:
"""从指定位置读取信号属性字典
Args:
sheet: Worksheet 对象
column: 列号
max_row: 最大行号
Returns:
信号属性字典,键为行号,值为单元格值
"""
attributes: dict[int, str] = {}
for row in range(1, max_row):
attributes[row] = sheet.cell(row=row, column=column).value
return attributes
def __get_data_log(
sheet: Worksheet,
row: int,
column: int,
+11 -9
View File
@@ -30,11 +30,11 @@ def update_case_excel(filename: str, old_data: dict, new_data: dict) -> None:
old_wb = old_data.pop('wb')
old_sheet = old_data.pop('sheet')
source_row = old_data.pop('source_row')
old_source_row = old_data.pop('source_row')
new_data.pop('wb')
new_data.pop('sheet')
new_data.pop('source_row')
new_source_row = new_data.pop('source_row')
if not isinstance(old_wb, Workbook) or not isinstance(old_sheet, Worksheet):
logger.error("数据类型错误,Workbook 或 Worksheet 类型不匹配")
@@ -47,17 +47,19 @@ def update_case_excel(filename: str, old_data: dict, new_data: dict) -> None:
for name, info in reversed(del_input.items()):
old_sheet.delete_cols(info['column'])
logger.info(f"删除第{info['column']}\t信号名: {name}")
for name, info in add_input.items():
old_sheet.insert_cols(info['column'])
old_sheet.cell(1, info['column']).value = name
old_sheet.cell(1, info['column']).data_type = "str"
for index in range(1,old_source_row):
if index == old_source_row -1 :
old_sheet.cell(index, info['column']).value = info['attributes'][new_source_row - 1]
old_sheet.cell(index, info['column']).data_type = "str"
else:
old_sheet.cell(index, info['column']).value = info['attributes'][index]
old_sheet.cell(index, info['column']).data_type = "str"
old_sheet.cell(3, info['column']).value = info['type']
old_sheet.cell(3, info['column']).data_type = "str"
for index in range(source_row + 1, datalog_len + source_row + 1):
for index in range(old_source_row + 1, datalog_len + old_source_row + 1):
try:
value = int(info['datalog'][0].value)
old_sheet.cell(index, info['column']).value = value