Parameterised Decorator in Python

So far most of have a fair idea about decorators, if not then

“Decorators are the way in python we can enhance the functionality of function, or we could say we could supercharge our functions.”

def hello():
print("Hello")
def bye():
print("Bye")

Now above two simple functions are there, they ordinarily print, “Hello” and “Bye”

But now lets Enhance the functionality of hello and bye functions!

But there a better way to do it

@decorator
def bye():
print("Bye")

and then simpally

Now lets say there are some parameters , and the function accept a name and a feeling

We need to enhance the decorator to handle the params:

>>>
>>> hello("Steven", status="Swimming")
##########
i am Steven and i am Swimming
##########
>>>

Now let’s get to a real situation, In our test code, we want a function to be attempted 3 times, before raising an error! calling an api, connecting to a remote server using paramiko etc, then we can use parameterised decorator.

The below decorator will try to call a function, with the number of attempt and the delay in attempts

def with_retries(attempts=3, delay=1):
def decorator(func):
def wrapper(*args, **kwargs):
for i in range(attempts):
print("Calling %s, "
"attempt %s" % (
func.__name__,
i))
try:
return func(*args, **kwargs)
except Exception as exc:
print("Failed with exception %s" % exc)
time.sleep(delay)
print("Failed to execute in %s attempts!" % attempts)
return wrapper
return decorator
@with_retries()
def get_api(api):
res = requests.get(api)
return res.status_code

We haven’t entered the parameters, and we want the decorator to run with the default values, otherwise we can pass the parameters to the call with desired parameters.

A Infra2Qe automation engineer