diff --git a/M02_MasterTemplete/M02_Machine_Hourly.py b/M02_MasterTemplete/M02_Machine_Hourly.py index c263059c6..1911f808c 100644 --- a/M02_MasterTemplete/M02_Machine_Hourly.py +++ b/M02_MasterTemplete/M02_Machine_Hourly.py @@ -235,7 +235,9 @@ def doc_sums(row: dict, logic: dict, index: Any) -> dict | None: return {"ok": True, "합": {k: float(got[k]) for k in (*cm.mf.COST_ITEMS, "계")}} -_BRIEF: dict[tuple, dict] = {} +#: {키: (색인, 줄)} — 색인이 바뀌면 비움 · 꺼낼 때 `is` 확인(옛 색인 주소 재사용 · 옛 값 막음 · 854-1) +_BRIEF: dict[str, tuple[Any, dict]] = {} +_BRIEF_OF: list[Any] = [None] def brief(keys: list[str]) -> list[dict[str, Any]]: @@ -246,11 +248,15 @@ def brief(keys: list[str]) -> list[dict[str, Any]]: from M02_MasterTemplete.M02_Item_Prices import to_json index, mf = store.loaded()[1], cm.mf - if len(_BRIEF) > 2000: # ponytail: 통째 비움 · 기계 수(655)의 세 배 + # ponytail: 통째 비움 · 기계 수(655)의 세 배 + if _BRIEF_OF[0] is not index or len(_BRIEF) > 2000: _BRIEF.clear() + _BRIEF_OF[0] = index out = [] for key in keys[:60]: - if (got := _BRIEF.get((id(index), key))) is None: + hit = _BRIEF.get(key) + got = hit[1] if hit and hit[0] is index else None + if got is None: try: row = (index.numbers.get("EQ") or {}).get(key) or index.get(key) except mf.FormulaError: @@ -272,7 +278,8 @@ def brief(keys: list[str]) -> list[dict[str, Any]]: ) hour = {"ok": True, "로직": logic["키"]} hour |= {k: float(sums[k]) for k in (*cm.mf.COST_ITEMS, "계")} - got = _BRIEF[(id(index), key)] = {**head, "값": to_json(row.get("값")), "시간당": hour} + got = {**head, "값": to_json(row.get("값")), "시간당": hour} + _BRIEF[key] = (index, got) out.append(got) return out diff --git a/resources/tester/test_854_1_my_machine_lock.py b/resources/tester/test_854_1_my_machine_lock.py index 001cf95e4..ede2f0ada 100644 --- a/resources/tester/test_854_1_my_machine_lock.py +++ b/resources/tester/test_854_1_my_machine_lock.py @@ -75,3 +75,24 @@ def test_counter_behind_skips_file(world): second = _at(me, mym.create, _easy("sub4임시둘째"))["줄"]["키"] assert second != first assert _at(me, mym.one, first)["줄"]["이름"] == "sub4임시처음" + + +def test_brief_cache_follows_index(monkeypatch): + """④ 기계 창 목록 메모(`Machine_Hourly.brief`) — 색인이 바뀌면 새 값 · 옛 색인 메모를 안 꺼냄 · 되돌리면 처음 값.""" + from M01_MasterData import M01_MasterData_Store as m01 + from M02_MasterTemplete import M02_Machine_Hourly as mh + from M02_MasterTemplete import M02_Sangeun_Copy as skc + + key = "EQ000022" # 굴착기 — 가격 · 시간당 있음 + base = m01.loaded() + first = mh.brief([key])[0] + row = base[1].get(key) + twice = {**row, "값": {**row["값"], "가격": row["값"]["가격"] * 2}} + other = skc.Copy(base[1], [twice], ("시험854",)) + monkeypatch.setattr(m01, "loaded", lambda: (base[0], other)) + got = mh.brief([key])[0] + assert got["값"]["가격"] == first["값"]["가격"] * 2 + assert got["시간당"]["계"] > first["시간당"]["계"] + assert all(v[0] is other for v in mh._BRIEF.values()) + monkeypatch.setattr(m01, "loaded", lambda: base) + assert mh.brief([key])[0] == first