Files
pricing_review/_analysis/定额单价专题/scan_price_rows.py

113 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""单价行规律全量实证(全部后缀):单位工程视角,分部分项/措施两区——
①区内各 Norm 的 UnitPriceCalculationOfItem 行组是否一致 ②组内除 Total 外全字段是否一致。"""
import re, os, zipfile, glob, sys
from collections import defaultdict
def items_of(blk):
out = []
for m in re.finditer(r'<UnitPriceCalculationOfItem\b([^>]*?)/>', blk):
a = dict(re.findall(r'(\w+)="([^"]*)"', m.group(1)))
out.append((a.get("Name", ""), a.get("QtyFormula", ""), a.get("Rate", ""),
a.get("Code", ""), a.get("Remark", "")))
return out
def zones(txt):
zs = []
for m in re.finditer(r'<(DivisionalWorks|Preliminaries)\b', txt):
tag = m.group(1)
e = txt.find("</%s>" % tag, m.end())
zs.append((tag, m.start(), e if e > 0 else len(txt)))
return zs
def zone_of(zs, pos):
for tag, s, e in zs:
if s <= pos < e:
return "分部" if tag == "DivisionalWorks" else "措施"
return None
files = []
for pat in ("后台需要导入的项目/*", "_backup_session_20261104/src/*",
"_analysis/gld/*.xml", "_analysis/cos4_inspect/*.xml"):
files += glob.glob(pat)
files = [f for f in files if os.path.isfile(f)
and f.lower().endswith((".cos", ".cos4", ".swt", ".swz", ".xml"))]
ues = []
unread = []
for f in files:
txts = []
try:
z = zipfile.ZipFile(f)
for n in z.namelist():
if n.endswith(".xml") and n.lower() != "project.xml":
txts.append((n, z.read(n).decode("utf-8", "replace")))
except Exception:
try:
t = open(f, encoding="utf-8", errors="replace").read()
if "<Norm" in t:
txts.append((os.path.basename(f), t))
except Exception:
pass
if not txts:
unread.append(os.path.basename(f))
continue
for n, t in txts:
if "<Norm" in t and "UnitPriceCalculationOfItem" in t:
ues.append((os.path.basename(f), n, t))
zonepairs = {} # (src,ue) → {区: {指纹}}
itemcnt = defaultdict(lambda: defaultdict(int)) (lambda: defaultdict(int)) if False else defaultdict(lambda: defaultdict(int))
byext = defaultdict(lambda: {"ue": 0, "分部ok": 0, "分部n": 0, "措施ok": 0, "措施n": 0})
same = diff = 0
bad = []
for src, ue, txt in ues:
zs = zones(txt)
d = defaultdict(set)
for m in re.finditer(r'<Norm\b.*?</Norm>', txt, re.S):
z = zone_of(zs, m.start())
if z is None:
continue
its = items_of(m.group(0))
if not its:
continue
d[z].add((len(its), tuple(its)))
itemcnt[z][len(its)] += 1
if not d:
continue
ext = os.path.splitext(src)[1].lower()
byext[ext]["ue"] += 1
zonepairs[(src, ue)] = d
for z, okk in (("分部", "分部ok"), ("措施", "措施ok")):
if z in d:
if len(d[z]) == 1:
byext[ext][okk] += 1
else:
byext[ext][z + "n"] += 1
if len(bad) < 6:
bad.append((src, ue, z, sorted(d[z])))
if "分部" in d and "措施" in d:
if d["分部"] & d["措施"]:
same += 1
else:
diff += 1
print("文件:", len(files), " 读不开:", len(unread), unread[:4])
print("有单价项的单位工程:", len(zonepairs))
print("%-8s %5s %8s %8s %8s %8s" % ("后缀", "UE数", "分部唯一", "分部多套", "措施唯一", "措施多套"))
for ext, v in sorted(byext.items()):
print("%-8s %5d %8d %8d %8d %8d" % (ext, v["ue"], v["分部ok"], v["分部n"], v["措施ok"], v["措施n"]))
for z in ("分部", "措施"):
print("%s区 行数分布:" % z, dict(sorted(itemcnt[z].items())))
print("分部+措施同套 %d / 异套 %d" % (same, diff))
print("== 反例(前6)==")
for src, ue, z, fps in bad:
print(" %s | %s | %s 行数%s" % (src[:26], ue[:14], z, [f[0] for f in fps]))
a, b = fps[0][1], fps[-1][1]
for i in range(max(len(a), len(b))):
ra = a[i] if i < len(a) else ("—",) * 5
rb = b[i] if i < len(b) else ("—",) * 5
if ra != rb:
print(" 行%d A:%s" % (i + 1, ra))
print(" B:%s" % (rb,))