1 Star 0 Fork 0

赵艳超/py-scripts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
import_metrics_elements.py 3.25 KB
一键复制 编辑 原始数据 按行查看 历史
zhaoyanchao 提交于 2023-12-09 19:42 . 添加指标元和指标的导入
import json
import warnings
from dataclasses import dataclass
from typing import Dict
import requests
from openpyxl.reader.excel import load_workbook
from model.consts import EXCEL_FILE_PATH, BASE_URL, HEADERS
from model.excel_utils import process_merged_cells
from model.json_utils import default_encoder
from model.model_def import LineStatistics
warnings.filterwarnings("ignore", category=UserWarning)
@dataclass
class MetricsElement:
key: str = ''
name: str = ''
script: str = ''
topicId: int = 0
dataItems: str = None
remark: str = ''
resultType: int = 1
resultRemark: str = ''
def parse_metrics_element(workbook, sheet_index) -> Dict[str, MetricsElement]:
# 先读取
sheet = workbook.worksheets[sheet_index]
merged_cells = process_merged_cells(sheet)
metrics_element_map = {}
# 行统计信息,用于调试数据缺失
line_statistics = LineStatistics()
# 读取数据
for row_number, row in enumerate(sheet.iter_rows(), start=1):
line_statistics.total += 1
# row,col 和 row[index] 中的index 下标不同,index 0开始, merged_cells 中 1开始
struct_name, field_key, metrics_element_name, metrics_element_key, metrics_element_script = (
row[5].value if row[5].value is not None else merged_cells.get((row_number, 6)),
row[7].value if row[7].value is not None else merged_cells.get((row_number, 8)),
row[8].value if row[8].value is not None else merged_cells.get((row_number, 9)),
row[9].value if row[9].value is not None else merged_cells.get((row_number, 10)),
row[10].value if row[10].value is not None else merged_cells.get((row_number, 11)))
if not (struct_name and field_key and metrics_element_name and metrics_element_key):
line_statistics.fail += 1
print(f"failed data {row_number}: {struct_name} {field_key}")
continue
struct_name, metrics_element_name = struct_name.replace('\n', ''), metrics_element_name.replace('\n', '')
if metrics_element_key in metrics_element_map:
print(f'duplicate metrics element key: {metrics_element_key}')
continue
metrics_element = MetricsElement()
metrics_element.key, metrics_element.name = metrics_element_key, metrics_element_name
metrics_element.script, metrics_element.remark = metrics_element_script, '结构:' + struct_name + ',字段:' + field_key
metrics_element_map[metrics_element_key] = metrics_element
print(f'line statistics: {line_statistics}')
return metrics_element_map
def save_metrics_element(metrics_element_map):
for metrics_element in metrics_element_map.values():
resp = requests.post(
url=BASE_URL + '/risk/sky/metrics/element',
data=json.dumps(metrics_element, default=default_encoder),
headers=HEADERS
)
print(f"resp: {resp.json()}")
def main():
# 解析
workbook = load_workbook(filename=EXCEL_FILE_PATH)
for sheet_index in range(len(workbook.worksheets)):
metrics_element_map = parse_metrics_element(workbook, sheet_index)
print(f'metrics element map {metrics_element_map}')
save_metrics_element(metrics_element_map)
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hibixby/py-scripts.git
git@gitee.com:hibixby/py-scripts.git
hibixby
py-scripts
py-scripts
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385