refactor: декомпозиция монолита в пакет smeta-railway
- Пакетная структура src/smeta_railway/ с модулями calc/, data/, output/ - Pydantic-конфиг: всё через YAML, сметчик не трогает Python - EstimateCalculator: ядро расчёта, параметризованные коэффициенты - FGISLookup: pickle cache (4.7с → ~50мс при повторном запуске) - CLI: smeta generate/fetch-resources/validate (typer) - 28 тестов (coefficients, overhead, fgis, estimate) - Исправлен баг: НР/СП для земляных работ (92%/46% вместо 109%/65%) - ПЗ совпадают бит-в-бит со старым генератором Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
"""
|
||||
FGIS CS (ФГИС ЦС) lookup — indices, base prices, current prices.
|
||||
Loads from fgis_split_form.xlsx with pickle cache for fast restarts.
|
||||
"""
|
||||
|
||||
import os
|
||||
import pickle
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
import openpyxl
|
||||
|
||||
|
||||
@dataclass
|
||||
class FGISLookup:
|
||||
"""Lookup tables built from FGIS split form."""
|
||||
index_map: dict[str, float] = field(default_factory=dict)
|
||||
base_price_map: dict[str, float] = field(default_factory=dict)
|
||||
current_price_map: dict[str, float] = field(default_factory=dict)
|
||||
labor_price_map: dict[str, float] = field(default_factory=dict)
|
||||
|
||||
def get_index(self, code: str) -> float | None:
|
||||
return self.index_map.get(code)
|
||||
|
||||
def get_base_price(self, code: str, fallback: float = 0) -> float:
|
||||
return self.base_price_map.get(code, fallback)
|
||||
|
||||
def get_current_price(self, code: str) -> float | None:
|
||||
return self.current_price_map.get(code)
|
||||
|
||||
def get_labor_price(self, code: str, default: float = 545.28) -> float:
|
||||
return self.labor_price_map.get(code, default)
|
||||
|
||||
|
||||
def _parse_float(val) -> float | None:
|
||||
if val is None or val == "-":
|
||||
return None
|
||||
try:
|
||||
return float(val)
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def build_fgis_lookup(xlsx_path: str) -> FGISLookup:
|
||||
"""Parse fgis_split_form.xlsx and build lookup tables."""
|
||||
wb = openpyxl.load_workbook(xlsx_path, read_only=True)
|
||||
ws = wb.active
|
||||
|
||||
lookup = FGISLookup()
|
||||
|
||||
for row in ws.iter_rows(min_row=1, values_only=True):
|
||||
code = str(row[0]).strip() if row[0] else ""
|
||||
if not code or len(code) < 5:
|
||||
continue
|
||||
|
||||
# Labor codes: 1-100-XX or 4-100-XXX
|
||||
if code.startswith("1-100-") or code.startswith("4-100-"):
|
||||
price = _parse_float(row[7])
|
||||
if price is not None:
|
||||
lookup.labor_price_map[code] = price
|
||||
continue
|
||||
|
||||
# Machine codes (91.xx)
|
||||
if code.startswith("91."):
|
||||
bp = _parse_float(row[4])
|
||||
if bp is not None:
|
||||
lookup.base_price_map[code] = bp
|
||||
cp = _parse_float(row[7])
|
||||
if cp is not None:
|
||||
lookup.current_price_map[code] = cp
|
||||
idx = _parse_float(row[8])
|
||||
if idx is not None:
|
||||
lookup.index_map[code] = idx
|
||||
continue
|
||||
|
||||
# Material codes
|
||||
for bp_col in [3, 4]:
|
||||
bp = _parse_float(row[bp_col])
|
||||
if bp is not None:
|
||||
lookup.base_price_map[code] = bp
|
||||
break
|
||||
cp = _parse_float(row[7])
|
||||
if cp is not None:
|
||||
lookup.current_price_map[code] = cp
|
||||
idx = _parse_float(row[8])
|
||||
if idx is not None:
|
||||
lookup.index_map[code] = idx
|
||||
|
||||
wb.close()
|
||||
return lookup
|
||||
|
||||
|
||||
def load_fgis(xlsx_path: str, cache_path: str | None = None) -> FGISLookup:
|
||||
"""
|
||||
Load FGIS lookup with pickle cache.
|
||||
|
||||
If cache_path is provided and exists and is newer than xlsx,
|
||||
loads from pickle (~50ms). Otherwise parses xlsx and saves cache.
|
||||
"""
|
||||
if cache_path and os.path.exists(cache_path):
|
||||
xlsx_mtime = os.path.getmtime(xlsx_path)
|
||||
cache_mtime = os.path.getmtime(cache_path)
|
||||
if cache_mtime > xlsx_mtime:
|
||||
with open(cache_path, "rb") as f:
|
||||
return pickle.load(f)
|
||||
|
||||
t0 = time.monotonic()
|
||||
lookup = build_fgis_lookup(xlsx_path)
|
||||
elapsed = time.monotonic() - t0
|
||||
print(f"FGIS loaded in {elapsed:.2f}s: "
|
||||
f"{len(lookup.index_map)} indices, "
|
||||
f"{len(lookup.base_price_map)} base prices, "
|
||||
f"{len(lookup.current_price_map)} current prices, "
|
||||
f"{len(lookup.labor_price_map)} labor prices")
|
||||
|
||||
if cache_path:
|
||||
with open(cache_path, "wb") as f:
|
||||
pickle.dump(lookup, f)
|
||||
|
||||
return lookup
|
||||
|
||||
|
||||
def get_labor_code(razryad: float, prefix: str = "1-100-") -> str:
|
||||
"""Build labor code from razryad: 3.0 -> '1-100-30', 5.0 -> '4-100-050'."""
|
||||
r = int(round(razryad * 10))
|
||||
if prefix == "4-100-":
|
||||
return f"4-100-0{r:02d}"
|
||||
return f"1-100-{r:02d}"
|
||||
|
||||
|
||||
def get_labor_current_price(
|
||||
fgis: FGISLookup,
|
||||
razryad: float,
|
||||
is_machinist: bool = False,
|
||||
) -> float:
|
||||
"""Get current labor price from FGIS lookup."""
|
||||
prefix = "4-100-" if is_machinist else "1-100-"
|
||||
code = get_labor_code(razryad, prefix)
|
||||
return fgis.get_labor_price(code)
|
||||
Reference in New Issue
Block a user