numbers.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """精确数值工具:Decimal 运算、缺失值传播、权重封顶。
  2. 设计原则(沿用第一阶段第三版代码):
  3. * 金额/数量一律用 ``Decimal``,不经过浮点,避免累加误差;
  4. * 任何无法识别的输入都向上传播为 ``None``(缺数),绝不当成 0;
  5. * 除数为 0 或缺失时返回 ``None``,由上层写入“计算说明”而不是填 0。
  6. """
  7. from __future__ import annotations
  8. import re
  9. import unicodedata
  10. from decimal import Decimal, DefaultContext, InvalidOperation, getcontext
  11. DefaultContext.prec = 50 # 新建的处理/接口线程也必须继承 50 位精度
  12. getcontext().prec = 50
  13. D = Decimal
  14. #: 指数基点(2025 年 1 月 = 1000)
  15. INDEX_BASE = D(1000)
  16. #: 单一价格样本权重上限
  17. WEIGHT_CAP = D("0.1")
  18. def number(raw: object) -> Decimal | None:
  19. """把任意原始单元格转换为 Decimal;无法识别时返回 None(不是 0)。"""
  20. if raw is None:
  21. return None
  22. if isinstance(raw, Decimal):
  23. return raw if raw.is_finite() else None
  24. if isinstance(raw, (int, float)):
  25. value = D(str(raw))
  26. return value if value.is_finite() else None
  27. text = unicodedata.normalize("NFKC", str(raw)).strip().strip('"\'').strip()
  28. text = text.replace("−", "-")
  29. if re.search(r"人民币|RMB|CNY|[¥¥€£]", text, re.I):
  30. return None
  31. text = re.sub(r"^(?:USD\s*|\$\s*)|(?:\s*USD|\s*美元)$", "", text, flags=re.I).strip()
  32. if text.startswith('(') and text.endswith(')'):
  33. text = '-' + text[1:-1]
  34. if re.fullmatch(r"[+-]?\d{1,3}(?:[, ]\d{3})+(?:\.\d+)?", text):
  35. text = text.replace(',', '').replace(' ', '')
  36. if text in {"", "-", "--", "/", "NaN", "nan", "NULL", "null"}:
  37. return None
  38. try:
  39. value = D(text)
  40. except (InvalidOperation, ValueError):
  41. return None
  42. return value if value.is_finite() else None
  43. def ratio(numerator: Decimal | None, denominator: Decimal | None, scale: object = 1) -> Decimal | None:
  44. """numerator / denominator * scale;任一缺失或分母为 0 时返回 None。"""
  45. if numerator is None or denominator is None or denominator == 0:
  46. return None
  47. return numerator / denominator * D(str(scale))
  48. def growth(current: Decimal | None, previous: Decimal | None) -> Decimal | None:
  49. """环比增长率 = 本期/上期 - 1(返回小数,写 Excel 时按百分比显示)。"""
  50. value = ratio(current, previous)
  51. return None if value is None else value - 1
  52. def index_of(current: Decimal | None, base: Decimal | None) -> Decimal | None:
  53. """固定基期指数 = 当期/基期 * 1000。"""
  54. return ratio(current, base, INDEX_BASE)
  55. def to_text(value: object) -> str:
  56. """把 Decimal 等写成不带科学计数法、不做舍入的文本。"""
  57. if value is None:
  58. return ""
  59. if isinstance(value, Decimal):
  60. if value == 0:
  61. return "0"
  62. text = format(value.normalize(), "f")
  63. return text
  64. return str(value)
  65. class StrictSum:
  66. """SQLite 聚合函数:任一成员无法识别则整体返回 NULL,而不是静默当 0。"""
  67. def __init__(self) -> None:
  68. self.total = D(0)
  69. self.bad = False
  70. def step(self, value: object) -> None:
  71. parsed = number(value)
  72. if parsed is None:
  73. self.bad = True
  74. else:
  75. self.total += parsed
  76. def finalize(self) -> str | None:
  77. return None if self.bad else str(self.total)
  78. def weights(amounts: list[Decimal]) -> list[Decimal]:
  79. """按年度进口金额计算价格指数样本权重。
  80. 规则:
  81. * 样本 < 10 个时取消 10% 上限,直接按金额占比(否则无解);
  82. * 样本 >= 10 个时,单样本不超过 10%,超额部分在剩余样本中按金额占比迭代再分配。
  83. """
  84. if not amounts or any(a is None or a <= 0 for a in amounts):
  85. return []
  86. total = sum(amounts)
  87. if total <= 0:
  88. return []
  89. if len(amounts) < 10:
  90. return [a / total for a in amounts]
  91. free = set(range(len(amounts)))
  92. result = [D(0)] * len(amounts)
  93. remaining = D(1)
  94. while free:
  95. subtotal = sum(amounts[i] for i in free)
  96. if subtotal <= 0:
  97. break
  98. over = {i for i in free if amounts[i] * remaining / subtotal > WEIGHT_CAP}
  99. if not over:
  100. for i in free:
  101. result[i] = amounts[i] * remaining / subtotal
  102. break
  103. for i in over:
  104. result[i] = WEIGHT_CAP
  105. remaining -= WEIGHT_CAP * len(over)
  106. free -= over
  107. return result