[Tutor] Really miss the C preprocessor!!
steve at pearwood.info
Sat Mar 6 14:51:30 CET 2010
On Sat, 6 Mar 2010 07:17:43 pm you wrote:
> I thought about suggesting using decorators for this, since I've done
> something similar (not exactly exponential backoff, but retrying a
> few times on exception). However, as I started writing the example, I
> got stuck at expressing a generic way to pass the exception and
> numretries. I was just wondering is there some way to do this sort of
> thing with decorators ??
> is there a way to decorate a function alongwith additional parameters
> which will be passed to the function ?
Yes, you need a decorator factory -- a function which returns a
def factory(exception, numretries):
def inner(*args, **kwargs):
t = 1
for i in range(numretries):
return func(*args, **kwargs)
t *= 2
msg = "no connection after %d attempts" % numretries
def check_some_website(url, x):
I haven't tested the above code, but it should do the trick.
More information about the Tutor