更新代码
This commit is contained in:
+37
-24
@@ -112,46 +112,59 @@ class ExpenseProcessor:
|
||||
@staticmethod
|
||||
def find_guid_quantity(project_data: Optional[Dict[str, Any]], guid: str) -> float:
|
||||
"""
|
||||
在projectDivision中查找指定GUID节点的数量
|
||||
在 projectDivision 中查找指定 GUID 节点的数量。
|
||||
|
||||
:param project_data: 项目数据
|
||||
:param guid: 要查找的GUID(带花括号的格式)
|
||||
:return: 数量值,如果未找到则返回1.0
|
||||
:param guid: 要查找的 GUID(带花括号的格式,如 "{12345678-...}")
|
||||
:return: 数量值(float)
|
||||
:raises KeyError: 如果未找到指定 GUID 的节点
|
||||
:raises ValueError: 如果找到节点但缺少 "数量" 字段,或数量无法转换为 float
|
||||
"""
|
||||
if not project_data or "projectDivision" not in project_data:
|
||||
return 1.0
|
||||
raise KeyError(f"projectDivision not found in project_data")
|
||||
|
||||
# 移除花括号以便比较
|
||||
guid_clean = guid.strip("{}")
|
||||
|
||||
def search_node_quantity(node):
|
||||
if isinstance(node, dict):
|
||||
# 检查当前节点的GUID
|
||||
node_guid = node.get("GUID", "").strip("{}")
|
||||
if node_guid == guid_clean:
|
||||
# 找到匹配的GUID,获取数量
|
||||
quantity = node.get("数量")
|
||||
if quantity:
|
||||
try:
|
||||
return float(quantity)
|
||||
except (ValueError, TypeError):
|
||||
return 1.0
|
||||
if "数量" not in node:
|
||||
raise ValueError(f"Node with GUID {guid} has no '数量' field")
|
||||
quantity = node["数量"]
|
||||
try:
|
||||
return float(quantity)
|
||||
except (ValueError, TypeError) as e:
|
||||
raise ValueError(f"Invalid quantity value for GUID {guid}: {quantity}") from e
|
||||
|
||||
# 递归查找子节点
|
||||
for key, value in node.items():
|
||||
# 递归搜索子节点
|
||||
for value in node.values():
|
||||
if isinstance(value, (dict, list)):
|
||||
result = search_node_quantity(value)
|
||||
if result != 1.0: # 找到非默认值
|
||||
try:
|
||||
result = search_node_quantity(value)
|
||||
return result
|
||||
except (KeyError, ValueError):
|
||||
continue # 继续搜索其他分支
|
||||
# 当前 dict 分支未找到
|
||||
raise KeyError(f"GUID {guid} not found in this branch")
|
||||
|
||||
elif isinstance(node, list):
|
||||
for item in node:
|
||||
result = search_node_quantity(item)
|
||||
if result != 1.0: # 找到非默认值
|
||||
return result
|
||||
try:
|
||||
return search_node_quantity(item)
|
||||
except (KeyError, ValueError):
|
||||
continue
|
||||
# 整个列表都未找到
|
||||
raise KeyError(f"GUID {guid} not found in list")
|
||||
|
||||
return 1.0 # 默认返回1.0
|
||||
else:
|
||||
# 非 dict/list 类型,不可能包含目标节点
|
||||
raise KeyError(f"GUID {guid} not found")
|
||||
|
||||
return search_node_quantity(project_data["projectDivision"])
|
||||
try:
|
||||
return search_node_quantity(project_data["projectDivision"])
|
||||
except KeyError:
|
||||
raise KeyError(f"projectDivision中没找到对应的GUID {guid}")
|
||||
|
||||
@staticmethod
|
||||
def process_node(
|
||||
@@ -385,8 +398,8 @@ def costsummary_upwards(
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
input_directory = "data/output/json"
|
||||
output_directory = "data/output/merged"
|
||||
input_directory = "data/input/json"
|
||||
output_directory = "data/input/merged"
|
||||
# 自动判断工程类型
|
||||
result = costsummary_upwards(input_directory, output_directory)
|
||||
if result:
|
||||
|
||||
Reference in New Issue
Block a user