[Python-ideas] Retrying EAFP without DRY
Carl M. Johnson
cmjohnson.mailinglist at gmail.com
Sat Jan 21 10:17:50 CET 2012
On Jan 20, 2012, at 11:14 PM, Carl M. Johnson wrote:
> def try_repeatedly(times, exception, callback, args, kwargs):
> for _ in range(times):
> try:
> return callback(*args, **kwargs)
> except exception:
> continue
> break
> error_message = "Tried {} times but could not complete callback {}"
> error_message = error_message.format(times, repr(callback))
> raise Exception(error_message)
>
> try_repeatedly(5, SpamError, do_something, [x])
Oops, don't need the "break".
More information about the Python-ideas
mailing list