"""FTS-REPARTO · comprobación mecánica del paquete de la micro-lectura (no es la lectura; copia de fuentes-a-la-cadena/lectura/comprobar_ue.py, solo FTS): con las reglas del PROMPT sellado sobre los originales ya enmascarados de para_lector, ¿la afirmación de cada caso cuadra? Esperado: cabeza y positivos sí; negativos no. Con --reglas-13nov, las reglas de antes (sin la frase sola ni el importe sin divulgar): la cabeza tiene que caer (el paquete distingue). Solo recuentos. Host, biblioteca estándar. python3 comprobar_reparto.py [--reglas-13nov]""" import csv import json import sys from collections import Counter, defaultdict from pathlib import Path X = Path(__file__).resolve().parent PL = X / "lotes/reparto/para_lector" VIEJAS = "--reglas-13nov" in sys.argv SR = ("100 % of the amount is displayed with the coordinator", "attributed to a single beneficiary", "divided by the number of beneficiaries") + \ (() if VIEJAS else ("repartition for each beneficiary was not available",)) NO_DIVULGADO = None if VIEJAS else "the contract amount will remain undisclosed" RENOMBRES = {"1.1.5 - Marco Polo": "1.1.5 - Marco-Polo"} def num(s): s = (s or "").strip().replace(",", "") if not s: return None try: return float(s) except ValueError: # «*****»: la Comisión no publica ese importe return None def suma(xs): xs = [x for x in xs if x is not None] return round(sum(xs), 2) if xs else None mapa = {int(r["ref"]): r for r in csv.DictReader(open(X / "lotes/reparto/mapa/grupos.tsv", encoding="utf-8"), delimiter="\t")} sel = json.load(open(X / "priv/sel_reparto.json", encoding="utf-8"))["casos"] res = {"cabeza": [0, 0], "positivo": [0, 0], "negativo": [0, 0]} for ref, m in mapa.items(): cel = {} lin = defaultdict(list) for f in sorted((PL / "originales" / f"{ref}_1").glob("fts_*.csv")): for r in csv.DictReader(open(f, encoding="utf-8")): lin[int(r["Year"])].append(r) for a, rs in lin.items(): ia = next(k for k in rs[0] if k.startswith("Beneficiary") and k.endswith("s contracted amount (EUR)")) fu = lambda r: (r["Source of (estimated) detailed amount"] or "").lower() # noqa: E731 imp = lambda r: None if NO_DIVULGADO and NO_DIVULGADO in fu(r) else num(r[ia]) # noqa: E731 srs = [r for r in rs if any(s.lower() in fu(r) for s in SR)] crs = [r for r in rs if r not in srs] pr = Counter((r["Programme name"] or "").strip() or "sin programa" for r in rs) cel[a] = (len(rs), suma(imp(r) for r in crs), len(srs), suma(imp(r) for r in srs), "|".join(f"{p}:{k}" for p, k in sorted(pr.items(), key=lambda x: (-x[1], x[0])))) caso = next(c for c in sel if c["nif"] == m["clave"] and (c["grupo"], c["subgrupo"]) == (m["grupo"], m["subgrupo"])) afi = {} for x in caso["celdas"]: pg = "|".join(f"{RENOMBRES.get(p, p)}:{k}" for p, k in (y.rsplit(":", 1) for y in (x["programas"] or "").split("|") if ":" in y)) afi[x["anio"]] = (x["n"], round(x["importe"], 2) if x["importe"] is not None else None, x["sin_reparto"], round(x["importe_sin_reparto"], 2) if x["importe_sin_reparto"] is not None else None, pg) ig = lambda a, b: all((p is None and q is None) or (isinstance(p, float) and q is not None and abs(p - q) <= 0.01) or p == q for p, q in zip(a, b)) # noqa: E731 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(("reglas del 13-nov: " if VIEJAS else "") + json.dumps(res))