Jakub
Add fuzzy near-match suggestions for numeric columns
fe0d1b4
Raw History Blame Contribute Delete
5.34 kB
"""Configuration models for table comparison."""
from __future__ import annotations
import dataclasses
import json
from dataclasses import asdict, dataclass, field
VALID_TYPES = ("text", "number", "date")
DATE_FORMAT_EXAMPLES: list[tuple[str, str]] = [
("%Y-%m-%d", "2024-01-15"),
("%d/%m/%Y", "15/01/2024"),
("%d/%m/%y", "15/01/24"),
("%m/%d/%Y", "01/15/2024"),
("%m/%d/%y", "01/15/24"),
("%d-%m-%Y", "15-01-2024"),
("%d-%m-%y", "15-01-24"),
("%m-%d-%Y", "01-15-2024"),
("%Y/%m/%d", "2024/01/15"),
("%d.%m.%Y", "15.01.2024"),
("%d.%m.%y", "15.01.24"),
("%Y%m%d", "20240115"),
]
@dataclass
class ColumnMapping:
"""Describes how one column in Table A maps to one column in Table B."""
column_a: str
column_b: str
col_type: str = "text" # "text" | "number" | "date"
date_format_a: str | None = None # strftime pattern for Table A (date only)
date_format_b: str | None = None # strftime pattern for Table B (date only)
rounding: int | None = None # decimal places (number only)
case_sensitive: bool = True # text only
decimal_separator_a: str = "." # "." or "," for Table A (number only)
decimal_separator_b: str = "." # "." or "," for Table B (number only)
margin: float | None = None # absolute tolerance for near-match (number only)
# ------------------------------------------------------------------
# validation
# ------------------------------------------------------------------
def validate(self) -> list[str]:
errors: list[str] = []
if self.col_type not in VALID_TYPES:
errors.append(
f"Invalid type '{self.col_type}' for column '{self.column_a}'. "
f"Must be one of: {VALID_TYPES}"
)
if self.col_type == "date":
if not self.date_format_a:
errors.append(
f"date_format_a is required for date column '{self.column_a}'"
)
if not self.date_format_b:
errors.append(
f"date_format_b is required for date column '{self.column_b}'"
)
if self.col_type == "number":
if self.rounding is not None and self.rounding < 0:
errors.append(f"Rounding for '{self.column_a}' must be >= 0")
if self.decimal_separator_a not in (".", ","):
errors.append(
f"decimal_separator_a for '{self.column_a}' must be '.' or ',', "
f"got '{self.decimal_separator_a}'"
)
if self.decimal_separator_b not in (".", ","):
errors.append(
f"decimal_separator_b for '{self.column_b}' must be '.' or ',', "
f"got '{self.decimal_separator_b}'"
)
if self.margin is not None and self.margin < 0:
errors.append(f"Margin for '{self.column_a}' must be >= 0")
if self.margin is not None and self.col_type != "number":
errors.append(
f"Margin is only valid for number columns, "
f"but '{self.column_a}' has type '{self.col_type}'"
)
return errors
# ------------------------------------------------------------------
# serialisation
# ------------------------------------------------------------------
def to_dict(self) -> dict:
return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> ColumnMapping:
known = {f.name for f in dataclasses.fields(cls)}
return cls(**{k: v for k, v in data.items() if k in known})
@dataclass
class ComparisonConfig:
"""Top-level configuration: paths + list of column mappings."""
table_a_path: str = ""
table_b_path: str = ""
columns: list[ColumnMapping] = field(default_factory=list)
# ------------------------------------------------------------------
# validation
# ------------------------------------------------------------------
def validate(self) -> list[str]:
errors: list[str] = []
if not self.columns:
errors.append("At least one column mapping is required.")
for col in self.columns:
errors.extend(col.validate())
return errors
# ------------------------------------------------------------------
# serialisation
# ------------------------------------------------------------------
def to_dict(self) -> dict:
return {
"table_a_path": self.table_a_path,
"table_b_path": self.table_b_path,
"columns": [c.to_dict() for c in self.columns],
}
@classmethod
def from_dict(cls, data: dict) -> ComparisonConfig:
return cls(
table_a_path=data.get("table_a_path", ""),
table_b_path=data.get("table_b_path", ""),
columns=[ColumnMapping.from_dict(c) for c in data.get("columns", [])],
)
def save(self, path: str) -> None:
with open(path, "w") as fh:
json.dump(self.to_dict(), fh, indent=2)
@classmethod
def load(cls, path: str) -> ComparisonConfig:
with open(path) as fh:
return cls.from_dict(json.load(fh))