"""Filtros sobre m2_base (sin nombres). Salida: salidas/26_filtros.json""" import json, sys, importlib.util, collections sp=importlib.util.spec_from_file_location('est','/x-graficos/tanda-4nov/sancionadas/py/25_est.py'); est=importlib.util.module_from_spec(sp); sp.loader.exec_module(est) R=est.carga('base') M=[u for u in R if not u['corta'] and u['pn'] and u['pn']>=1] C=[u for u in R if u['corta']] XX='expediente excluido (ver nota)' out={} def fila(nom,U,k='n'): l=est.linea(U,k,nom); out[nom+'|'+k]=l; print(json.dumps(l,ensure_ascii=False)[:330]) for k in ('n','e','nm','p'): fila('principal',M,k) fila('sin_excluido',[u for u in M if u['exp']!=XX],k) fila('solo_firmes',[u for u in M if u['estado'] in ('F','FP')]); fila('firmes_sin_excluido',[u for u in M if u['estado'] in ('F','FP') and u['exp']!=XX]) fila('solo_recurridas',[u for u in M if u['estado']=='R']); fila('sin_dato',[u for u in M if u['estado']=='X']) fila('solo_cnmc',[u for u in M if u['aut']=='CNMC']); fila('solo_cnmc_sinexcluido',[u for u in M if u['aut']=='CNMC' and u['exp']!=XX]) fila('antes_2021',[u for u in M if u['t0']<'2021-01-01']); fila('desde_2021',[u for u in M if u['t0']>='2021-01-01']) may=max(M,key=lambda u:u['qn']); fila('sin_mayor_n',[u for u in M if u is not may]) for k in ('n','e'): mm=max(M,key=lambda u:u[est.PAR[k][1]]); fila('sin_mayor_'+k,[u for u in M if u is not mm],k) # Unidad con mayor PRE en euros mp=max(M,key=lambda u:u['pe']); fila('sin_mayor_pre_e',[u for u in M if u is not mp],'e') # Ventana corta for e in ('S/0001/23','CGC 18/2025'): U=[u for u in C if u['exp']==e and u['pn'] and u['pn']>=1] for k in ('n','e','p','nm','m'): if U: O,E,P=est.oe(U,k); print(e,k,'unidades',len(U),'O',round(O),'P',round(P),'E',round(E),'OE',round(O/E,3) if E else None,'bruto',round(O/P,3) if P else None,'g',round(E/P,3) if P else None); out[e+'|'+k]=dict(unidades=len(U),O=O,P=P,E=E) # Por expediente print('--- por expediente') g=collections.defaultdict(list) for u in M: g[u['exp']].append(u) pe=[] for e,U in sorted(g.items(),key=lambda x:x[0]): O,E,P=est.oe(U,'n'); O2,E2,P2=est.oe(U,'e') pe.append(dict(exp=e,unidades=len(U),post=O,pre=P,esp=round(E,1),OE=round(O/E,2) if E else None,epost=round(O2/1e6,1),epre=round(P2/1e6,1),eesp=round(E2/1e6,1),OEe=round(O2/E2,2) if E2 else None)) print(pe[-1]) out['por_exp']=pe json.dump(out,open('/x-graficos/tanda-4nov/sancionadas/salidas/26_filtros.json','w'),ensure_ascii=False,indent=1)