"""ETIQ-3 · E9 · impl. 1 · banco ayuntamiento-año (DEFINICIONES_PREVIAS_E9 §1). Censo DIR3 sin PV/Navarra; etiqueta del Anexo 4 (TSV casados de DAT-2); identidad dudosa fuera. Uso (host): python3 -I e9_banco_i1.py """ import csv, sys, re, unicodedata, difflib, collections AN, REF, OUT = sys.argv[1:4] FUERA_PROV = {'01', '20', '48', '31'} CAT = {'08', '17', '25', '43'} CAT_NA = {2017, 2018, 2019, 2020, 2021, 2023} def norm(s): s = unicodedata.normalize('NFD', (s or '').upper()) s = ''.join(c for c in s if unicodedata.category(c) != 'Mn') s = re.sub(r'^(AYUNTAMIENTO|AJUNTAMENT|CONCELLO|AYUNTAMIENTO DEL?|AJUNTAMENT D)\b', '', s) s = re.sub(r'\b(DE|DEL|D|LA|EL|LOS|LAS|L|ELS|LES|O|A|OS|AS|SA|ES|SES)\b', ' ', s) return re.sub(r'[^A-Z0-9]+', '', s) cen = [r for r in csv.DictReader(open(f'{REF}/censo_ay_dir3.tsv'), delimiter='\t') if r['prov'] not in FUERA_PROV] pad = {r['ine']: r for r in csv.DictReader(open(f'{REF}/padron_municipios_2019_2025.csv'))} def pob(ine, ej): r = pad.get(ine) if not r: return None for y in [max(ej, 2019)] + list(range(2019, 2026)): v = r.get(f'poblacion_{y}') if v: return int(v) return None def tramo(p): if p is None: return '' return '1:<=1000' if p <= 1000 else '2:1001-5000' if p <= 5000 else '3:5001-20000' if p <= 20000 else '4:20001-50000' if p <= 50000 else '5:>50000' filas, todas, stats = [], [], collections.Counter() for ej in range(2017, 2025): an = list(csv.DictReader(open(f'{AN}/anexo_casado_{ej}.tsv'), delimiter='\t')) for x in an: if x['nif']: todas.append({'ejercicio': ej, 'tipo': x['tipo'], 'entidad': x['entidad'], 'provincia': x['provincia'], 'nif': x['nif'], 'contratos': x['contratos'], 'reparos': x['reparos'], 'pagina': x['pagina']}) ay = [x for x in an if x['tipo'] == 'AYUNTAMIENTO'] lab = {x['nif']: x for x in ay if x['casado'] == 'unico' and x['nif']} dudosas = [x for x in ay if x['casado'] != 'unico' or not x['nif']] excl = set() for x in dudosas: nx = norm(x['entidad']) for c in cen: if c['prov'] != x['prov_ine'] or c['nif'] in lab: continue nc = norm(c['nombre']) if nx and nc and (difflib.SequenceMatcher(None, nx, nc).ratio() >= 0.80 or nx in nc or nc in nx): excl.add(c['nif']) for c in cen: if not c['nif']: stats[(ej, 'censo sin NIF')] += 1 continue p = pob(c['ine'], ej) x = lab.get(c['nif']) est = x['contratos'] if x else 'no_listado' rep = x['reparos'] if x else 'no_listado' if c['prov'] in CAT and ej in CAT_NA: y = ys = None; motivo = 'cataluna_NA' elif x is None and c['nif'] in excl: y = ys = None; motivo = 'identidad_dudosa' else: motivo = '' y = 1 if est == 'NO' else (0 if est in ('SI', 'no_listado') else None) ys = 1 if est in ('NO', 'NO*') else (0 if est in ('SI', 'no_listado') else None) yr = 1 if rep == 'NO' else (0 if rep in ('SI', 'no_listado') else None) if motivo: yr = None filas.append({'ejercicio': ej, 'nif': c['nif'], 'ine': c['ine'], 'dir3': c['dir3'], 'nombre': c['nombre'], 'prov': c['prov'], 'cod_ca': c['cod_ca'], 'ccaa': c['ccaa'], 'poblacion': p, 'tramo': tramo(p), 'estado_contratos': est, 'estado_reparos': rep, 'motivo_fuera': motivo, 'y_nr': y, 'y_nrstar': ys, 'y_rep': yr}) stats[(ej, motivo or ('NR' if y == 1 else 'neg' if y == 0 else 'fuera:' + est))] += 1 stats[(ej, 'anexo_ay_dudosas')] = len(dudosas) stats[(ej, 'excluidas_por_dudosa')] = len(excl) cols = list(filas[0].keys()) with open(f'{OUT}/banco_e9_i1.tsv', 'w', newline='') as fh: w = csv.DictWriter(fh, cols, delimiter='\t'); w.writeheader(); w.writerows(filas) with open(f'{OUT}/anexo_todas_i1.tsv', 'w', newline='') as fh: w = csv.DictWriter(fh, list(todas[0].keys()), delimiter='\t'); w.writeheader(); w.writerows(todas) for ej in range(2017, 2025): print(ej, {k[1]: v for k, v in sorted(stats.items()) if k[0] == ej})