|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import json |
| 5 | +import re |
| 6 | +from decimal import Decimal, InvalidOperation |
| 7 | +from typing import Any, Iterable |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | + |
| 12 | +MISSING_STRINGS = {"", "nan", "none", "null", "nat"} |
| 13 | + |
| 14 | + |
| 15 | +def is_missing(value: Any) -> bool: |
| 16 | + if value is None: |
| 17 | + return True |
| 18 | + |
| 19 | + try: |
| 20 | + if pd.isna(value): |
| 21 | + return True |
| 22 | + except (TypeError, ValueError): |
| 23 | + pass |
| 24 | + |
| 25 | + if isinstance(value, str) and value.strip().lower() in MISSING_STRINGS: |
| 26 | + return True |
| 27 | + |
| 28 | + return False |
| 29 | + |
| 30 | + |
| 31 | +def normalize_reference(value: Any) -> str | None: |
| 32 | + """Normalize transaction references for matching. |
| 33 | +
|
| 34 | + Example: |
| 35 | + " ref-000123 " -> "REF000123" |
| 36 | + """ |
| 37 | + if is_missing(value): |
| 38 | + return None |
| 39 | + |
| 40 | + if isinstance(value, float) and value.is_integer(): |
| 41 | + raw_value = str(int(value)) |
| 42 | + else: |
| 43 | + raw_value = str(value) |
| 44 | + |
| 45 | + normalized = re.sub(r"[^A-Za-z0-9]", "", raw_value.strip()).upper() |
| 46 | + return normalized or None |
| 47 | + |
| 48 | + |
| 49 | +def parse_amount(value: Any) -> float | None: |
| 50 | + """Parse amount values into numeric form. |
| 51 | +
|
| 52 | + Supports common demo formats such as: |
| 53 | + "1,250.50" |
| 54 | + "$1,250.50" |
| 55 | + "(1,250.50)" |
| 56 | + """ |
| 57 | + if is_missing(value): |
| 58 | + return None |
| 59 | + |
| 60 | + if isinstance(value, (int, float, Decimal)) and not isinstance(value, bool): |
| 61 | + return float(value) |
| 62 | + |
| 63 | + text = str(value).strip() |
| 64 | + is_negative_parentheses = text.startswith("(") and text.endswith(")") |
| 65 | + |
| 66 | + if is_negative_parentheses: |
| 67 | + text = text[1:-1] |
| 68 | + |
| 69 | + text = ( |
| 70 | + text.replace("$", "") |
| 71 | + .replace(",", "") |
| 72 | + .replace("CAD", "") |
| 73 | + .replace("USD", "") |
| 74 | + .replace(" ", "") |
| 75 | + .strip() |
| 76 | + ) |
| 77 | + |
| 78 | + try: |
| 79 | + amount = Decimal(text) |
| 80 | + except InvalidOperation: |
| 81 | + return None |
| 82 | + |
| 83 | + if is_negative_parentheses: |
| 84 | + amount = -amount |
| 85 | + |
| 86 | + return float(amount) |
| 87 | + |
| 88 | + |
| 89 | +def parse_date(value: Any) -> str | None: |
| 90 | + """Parse a date-like value into ISO format: YYYY-MM-DD.""" |
| 91 | + if is_missing(value): |
| 92 | + return None |
| 93 | + |
| 94 | + parsed = pd.to_datetime(value, errors="coerce") |
| 95 | + |
| 96 | + if pd.isna(parsed): |
| 97 | + return None |
| 98 | + |
| 99 | + return parsed.date().isoformat() |
| 100 | + |
| 101 | + |
| 102 | +def _stable_string(value: Any) -> str: |
| 103 | + if is_missing(value): |
| 104 | + return "" |
| 105 | + |
| 106 | + if isinstance(value, pd.Timestamp): |
| 107 | + return value.isoformat() |
| 108 | + |
| 109 | + return str(value).strip() |
| 110 | + |
| 111 | + |
| 112 | +def build_row_hash(row: dict[str, Any] | pd.Series, fields: Iterable[str] | None = None) -> str: |
| 113 | + """Create a deterministic hash for row-level traceability.""" |
| 114 | + row_dict = row.to_dict() if isinstance(row, pd.Series) else dict(row) |
| 115 | + |
| 116 | + if fields is not None: |
| 117 | + payload = {field: _stable_string(row_dict.get(field)) for field in fields} |
| 118 | + else: |
| 119 | + payload = {key: _stable_string(value) for key, value in row_dict.items()} |
| 120 | + |
| 121 | + serialized = json.dumps(payload, sort_keys=True, ensure_ascii=False) |
| 122 | + return hashlib.sha256(serialized.encode("utf-8")).hexdigest() |
0 commit comments