Advertisement
MisterQuirk

Untitled

Apr 13th, 2024 (edited)
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. #Task 1
  2. import time
  3. def timer(func):
  4.     def inside_func(*args, **kwargs):
  5.         st = time.time()
  6.         func(*args)
  7.         fin = time.time()
  8.         print(f'Function worked for {fin - st} seconds')
  9.  
  10.     return inside_func
  11.  
  12.  
  13. #Task 2
  14.  
  15. def cache(f):
  16.     D = dict()
  17.     def inside_f(*args):
  18.         if args not in D:
  19.             D[args] = f(*args)
  20.         return D[args]
  21.     return inside_f
  22.  
  23. #Task 3
  24.  
  25. def logging(f):
  26.     def inside(*args):
  27.         result = f(*args)
  28.         with open('log.txt', 'a+') as file:
  29.             print(f'The function has been called {f.__name__}, the result of its work is - {result}', file=file)
  30.             return result
  31.  
  32.     return inside
  33.  
  34. #Task 4
  35.  
  36. def retry(sec, times):
  37.     def decor(f):
  38.         def inside(*args):
  39.             for i in range(times):
  40.                 if f(*args) is not None:
  41.                     return f(*args)
  42.                 time.sleep(sec)
  43.             return None
  44.         return inside
  45.     return decor
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement