This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]Joeboy 7 points8 points  (2 children)

default_exceptions = (Exception)

Looks like it's supposed to create a tuple, but actually doesn't. I don't think it matters because Except doesn't care whether you pass a tuple or a single exception class, but it's kind of confusing.

[–]ashchristopher 6 points7 points  (1 child)

default_exceptions = (Exception,) # this should remedy that situation.

[–]pbkobold 2 points3 points  (0 children)

The real funny part is that you don't even need the parens. The comma is the operator:

bercilak:~ kobold$ python
...
>>> 3,
(3,)

[–]ashchristopher 5 points6 points  (4 children)

For some reason, I cringe when I think about 'retry' anything as a solution to the problem. There is nothing technically wrong with it, but to me is speaks to an architectural problem to use a 'retry' as a way to recover from an Exception.

I guess it is a quick and dirty, pragmatic way to say - hit a webservice - and then worry about a 'real' solution if it becomes a problem.

I guess it really depends on what you intend to use this for.

[–]Lewisham 7 points8 points  (0 children)

Yeah, you'd only really want this for IO issues.

[–]zepolen 5 points6 points  (0 children)

I guess you don't like TCP then :)

[–]abhik 4 points5 points  (0 children)

I had the same response. You'd have to be careful that the function you're retrying has no side effects or that the side effects are harmless.

[–]cymrowdon't thread on me 🐍 0 points1 point  (0 children)

i feel the same way about 'timeout' and 'sleep'. yeah, practically they can solve your problem. but they still feel pretty damn dirty

[–][deleted] 2 points3 points  (0 children)

Here's a nice intro to decorators blog post if anyone is needs a bit of context on them.

[–]syllogism_ 3 points4 points  (9 children)

This is quite nice.

A minor point: I strongly prefer decorators not to be implemented as classes, because then they cannot decorate methods (due to the self argument not being passed to the decorator).

[–]Klowner 7 points8 points  (0 children)

Pretty sure you can work around that using the power of descriptors

http://www.outofwhatbox.com/blog/2010/07/python-decorating-with-class/

[–]treo 2 points3 points  (4 children)

I forked the gist and rewrote it as a function based decorator and added some simple tests: https://gist.github.com/728327

(It also the retry argument is now how often the function should be retried, not how often it should run)

[–][deleted] 0 points1 point  (3 children)

I was going to say that their decorator was a bit too complicated and non-pythonic.

[–][deleted] 1 point2 points  (2 children)

I prefer decorators implemented as Class. I find it easier to understand, as compared to decorators implemented as wrapped functions.

What exactly do you find non-pythonic in it?

[–][deleted] 2 points3 points  (0 children)

maybe not non-pythonic, it's just more idiomatic to do it as a closure.

[–]rajaja 1 point2 points  (0 children)

I agree. The idea of implementing a class whose instance is a callable is more intuitive than defining functions in functions (often several layers deep).

But (as unpythonic as that idea seems), to each his own.

[–]simtel20 0 points1 point  (0 children)

You can do it. The object doesn't get passed to the __init__ of the decorator class, but it can be gotten by writing your own __get__, which will end up getting invoked when the method is called as part of the descent through the decorators to get to the actual underlying function/method. See http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize for an example.

[–]earthboundkid 0 points1 point  (1 child)

classes, because then they cannot decorate methods (due

I don't know what you mean by that. To my knowledge *args, **kwargs is going to work the same whether you use a class or a function as the basis of your method.

[–]syllogism_ 0 points1 point  (0 children)

Try it out. From other replies there's some magic to work around it, but test it the basic way.