Spaces:
Runtime error
Runtime error
File size: 5,339 Bytes
531d507 b712d55 531d507 b712d55 531d507 b712d55 531d507 b712d55 531d507 0c971d1 fe0d1b4 531d507 0c971d1 fe0d1b4 531d507 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | """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))
|