This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
leer_puestos.py
executable file
·107 lines (91 loc) · 3.09 KB
/
leer_puestos.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
#!/usr/bin/env python3
import os
import re
import sys
import textwrap
import requests
import xlrd
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
re_etiqueta = re.compile(r"^(\S+)\s+\[(\d+),\s*(\d+)\]\s*$")
re_space = re.compile(r" +")
re_number = re.compile(r"^\d+,\d+$")
if not os.path.isfile(".ig_leer_puestos"):
sys.exit(textwrap.dedent('''
Ha de crear un fichero .ig_leer_puestos con el siguiente formato
URL al excel de dropbox
Tu posición
etiqueta1 [puestoA, puestoZ]
etiqueta2 [puestoA, puestoZ]
etiqueta_por_defecto
Ejemplo:
https://www.dropbox.com/s/********/***************.xlsx?dl=0
28
giss [246, 268]
sanidad [304, 310]
cultura [125, 130]
aeat [19, 90]
''').strip())
def parse(cell, parse_number=True):
if not cell:
return None
v = cell.value
if isinstance(v, float):
return int(v) if v.is_integer() else v
if isinstance(v, str):
v = v.strip()
v = re_space.sub(" ", v)
if parse_number and re_number.match(v):
v = float(v.replace(",", "."))
return int(v) if v.is_integer() else v
return v if len(v) else None
return v
config = {}
with open(".ig_leer_puestos") as f:
lineas = [l.strip() for l in f.readlines() if l.strip()]
config['URL'] = lineas[0]
config['PUESTO'] = int(lineas[1])
config['ETIQUETAS'] = {}
for e in lineas[2:]:
e, ini, fin = re_etiqueta.match(e).groups()
config['ETIQUETAS'][e] = list(range(int(ini), int(fin)+1))
if len(sys.argv) > 1 and os.path.isfile(sys.argv[1]):
wb = xlrd.open_workbook(sys.argv[1])
else:
r = requests.get(config['URL']+"&raw=1")
wb = xlrd.open_workbook(file_contents=r.content)
sh = wb.sheet_by_index(2)
posibilidades = {k: (0, 0) for k in config['ETIQUETAS'].keys()}
orden = []
pesimista = set()
for rx in range(1, sh.nrows):
row = [parse(c) for c in sh.row(rx)]
if row[0] == config['PUESTO']:
print("Faltan %s por delante" % row[2])
asignacion = row[1]
for r in row[3:]:
if r is not None and r > 0:
r = int(r)
ok = False
for k, v in config['ETIQUETAS'].items():
if r in v:
pesi = posibilidades[k][1]
posibilidades[k] = (
posibilidades[k][0]+1, pesi+1 if r not in pesimista else pesi)
ok = True
if k not in orden:
orden.append(k)
if not ok:
print("Puesto no encontrado en las etiquetas: %s" % r)
s_max = max([len(s) for s in orden])
for o in orden:
print(("%"+str(s_max)+"s %s") % (o, posibilidades[o]))
sys.exit()
else:
count = 0
m_count = 3 # 5 if row[2] is None else max(5, row[2]-5)
for i in row[3:]:
if i is not None and i > 0 and count < m_count:
pesimista.add(int(i))
count = count + 1