-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorador_timefunc.py
More file actions
36 lines (30 loc) · 1.1 KB
/
decorador_timefunc.py
File metadata and controls
36 lines (30 loc) · 1.1 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
from datetime import datetime
def execution_time(func):
def wrapper(*args, **kwargs): # Ver mensaje debajo
initial_time = datetime.now()
func(*args, **kwargs)
final_time = datetime.now()
time_elapsed = final_time - initial_time
print("Pasaron "+str(time_elapsed.total_seconds())+" segundos.")
return wrapper
@execution_time # Decoro la función que tengo debajo con el decorador que construí arriba
def random_func():
for x in range(1,1000000):
pass
@execution_time
def sum(a: int, b: int) -> int:
return a + b
@execution_time
def saludo(nombre="Cesar"): # Fijo un parametro por defecto. Un Kwargs
print("Hola "+ nombre)
"""
Si decoro la funcion sum, dejando la función wrapper sin argumentos
no se va a ejecutar y va a correr un TypeError ya que sum tiene 2
argumentos y wrapper no tiene ninguno. Para lograr que wrapper funcione
con cualquier cantidad de argumentos nombrados y sin nombrar debo
agregar como paramentros de la función (*args, **kwargs)
"""
sum(5, 5)
random_func() #Ejecuto mi random_func decorada.
saludo("Mariano")
saludo()