From a0795f56ab7626713f7647a66b921b7435da18a5 Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Wed, 19 Mar 2025 17:51:14 +0000 Subject: [PATCH] decorator finished --- decorators/decorator.py | 31 ++++++++++++++----------------- decorators/wiki.txt | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/decorators/decorator.py b/decorators/decorator.py index eab8160..c5dc44f 100644 --- a/decorators/decorator.py +++ b/decorators/decorator.py @@ -1,20 +1,17 @@ -#decorator -def my_decorator(func): +from time import time - def wrap_func(): - print('************') - func() - print('************') +def performance(fn): + def wrapper(*args, **kwargs): + t1 = time() + result = fn(*args, **kwargs) + t2 = time() + print(f'took {t2-t1} ms') + return result + return wrapper - return wrap_func +@performance +def long_time(): + for i in range(1000000): + i*5 -@my_decorator -def hello(): - print('hellllooooooooooo') - -@my_decorator -def bye(): - print('see ya letter') - -hello() -bye() \ No newline at end of file +long_time() \ No newline at end of file diff --git a/decorators/wiki.txt b/decorators/wiki.txt index 2c06ca0..c9a5a08 100644 --- a/decorators/wiki.txt +++ b/decorators/wiki.txt @@ -108,4 +108,24 @@ hello('hii') - + + for ex: + from time import time + + def performance(fn): + def wrapper(*args, **kwargs): + t1 = time() + result = fn(*args, **kwargs) + t2 = time() + print(f'took {t2-t1} ms') + return result + return wrapper + + @performance + def long_time(): + for i in range(1000000): + i*5 + + long_time() + + \ No newline at end of file