"""合成逐市场能力检查;币种绝对值不作跨池排序。""" import json capabilities = {"A": {"currency": "CNY", "growth": True}, "HK": {"currency": "HKD", "growth": False}, "US": {"currency": "USD", "growth": True}} rows = {"A": [("A1", 18, .12, 30), ("A2", 18, .12, 10)], "HK": [("H1", 18, None, 30)], "US": [("U1", 18, .12, 30), ("U2", 18, .05, 30)]} output = {} for market, contract in capabilities.items(): common = [s for s, pe, _, _ in rows[market] if 0 < pe < 20] if not contract["growth"]: output[market] = {"status": "partial", "common_pe_pass": common, "full_pass": None, "blocked_field": "growth", "currency": contract["currency"]} else: passed = [s for s, pe, growth, turnover in rows[market] if 0 < pe < 20 and growth is not None and growth >= .10 and turnover >= 20] output[market] = {"status": "complete", "common_pe_pass": common, "full_pass": passed, "currency": contract["currency"]} assert output["A"]["full_pass"] == ["A1"] and output["US"]["full_pass"] == ["U1"] assert output["HK"]["full_pass"] is None and output["HK"]["common_pe_pass"] == ["H1"] def rank_absolute(records): if len({currency for _, _, currency in records}) != 1: raise ValueError("mixed_currency_ranking") return sorted(records, key=lambda row: (-row[1], row[0])) try: rank_absolute([("A1", 30, "CNY"), ("U1", 30, "USD")]) except ValueError as exc: assert str(exc) == "mixed_currency_ranking" else: raise AssertionError("混币种排名必须阻断") print(json.dumps({"markets": output, "ranking_blocked": "mixed_currency_ranking", "threshold_basis": "20 million local currency per market; not economically equivalent"}, sort_keys=True))