- 규칙 한 벌 = common_util_show_digits.ts ↔ master_doc_digits.py(짝 · 거울 시험 test_859_3) 표 값 = 고른 줄의 원문 글자 자리(끝 0 살림 · 서버 표 캐시에 줄마다 `자리` 실음) · 식 값 = Q 3 · n 2 · cm 3(8-1 주①) › 원 단위 · 비목 이름 = 소수 한 자리 › 그 밖 유효숫자 3(소수 2~6) · 입력 · 요소 = 자료 그대로 · 만약으로 고른 기호 하나인 식은 그 값의 갈래를 따름 - 줄 쌓기 오른쪽(공식 줄 · 값 묶음 · 금액 줄 수량 · 단가 식 · 결과) · 왼쪽 값 · 작업량 줄 · M02 계산서 글 - test_845_2 기대 글 고침(노무비 계수 0.208333 → 0.208 — 뜻한 바뀜 · 금액 59,020.2 그대로) - 관련 시험 + 관문 256 통과 · typecheck 0 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
7.1 KiB
Python
162 lines
7.1 KiB
Python
"""859-3 지킴 — 보이는 수 자릿수 규칙 한 쌍
|
|
(TS `common_util_show_digits.ts` ↔ PY `master_doc_digits.py`).
|
|
|
|
① 거울 — 같은 값 · 갈래 · 기호 · 단위 · 표 자리 → 같은 글(헬퍼 `helper_859_3_show_digits.cjs`) ·
|
|
갈래 판정 · 표 값 원문 글자 자리(서버 표 캐시 `자리` → 화면 `tablePlaces`) · 문서 기호 값 글 —
|
|
M01 수식 문서 전부(기본 입력 셈 답).
|
|
② 규칙 결과 — 표 값 끝 0 살림 · 식 품 유효숫자 3 · 원 단위 소수 한 자리 · 8-1 Q 3 자리 ·
|
|
입력 · 요소 그대로.
|
|
③ 그림만 — 서버 산출근거 글의 금액은 셈 답 그대로(글 끝 「= 금액」 = 줄 금액 소수 한 자리).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import json
|
|
import os
|
|
import subprocess
|
|
from decimal import Decimal
|
|
from functools import lru_cache
|
|
|
|
from M01_MasterData import M01_MasterData_Doc as m01_doc
|
|
from M01_MasterData import M01_MasterData_Store as m01
|
|
from M02_MasterTemplete import M02_Logic_Standard as st
|
|
|
|
mc = importlib.import_module("master_doc_calc")
|
|
md = importlib.import_module("master_doc")
|
|
mdd = importlib.import_module("master_doc_digits")
|
|
mdt = importlib.import_module("master_doc_text")
|
|
rec = importlib.import_module("master_doc_record")
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
NUMS = [0, 1, -1, 8, 250, 0.5, 1.5, 2.5, -2.5, 0.05, 0.15, 0.25, 0.35, 1.05, 1.005, 2.675, 99.95,
|
|
-0.05, 0.1 + 0.2, 1e-7, 0.0004, 0.000417, 0.027778, 0.0285, 0.0995, 0.99999, 1.4552,
|
|
0.606061, 33971.813333, 535.396667, 12987.917773, 2446.08, 1234567.891234567,
|
|
59018.0857142857, 284.42142857142, 123456789012.345, 26890.75, 1502.04999,
|
|
0.1, 3.0] # fmt: skip
|
|
KINDS = [("식", "", ""), ("식", "Q", ""), ("식", "n", ""), ("식", "cm", ""), ("식", "", "원/시간"),
|
|
("식", "", "인"), ("표", "", ""), ("입력", "", ""), ("요소", "", "원/인")] # fmt: skip
|
|
|
|
|
|
def _js(v):
|
|
if isinstance(v, Decimal):
|
|
return float(v)
|
|
if isinstance(v, dict):
|
|
return {k: _js(x) for k, x in v.items()}
|
|
if isinstance(v, (list, tuple)):
|
|
return [_js(x) for x in v]
|
|
if isinstance(v, (mc.Blank, mc.Stop)):
|
|
return None
|
|
return v
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def world():
|
|
whole = m01.loaded()[1]
|
|
docs = m01_doc.docs()
|
|
links = st._links(whole)
|
|
answers = {k: mc.calc(d, links, None) for k, d in docs.items() if d.get("금액")}
|
|
return docs, links, answers
|
|
|
|
|
|
def _helper(payload: dict) -> dict:
|
|
run = subprocess.run(
|
|
["node", os.path.join(HERE, "helper_859_3_show_digits.cjs")],
|
|
input=json.dumps(payload, ensure_ascii=False),
|
|
capture_output=True,
|
|
text=True,
|
|
encoding="utf-8",
|
|
check=True,
|
|
)
|
|
return json.loads(run.stdout)
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def mirror():
|
|
docs, links, answers = world()
|
|
nums = [[v, k, n, u, p] for v in NUMS for k, n, u in KINDS for p in (None, 2, 3)]
|
|
kinds, places, tables, py = [], [], {}, {"수": [], "갈래": [], "자리": [], "문서값": []}
|
|
whole = []
|
|
keep = rec.Rec(links)
|
|
for key, a in answers.items():
|
|
doc = docs[key]
|
|
vals = a.get("값") or {}
|
|
look = mdt._Look(doc, a, links)
|
|
shown = {k: _js(v) for k, v in vals.items() if v is not None}
|
|
names = [md.var_id(x) for x in doc.get("변수") or []]
|
|
names += [str(x.get("기호") or "") for x in doc.get("식") or []]
|
|
pairs = [[n, float(vals[n])] for n in names if isinstance(vals.get(n), Decimal)]
|
|
get = links.master.get
|
|
py["문서값"].append([mdd.show_value(doc, n, vals[n], look.value, get) for n, _ in pairs])
|
|
part = {"식": doc.get("식") or [], "변수": _js(doc.get("변수") or [])}
|
|
whole.append({"문서": part, "값들": shown, "기호": pairs})
|
|
for var in doc.get("변수") or []:
|
|
name = md.var_id(var)
|
|
kinds.append({"문서": {"식": doc.get("식") or []}, "이름": name, "변수": _js(var)})
|
|
py["갈래"].append(mdd.kind_of(doc, name, var))
|
|
v = vals.get(name)
|
|
link = var.get("연결") or {}
|
|
if link.get("갈래") != "표" or not isinstance(v, Decimal):
|
|
continue
|
|
ref = str(link.get("표") or "")
|
|
keep.see_table(ref)
|
|
tables[ref] = _js(keep.표[ref])
|
|
places.append({"변수": _js(var), "값": float(v), "값들": shown})
|
|
py["자리"].append(mdd.table_value_places(link, v, look.value, links.master.get))
|
|
nums.append([float(v), "표", name, "", py["자리"][-1]])
|
|
for v, k, n, u, p in nums:
|
|
py["수"].append(mdd.show(v, k, n, u, p))
|
|
every = [v for d in docs.values() for v in d.get("변수") or []]
|
|
refs = {str((v.get("연결") or {}).get("표") or "") for v in every}
|
|
for ref in refs:
|
|
if ref and "{" not in ref and ref not in tables:
|
|
keep.see_table(ref)
|
|
tables[ref] = _js(keep.표[ref])
|
|
ts = _helper({"수": nums, "갈래": kinds, "표": tables, "자리": places, "문서값": whole})
|
|
return nums, py, ts
|
|
|
|
|
|
def test_수_꼴_거울():
|
|
nums, py, ts = mirror()
|
|
assert len(nums) > 7000
|
|
bad = [(n, a, b) for n, a, b in zip(nums, py["수"], ts["수"]) if a != b]
|
|
assert not bad, bad[:10]
|
|
|
|
|
|
def test_갈래_표_자리_거울():
|
|
_, py, ts = mirror()
|
|
assert len(py["갈래"]) > 10000 and py["갈래"] == ts["갈래"]
|
|
assert len(py["자리"]) > 7000 and py["자리"] == ts["자리"]
|
|
assert sum(1 for p in py["자리"] if p) > 500 # 끝 0 글자 자리가 실제로 실려 감
|
|
|
|
|
|
def test_문서_기호_값_글_거울():
|
|
_, py, ts = mirror()
|
|
assert sum(map(len, py["문서값"])) > 15000
|
|
bad = [(a, b) for x, y in zip(py["문서값"], ts["문서값"]) for a, b in zip(x, y) if a != b]
|
|
assert not bad and py["문서값"] == ts["문서값"], bad[:10]
|
|
|
|
|
|
def test_규칙_결과():
|
|
show = mdd.show
|
|
assert show(0.1, "표", "Q_수량^표", "인", 2) == "0.10" # 원문 글자 끝 0
|
|
assert show(0.1, "표") == "0.1" and show(3.0, "표", "", "", 1) == "3.0"
|
|
assert show(0.027778, "식", "Q_보링공", "인") == "0.0278" # 유효숫자 3
|
|
assert show(1.4552, "식") == "1.46" and show(8, "식") == "8"
|
|
assert show(33971.813333, "식", "", "원") == "33,971.8" # 원 단위 한 자리
|
|
assert show(70.12345, "식", "Q") == "70.123" and show(2.456, "식", "n") == "2.46"
|
|
assert show(0.027778, "입력") == "0.027778" and show(1846.39, "요소") == "1,846.39"
|
|
assert show("5401-0017") == "5401-0017" and show(None) == "" and show(1048, "입력") == "1,048"
|
|
|
|
|
|
def test_서버_글_금액_그대로():
|
|
docs, links, answers = world()
|
|
for key in ("GF000220", "GC000994", "GC000996"):
|
|
for line in mdt.money_lines(docs[key], answers[key], links):
|
|
if line.get("금액") is not None:
|
|
assert line["글"].endswith(f"= {mdt.one(line['금액'])} 원")
|
|
got = {x["이름"]: x for x in mdt.money_lines(docs["GC000994"], answers["GC000994"], links)}
|
|
assert got["보링공"]["수량글"] == "0.0278"
|
|
rate = [x for x in mdt.money_lines(docs["GC000996"], answers["GC000996"], links)]
|
|
assert any(x.get("단가글") == "33,971.8" for x in rate)
|