from decorators import decorator_maker
from exceptions import CustomException
@decorator_maker(delay=4)
def decorated_test_func(*args, **kwargs):
print('Function')
print('args:', args)
if args[0] == 1:
print('Raising exception')
raise CustomException
return True
decorated_test_func(2, 5)
from exceptions import CustomException
def decorator_maker(delay=0):
def do_if_raisen(func):
def wrap(self, *args, **kwargs):
try:
if delay !=0:
raise CustomException
return func(*args, **kwargs)
except CustomException as e:
print('delay:', delay)
print(e)
print('Code delayed for {s} sec.'.format(s=delay))
sleep(delay)
return func(*args, **kwargs)
return wrap
return do_if_raisen
# @decorator_maker(delay=4)
>>python test.py
>>Function
>>args: (2, 5)
>>python test.py
>>delay: 4
>>Message: None
>>Code delayed for 4 sec.
>>Function
>>args: (5,)