31 lines
1021 B
Python
31 lines
1021 B
Python
def align_records(cobol_records: list[dict], java_records: list[dict],
|
|
key_field: str = "CUST-ID") -> list[tuple]:
|
|
if not cobol_records and not java_records:
|
|
return []
|
|
|
|
def _by(records, kf):
|
|
d = {}
|
|
for r in records:
|
|
key = str(r.get(kf, "__NONE__"))
|
|
d.setdefault(key, []).append(r)
|
|
return d
|
|
|
|
c_by = _by(cobol_records, key_field)
|
|
j_by = _by(java_records, key_field)
|
|
pairs = []
|
|
all_keys = set(c_by) | set(j_by)
|
|
|
|
for k in sorted(all_keys):
|
|
c_items = c_by.get(k, [])
|
|
j_items = j_by.get(k, [])
|
|
for i in range(max(len(c_items), len(j_items))):
|
|
c = c_items[i] if i < len(c_items) else None
|
|
j = j_items[i] if i < len(j_items) else None
|
|
if c and j:
|
|
pairs.append((c, j, "MATCHED"))
|
|
elif c:
|
|
pairs.append((c, None, "MISSING_IN_SPARK"))
|
|
else:
|
|
pairs.append((None, j, "EXTRA_IN_SPARK"))
|
|
return pairs
|