142 lines
5.3 KiB
Python
142 lines
5.3 KiB
Python
import sys, os
|
||
|
||
def caculate_work_days(start_date : str, end_date : str) -> str:
|
||
"""根据起止日期计算工期
|
||
|
||
Args:
|
||
start_date (str): 格式: yyyy-mm-ddThh:mm:ss
|
||
end_date (str): 格式: yyyy-mm-ddThh:mm:ss
|
||
|
||
Returns:
|
||
str: 计算出来的总工期,单位为天
|
||
"""
|
||
import datetime
|
||
if start_date == None or end_date == None:
|
||
return f"开始时间{start_date} ---- 结束时间{end_date} 日期格式错误"
|
||
|
||
start_date = datetime.datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S')
|
||
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S')
|
||
|
||
return (end_date - start_date).days
|
||
|
||
|
||
|
||
def get_year_month(date):
|
||
"""根据格式化date字符串获取年月 'date': '二〇二一年十二月十日 9:00'
|
||
|
||
Args: date (str): 日期字符串
|
||
|
||
Returns: 年月字符串 '二〇二一年十二月'
|
||
"""
|
||
unit_map = {'1' : '一', '2' : '二', '3' : '三', '4' : '四', '5' : '五', '6' : '六', '7' : '七', '8' : '八', '9' : '九', '0' : '〇'}
|
||
unit_map_month = {1 : '一', 2 : '二', 3 : '三', 4 : '四', 5 : '五', 6 : '六', 7 : '七', 8 : '八', 9 : '九', 10 : '十', 11 : '十一', 12 : '十二'}
|
||
year = date.split('年')[0]
|
||
month = date.split('年')[1].split('月')[0]
|
||
year = ''.join([unit_map[i] for i in year])
|
||
month = unit_map_month[int(month)]
|
||
return f"{year}年{month}月"
|
||
|
||
def merge_info(frontend_info, default_info):
|
||
"""
|
||
合并前端传入的 info 和默认 info
|
||
新规则:以default_info为基础字典,用frontend_info完全覆写,取两者的并集
|
||
|
||
Args:
|
||
frontend_info: 前端传入的字典
|
||
default_info: 默认的完整字典
|
||
Returns:
|
||
合并后的完整字典
|
||
"""
|
||
if not isinstance(frontend_info, dict) or frontend_info is None:
|
||
return default_info.copy()
|
||
|
||
# 先复制默认字典
|
||
merged_info = default_info.copy()
|
||
# 用前端字典完全覆写
|
||
merged_info.update(frontend_info)
|
||
|
||
return merged_info
|
||
|
||
def merge_dicts(dict1, dict2):
|
||
# 创建一个新的字典来存储合并结果
|
||
merged_dict = {}
|
||
|
||
# 遍历第一个字典
|
||
for key, value_list in dict1.items():
|
||
if key in dict2:
|
||
# 如果键在第二个字典中存在,合并两个列表
|
||
merged_dict[key] = value_list + dict2[key]
|
||
else:
|
||
# 如果键在第二个字典中不存在,直接使用第一个字典的值列表
|
||
merged_dict[key] = value_list
|
||
|
||
# 遍历第二个字典
|
||
for key, value_list in dict2.items():
|
||
if key not in dict1:
|
||
# 如果键在第一个字典中不存在,直接使用第二个字典的值列表
|
||
merged_dict[key] = value_list
|
||
|
||
return merged_dict
|
||
|
||
def get_defect_str(Y_defect_list : list[dict] ) -> list:
|
||
"""将叶片缺陷信息转换为一条条描述信息
|
||
|
||
Args:
|
||
Y_defect_list (list):[
|
||
{
|
||
'record' : {'defectId': '02d892a178a82561bb565559102c7a58', 'imageId': '41543f531be24522b7bec741b9a483a2', 'defectName': '手动添加的缺陷1', 'defectCode': None, 'partName': '叶片1', 'defectTypeLabel': '表面裂纹', 'defectType': 'bmlw', 'defectLevelLabel': '轻微缺陷', 'defectLevel': 'SLIGHT', 'defectPosition': '', 'description': '手动添加的缺陷,请填写详细描述', 'repairIdea': '建议进行进一步检查', 'labelInfo': None, 'markInfo': {'label': None, 'clsId': None, 'bbox': None, 'confidence': 0.0}}
|
||
'imagePath' : '/image/path'
|
||
},
|
||
....
|
||
]
|
||
Returns:
|
||
result (list):
|
||
[
|
||
"叶片1表面裂纹轻微缺陷,位于{defectPosition},建议进行进一步检查",
|
||
],
|
||
...
|
||
"""
|
||
result = []
|
||
for item in Y_defect_list:
|
||
record = item['record']
|
||
defect_type_label = record.get('defectTypeLabel', '未知类型')
|
||
defect_level_label = record.get('defectLevelLabel', '未知等级')
|
||
defect_position = record.get('defectPosition', '未知位置')
|
||
repair_idea = record.get('repairIdea', '无建议')
|
||
|
||
defect_description = f"{defect_type_label}{defect_level_label}缺陷,位于{defect_position},{repair_idea}"
|
||
result.append(defect_description)
|
||
|
||
return result
|
||
|
||
def safe_get(data, *keys, default=None):
|
||
"""
|
||
递归安全访问嵌套字典的键,如果中间键不存在,则返回默认值
|
||
|
||
Args:
|
||
data (dict): 要访问的字典
|
||
*keys: 要访问的键(可以是多个,如 "叶片1", "裂纹")
|
||
default: 如果键不存在,返回的默认值(默认None)
|
||
|
||
Returns:
|
||
如果所有键都存在,返回对应的值;否则返回 default
|
||
"""
|
||
if not keys or data is None:
|
||
return data if data is not None else default
|
||
|
||
current_key = keys[0]
|
||
if isinstance(data, dict):
|
||
return safe_get(data.get(current_key), *keys[1:], default=default)
|
||
else:
|
||
return default
|
||
|
||
def get_resource_path(relative_path):
|
||
""" 获取打包后资源的绝对路径 """
|
||
try:
|
||
# PyInstaller创建的临时文件夹
|
||
base_path = sys._MEIPASS
|
||
except AttributeError:
|
||
# 正常开发环境
|
||
base_path = os.path.abspath(".")
|
||
|
||
return os.path.join(base_path, relative_path) |