Last active
January 5, 2021 04:17
-
-
Save daddykotex/ce00b02917fc8f21240395619424abee to your computer and use it in GitHub Desktop.
Use an existing python decorator with variable parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# pip install tenacity | |
# Usage: | |
# → py variable-python-decorators.py 4 | |
# The given maximum attempt number is 4 | |
# We want this function to be retried 5 times | |
# We want this function to be retried 5 times | |
# We want this function to be retried 5 times | |
# We want this function to be retried 5 times | |
# We want this function to be retried 5 times | |
# The function was retried 5 times. | |
# We want this function to be retried only 4 times | |
# We want this function to be retried only 4 times | |
# We want this function to be retried only 4 times | |
# We want this function to be retried only 4 times | |
# The function was retried 4 times. | |
# | |
import sys | |
from tenacity import retry, stop_after_attempt | |
# Typically, if the decorators you use are customisable, you | |
# have to pass parameters in the decorator like this: | |
max_attempts = 5 | |
@retry(stop=stop_after_attempt(max_attempts)) | |
def constant_decoration(): | |
print "We want this function to be retried {} times".format(max_attempts) | |
raise Exception("Exception thrown to trigger retry") | |
def static_decorator(): | |
try: | |
constant_decoration() | |
except: | |
print "The function was retried {} times.".format(max_attempts) | |
# To obtain a behavior that is variable based on runtime given arguments | |
# or maybe configuration, you can enhance the decorator like this: | |
# | |
# Create your own decarator that forward the call to the underlying decorator | |
# with the arguments you want | |
def variable_retry(func): | |
def _retry(*args): | |
if args.count > 0: | |
def call(): | |
func(*args) | |
variable_max_attempt = int(args[0]) | |
return apply(retry(stop=stop_after_attempt(variable_max_attempt))(call)) | |
else: | |
return func(*args) | |
return _retry | |
# Use your new decorator | |
@variable_retry | |
def variable_decoration(given_max_attempts): | |
print "We want this function to be retried only {} times".format(given_max_attempts) | |
raise Exception("Exception thrown to trigger retry") | |
def variable_decorator(given_max_attempts): | |
try: | |
variable_decoration(given_max_attempts) | |
except: | |
print "The function was retried {} times.".format(given_max_attempts) | |
def main(args): | |
given_max_attempts = args[1] | |
print "The given maximum attempt number is {}".format(given_max_attempts) | |
static_decorator() | |
variable_decorator(given_max_attempts) | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment