-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogreso.py
More file actions
70 lines (53 loc) · 1.7 KB
/
Copy pathprogreso.py
File metadata and controls
70 lines (53 loc) · 1.7 KB
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
import time
from utilidades import Formateador
class BarraProgreso:
"""
Clase reutilizable para mostrar progreso en terminal.
Sirve para:
- Generación normal de combinaciones
- Auditoría por área
- Cualquier proceso futuro que tenga total y avance
"""
def __init__(self, total: int, ancho: int = 30):
self.total = total
self.ancho = ancho
self.inicio = time.time()
@staticmethod
def calcular_intervalo(total: int) -> int:
"""
Calcula cada cuántas iteraciones actualizar la barra.
Evita imprimir demasiado y hacer lento el programa.
"""
return max(
1,
min(100000, total // 1000)
)
def tiempo_transcurrido(self) -> float:
return time.time() - self.inicio
def mostrar(self, actual: int):
if self.total <= 0:
porcentaje = 1
else:
porcentaje = actual / self.total
porcentaje = min(max(porcentaje, 0), 1)
llenado = int(self.ancho * porcentaje)
barra = "#" * llenado + "-" * (self.ancho - llenado)
transcurrido = self.tiempo_transcurrido()
if transcurrido > 0:
velocidad = actual / transcurrido
else:
velocidad = 0
restante = self.total - actual
if velocidad > 0:
tiempo_restante = restante / velocidad
else:
tiempo_restante = 0
print(
f"\r[{barra}] "
f"{porcentaje * 100:6.2f}% | "
f"{actual:,}/{self.total:,} | "
f"{velocidad:,.0f}/s | "
f"ETA: {Formateador.tiempo_legible(tiempo_restante)}",
end="",
flush=True
)