"""精确数值工具:Decimal 运算、缺失值传播、权重封顶。 设计原则(沿用第一阶段第三版代码): * 金额/数量一律用 ``Decimal``,不经过浮点,避免累加误差; * 任何无法识别的输入都向上传播为 ``None``(缺数),绝不当成 0; * 除数为 0 或缺失时返回 ``None``,由上层写入“计算说明”而不是填 0。 """ from __future__ import annotations import re import unicodedata from decimal import Decimal, DefaultContext, InvalidOperation, getcontext DefaultContext.prec = 50 # 新建的处理/接口线程也必须继承 50 位精度 getcontext().prec = 50 D = Decimal #: 指数基点(2025 年 1 月 = 1000) INDEX_BASE = D(1000) #: 单一价格样本权重上限 WEIGHT_CAP = D("0.1") def number(raw: object) -> Decimal | None: """把任意原始单元格转换为 Decimal;无法识别时返回 None(不是 0)。""" if raw is None: return None if isinstance(raw, Decimal): return raw if raw.is_finite() else None if isinstance(raw, (int, float)): value = D(str(raw)) return value if value.is_finite() else None text = unicodedata.normalize("NFKC", str(raw)).strip().strip('"\'').strip() text = text.replace("−", "-") if re.search(r"人民币|RMB|CNY|[¥¥€£]", text, re.I): return None text = re.sub(r"^(?:USD\s*|\$\s*)|(?:\s*USD|\s*美元)$", "", text, flags=re.I).strip() if text.startswith('(') and text.endswith(')'): text = '-' + text[1:-1] if re.fullmatch(r"[+-]?\d{1,3}(?:[, ]\d{3})+(?:\.\d+)?", text): text = text.replace(',', '').replace(' ', '') if text in {"", "-", "--", "/", "NaN", "nan", "NULL", "null"}: return None try: value = D(text) except (InvalidOperation, ValueError): return None return value if value.is_finite() else None def ratio(numerator: Decimal | None, denominator: Decimal | None, scale: object = 1) -> Decimal | None: """numerator / denominator * scale;任一缺失或分母为 0 时返回 None。""" if numerator is None or denominator is None or denominator == 0: return None return numerator / denominator * D(str(scale)) def growth(current: Decimal | None, previous: Decimal | None) -> Decimal | None: """环比增长率 = 本期/上期 - 1(返回小数,写 Excel 时按百分比显示)。""" value = ratio(current, previous) return None if value is None else value - 1 def index_of(current: Decimal | None, base: Decimal | None) -> Decimal | None: """固定基期指数 = 当期/基期 * 1000。""" return ratio(current, base, INDEX_BASE) def to_text(value: object) -> str: """把 Decimal 等写成不带科学计数法、不做舍入的文本。""" if value is None: return "" if isinstance(value, Decimal): if value == 0: return "0" text = format(value.normalize(), "f") return text return str(value) class StrictSum: """SQLite 聚合函数:任一成员无法识别则整体返回 NULL,而不是静默当 0。""" def __init__(self) -> None: self.total = D(0) self.bad = False def step(self, value: object) -> None: parsed = number(value) if parsed is None: self.bad = True else: self.total += parsed def finalize(self) -> str | None: return None if self.bad else str(self.total) def weights(amounts: list[Decimal]) -> list[Decimal]: """按年度进口金额计算价格指数样本权重。 规则: * 样本 < 10 个时取消 10% 上限,直接按金额占比(否则无解); * 样本 >= 10 个时,单样本不超过 10%,超额部分在剩余样本中按金额占比迭代再分配。 """ if not amounts or any(a is None or a <= 0 for a in amounts): return [] total = sum(amounts) if total <= 0: return [] if len(amounts) < 10: return [a / total for a in amounts] free = set(range(len(amounts))) result = [D(0)] * len(amounts) remaining = D(1) while free: subtotal = sum(amounts[i] for i in free) if subtotal <= 0: break over = {i for i in free if amounts[i] * remaining / subtotal > WEIGHT_CAP} if not over: for i in free: result[i] = amounts[i] * remaining / subtotal break for i in over: result[i] = WEIGHT_CAP remaining -= WEIGHT_CAP * len(over) free -= over return result