-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerador.py
More file actions
106 lines (82 loc) · 2.77 KB
/
Copy pathgenerador.py
File metadata and controls
106 lines (82 loc) · 2.77 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
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
from itertools import product, islice
import os
import tempfile
import time
from utilidades import Formateador
from progreso import BarraProgreso
from config import ResultadoArchivo
class GeneradorCombinaciones:
def probar_velocidad(
self,
caracteres: str,
longitud: int,
cantidad: int,
carpeta: str
):
muestra = min(cantidad, 50000)
if muestra <= 0:
return 0
archivo_temp = None
try:
temp = tempfile.NamedTemporaryFile(
delete=False,
dir=carpeta,
prefix="prueba_velocidad_",
suffix=".tmp",
mode="w",
encoding="utf-8",
newline="\n"
)
archivo_temp = temp.name
inicio = time.time()
with temp as archivo:
generador = product(caracteres, repeat=longitud)
for combinacion in islice(generador, muestra):
archivo.write("".join(combinacion) + "\n")
fin = time.time()
tiempo = fin - inicio
if tiempo <= 0:
return 0
return muestra / tiempo
finally:
if archivo_temp and os.path.exists(archivo_temp):
os.remove(archivo_temp)
def generar_archivo(
self,
caracteres: str,
longitud: int,
cantidad: int,
ruta: str
):
print("\nGenerando archivo...\n")
barra = BarraProgreso(cantidad)
intervalo_progreso = BarraProgreso.calcular_intervalo(cantidad)
contador = 0
with open(
ruta,
"w",
encoding="utf-8",
newline="\n"
) as archivo:
generador = product(caracteres, repeat=longitud)
for combinacion in islice(generador, cantidad):
archivo.write("".join(combinacion) + "\n")
contador += 1
if contador % intervalo_progreso == 0:
barra.mostrar(contador)
barra.mostrar(contador)
tiempo_total = barra.tiempo_transcurrido()
tamano_real = os.path.getsize(ruta)
print("\n\n" + "=" * 70)
print("✅ PROCESO FINALIZADO")
print("=" * 70)
print(f"Combinaciones escritas : {contador:,}")
print(f"Tiempo real empleado : {Formateador.tiempo_legible(tiempo_total)}")
print(f"Tamaño real del archivo : {Formateador.tamano_legible(tamano_real)}")
print(f"Archivo generado : {ruta}")
return ResultadoArchivo(
ruta_archivo=ruta,
elementos_escritos=contador,
tiempo_segundos=tiempo_total,
tamano_real=tamano_real,
nombre_elemento="combinaciones")