"""FUENTES-A-LA-CADENA · comprobación mecánica del paquete INC (no es la lectura): con las reglas del PROMPT sobre los originales ya enmascarados de para_lector, ¿la tabla de cada caso cuadra? Esperado: cabeza y positivos sí; negativos no. Solo recuentos. Host, biblioteca estándar.""" import csv, json from pathlib import Path X = Path(__file__).resolve().parent; PL = X / "lotes/inc/para_lector" def ins(s): u = (s or "").strip().upper() return "subvencion" if u.startswith("SUBVENCI") else "prestamo" if u in ("PRÉSTAMO", "PRESTAMO") else "garantia" if u.startswith("GARANT") else "ventaja_fiscal" if u == "VENTAJA FISCAL" else "otros" mapa = {int(r["ref"]): r for r in csv.DictReader(open(X / "lotes/inc/mapa/grupos.tsv", encoding="utf-8"), delimiter="\t")} sel = json.load(open(X / "priv/sel_inc.json"))["casos"] res = {"cabeza": [0, 0], "positivo": [0, 0], "negativo": [0, 0]} for ref, m in mapa.items(): d = PL / "originales" / f"{ref}_1" ret = {l.strip() for l in open(d / "retiradas.txt", encoding="utf-8") if l.strip()} vistos, cel = set(), {} for f in ("bdns_concesiones_copia.jsonl", "bdns_concesiones_incremental.jsonl"): for l in open(d / f, encoding="utf-8"): x = json.loads(l) if not ("2022" <= str(x["fechaConcesion"]) < "2027") or str(x.get("id")) in ret or x.get("codConcesion") in vistos: continue vistos.add(x.get("codConcesion")) k = (int(str(x["fechaConcesion"])[:4]), ins(x.get("instrumento"))); o = cel.setdefault(k, [0, None, None]); o[0] += 1 if x.get("importe") not in (None, ""): o[1] = (o[1] or 0) + float(x["importe"]) if x.get("ayudaEquivalente") not in (None, ""): o[2] = (o[2] or 0) + float(x["ayudaEquivalente"]) caso = next(c for c in sel if c["nif"] == m["clave"] and (c["grupo"], c["subgrupo"]) == (m["grupo"], m["subgrupo"])) afi = {(x["anio"], x["instrumento"]): [x["n"], x["importe"], x["equivalente"]] for x in caso["celdas"]} ig = lambda a, b: a[0] == b[0] and all((p is None and q is None) or (p is not None and q is not None and abs(p - q) <= 0.01) for p, q in zip(a[1:], b[1:])) ok = set(afi) == set(cel) and all(ig(afi[k], cel[k]) for k in afi) g = "cabeza" if m["grupo"] == "cabeza" else m["subgrupo"] res[g][0] += ok; res[g][1] += 1 print(res)