#!/usr/bin/env python3 """How a Shadbala strength is shown (astrologer rulings 2026-10-03 甲13, BUG-1208). Whether the planet meets the BPHS minimum (BPHS ch. 27 vv. 32-33, the `shadbala.MIN_REQUIRED` table). `shadbala.py` is in the frozen rectification identity, so the fields are added here, at display time, from values the engine already returns; nothing is recomputed. BUG-1961 (占星师 2026-11-17 问答数据口径 Q2): the site's own band (150/125/100/75/50 % of that minimum -> 极强…极弱) has no named rule or source, so it is no longer presented. `minimum_fields` / `row_fields` stopped returning `level` and `level_source`; the engine still computes `strength_level` (the rectification feature hash covers the whole row) but no surface shows it. What stays: the minimum line, met / not met, and the raw total ÷ minimum ratio. """ from __future__ import annotations from typing import Any, Dict, Optional LEVEL_SOURCE = 'site_band' SITE_BAND_SUFFIX = '(网站分档)' SITE_BAND_SUFFIX_EN = ' (site band)' # BUG-2266 (问答可读性盘点 C-S4): 原值「达到/未达到 BPHS 最低要求」;新值不写书名,与正文同一说法(BUG-2243)。 MEETS_ZH = '达到最低线' MISSES_ZH = '未达到最低线' MEETS_EN = 'meets the BPHS minimum' MISSES_EN = 'below the BPHS minimum' def _number(value: Any) -> Optional[float]: return float(value) if isinstance(value, (int, float)) and not isinstance(value, bool) else None def minimum_fields(rupas: Any, min_required: Any, band: Any = None) -> Dict[str, Any]: """`min_required`, `meets_minimum`, `minimum_check`. 原值: 另外返回 `level`(如「略弱(网站分档)」)和 `level_source: site_band` 新值: 不再返回这两项;`band` 参数保留只为不改调用方,值不使用 原因: BUG-1961,占星师 2026-11-17 Q2:分档没有具名规则和来源,冻结前不自动显示 """ total = _number(rupas) minimum = _number(min_required) meets = None if total is None or minimum is None else total >= minimum return { 'min_required': minimum, 'meets_minimum': meets, 'minimum_check': None if meets is None else (MEETS_ZH if meets else MISSES_ZH), } PARAMETER_SENSITIVE = 'parameter_sensitive' def row_fields(row: Dict[str, Any]) -> Dict[str, Any]: """Display fields for one `shadbala.calc_shadbala` planet row. 占星师 2026-11-08 第五轮裁定 4: when `shadbala_service.sensitivity` found that the open parameter choices flip the planet's pass/fail, `minimum_check` is `parameter_sensitive` (the default verdict stays in `minimum_check_default`) and the open questions that flip it are listed. """ fields = minimum_fields(row.get('total_rupas', row.get('rupas')), row.get('min_required'), row.get('strength_level')) sensitivity = row.get('parameter_sensitivity') if isinstance(row.get('parameter_sensitivity'), dict) else {} if row.get('minimum_check_status') == PARAMETER_SENSITIVE and fields.get('minimum_check'): fields['minimum_check_default'] = fields['minimum_check'] fields['minimum_check'] = PARAMETER_SENSITIVE fields['minimum_open_questions'] = list(sensitivity.get('open_questions') or []) elif row.get('minimum_check_status'): fields['minimum_check_status'] = row['minimum_check_status'] return fields BAND_EN = {'极强': 'very strong', '强': 'strong', '充足': 'adequate', '略弱': 'slightly weak', '弱': 'weak', '极弱': 'very weak'} def band_text(band: Any, language: str = 'zh') -> Optional[str]: if band in (None, ''): return None if language == 'en': return f"{BAND_EN.get(str(band), str(band))}{SITE_BAND_SUFFIX_EN}" return f'{band}{SITE_BAND_SUFFIX}' def check_text(meets: Optional[bool], language: str = 'zh') -> str: if meets is None: return '-' if language == 'en': return MEETS_EN if meets else MISSES_EN return MEETS_ZH if meets else MISSES_ZH # BUG-2266: 原值「parameter_sensitive(是否达到 BPHS 最低要求取决于未定 Shadbala 参数,不据此下好坏结论)」; # 新值用 BUG-2243 的原句(占星师 11-17 裁定),不露字段名和书名。 SENSITIVE_ZH = '按本报告所用方法的结果见合计;其他取法结果不同,不据此下好坏结论' SENSITIVE_EN = 'parameter_sensitive (whether the BPHS minimum is met depends on open Shadbala parameters; no good/bad conclusion)' def row_check_text(row: Dict[str, Any], language: str = 'zh') -> str: """`check_text` for a whole row: parameter_sensitive wins over the default verdict (第五轮裁定 4).""" if isinstance(row, dict) and row.get('minimum_check_status') == PARAMETER_SENSITIVE: if language == 'en': return SENSITIVE_EN # BUG-2266: BUG-2243's own sentence, with this method's result when the row has one. meets = row_fields(row)['meets_minimum'] if meets is None: return SENSITIVE_ZH return f"按本报告所用方法{'达到' if meets else '未达到'}最低线;其他取法结果不同" return check_text(row_fields(row)['meets_minimum'] if isinstance(row, dict) else None, language) REQUIRED_STRENGTH_SEMANTICS = 'deprecated_alias_of_required_strength_pct_not_ishta_phala' def attach_required_strength_alias(result: Any) -> Any: """Add `required_strength_pct` beside `ishta_bala_pct` on every planet row, in place. 产品 2026-11-07 同步占星师交接分支 99076dad:`ishta_bala_pct` 是总 Rupa ÷ BPHS 最低 要求 × 100(达标比),不是 Ishta Phala。她在 `shadbala.calc_shadbala` 里加了明确名 `required_strength_pct` 和别名说明 `ishta_bala_pct_semantics`;本仓 `shadbala.py` 在冻结身份里(BUG-1699 测试),所以同名字段在这里补,值照抄、不重算。 """ planets = result.get('planets') if isinstance(result, dict) else None if not isinstance(planets, dict): return result for row in planets.values(): if isinstance(row, dict) and _number(row.get('ishta_bala_pct')) is not None: row.setdefault('required_strength_pct', row['ishta_bala_pct']) row.setdefault('ishta_bala_pct_semantics', REQUIRED_STRENGTH_SEMANTICS) # BUG-1964(占星师 2026-11-17 第二批第 9 项):`ishta_bala_pct` 若只是总分 ÷ 最低线, # 迁到 `required_strength_ratio` 等不混淆的名字;旧字段保留供兼容读取。 # 值取引擎给的 pct ÷ 100(引擎已把 pct 取到 0.1);最低线比较仍用 total_rupas 与 min_required 原值, # 不用这个比值;显示取整另由页面做。 row.setdefault('required_strength_ratio', _number(row['ishta_bala_pct']) / 100.0) return result