this definitely calls for a decorator.
you'd probably want something like:
import functools
def retry(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
retExc = None
for i in xrange(4):
try:
return func(*args, **kwargs)
except Exception, e:
retExc = e
raise retExc
return wrapper
and then to run your code, just use:
@retry
def some_request(...):
...
you could make it fancier by adding in a little sleep, adding a numTimes to retry, etc, but this will do it.