-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresumen.py
144 lines (127 loc) · 5.78 KB
/
resumen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from pymongo import MongoClient, ASCENDING
import csv
def make_cand_resumen(cand_dict):
datosPersonales = cand_dict["datosPersonales"]
postulacion = cand_dict["postulacion"]
nacimiento = cand_dict["nacimiento"]
residencia = cand_dict["residencia"]
experiencia = cand_dict["experiencia"] if cand_dict["experiencia"] else []
civil = cand_dict["civil"] if cand_dict["civil"] else []
penal = cand_dict["penal"] if cand_dict["penal"] else []
ingresos = cand_dict["ingresos"]
if cand_dict["bienes"]:
muebles = cand_dict["bienes"]["muebles"]
inmuebles = cand_dict["bienes"]["inmuebles"]
if muebles == None:
muebles = []
if inmuebles == None:
inmuebles = []
else:
muebles = []
inmuebles = []
if cand_dict["educacionBasica"]:
primaria = cand_dict["educacionBasica"]["primaria"]
secundaria = cand_dict["educacionBasica"]["secundaria"]
if primaria == None:
primaria = []
if secundaria == None:
secundaria = []
else:
primaria = []
secundaria = []
if cand_dict["educacionSuperior"]:
universitario = cand_dict["educacionSuperior"]["universitario"] if cand_dict["educacionSuperior"]["universitario"] else []
postgrado_dirty = cand_dict["educacionSuperior"]["postgrado"] if cand_dict["educacionSuperior"]["postgrado"] else []
tecnico = cand_dict["educacionSuperior"]["tecnico"] if cand_dict["educacionSuperior"]["tecnico"] else []
else:
postgrado_dirty = []
tecnico = []
universitario = []
postgrado = []
for item in postgrado_dirty:
if item["tipo"] == None and item["gradoTitulo"]=='':
dict_prueba_post = { key:item[key]
for key in ["especialidad", "fin","inicio","instEducativa","pais"]}
list_prueba_tec = [{ key:d[key]
for key in ["especialidad",
"fin",
"inicio",
"instEducativa",
"pais"]} for d in tecnico]
if dict_prueba_post not in list_prueba_tec:
postgrado.append(item)
else:
postgrado.append(item)
ed_concluido = lambda ed_list: reduce(lambda x,y: x or y,
map(lambda d: d["concluido"],ed_list), 0) if ed_list else ""
educacion_basica_dict = {
"primaria_concluida": ed_concluido(primaria),
"secundaria_concluida": ed_concluido(secundaria),
}
educacion_superior_dict = {
"tecnico_concluido": ed_concluido(tecnico),
"universitario_concluido": ed_concluido(universitario),
"postgrado_concluido": ed_concluido(postgrado),
}
suma_concluido = lambda ed_list: sum(map (lambda d: (d["fin"] if d["fin"] else 2014) - d["inicio"] + 1
if d["concluido"] else 0,
ed_list))
n_empleos_sector = lambda sector: sum(map (lambda d: 1
if d["sector"].lower() == sector.lower()
else 0, experiencia))
tiempo_sector = lambda sector: sum(map (lambda d: (d["fin"] if d["fin"] != "AHORA" else 2014) - d["inicio"]
if d["sector"].lower() == sector
else 0, experiencia))
res_dict = {
"id_candidato": cand_dict["_id"],
"dni": str(datosPersonales ["dni"]).zfill(8),
"postulacion_cargo": postulacion["cargo"],
"nacimiento_anio": nacimiento["fecha"]["y"],
"edad": 2014 - nacimiento["fecha"]["y"],
"postulacion_ubigeo": postulacion["ubigeo"],
"residencia_tiempo": residencia["tiempo"],
"educacion_basica_suma": suma_concluido(primaria + secundaria),
"educacion_superior_suma": suma_concluido(universitario + tecnico + postgrado),
"empleos_sector_privado": n_empleos_sector("privado"),
"empleos_sector_publico": n_empleos_sector(u"p\xfablico"), # ._.
"tiempo_sector_privado": tiempo_sector("privado"),
"tiempo_sector_publico": tiempo_sector(u"p\xfablico"), # ._.
"penal_o_civil": 1 if len(penal) + len(civil) > 0 else 0,
"ingreso_total": ingresos["total"],
"total_bienes": sum(map (lambda d: d["valor"], muebles + inmuebles)),
}
res_dict.update(educacion_basica_dict)
res_dict.update(educacion_superior_dict)
return res_dict
def get_resumen_all(db, coleccion, client=MongoClient(), ):
cursor = client[db][coleccion].find({"ok":True}).sort("_id", ASCENDING)
return ( make_cand_resumen(cand_dict) for cand_dict in cursor)
def write_resumen(filename = "resumen.csv", **mongoOptions):
with open(filename, 'w') as csvfile:
fieldnames = [
'id_candidato',
'dni',
'nacimiento_anio',
'edad',
'postulacion_cargo',
"postulacion_ubigeo",
'residencia_tiempo',
'penal_o_civil',
'primaria_concluida',
'secundaria_concluida',
'universitario_concluido',
'postgrado_concluido',
'tecnico_concluido',
'educacion_superior_suma',
'educacion_basica_suma',
'empleos_sector_privado',
'empleos_sector_publico',
'tiempo_sector_publico',
'tiempo_sector_privado',
'total_bienes',
'ingreso_total',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for res in get_resumen_all(**mongoOptions):
writer.writerow(res)