By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

retry in exception

P: n/a
Hi,

I hope I don't upset anybody by comparing Python to Ruby (again). Is
there something like Ruby's retry keyword in Python? I couldn't find any
thing...

Regards,
antoine
Sep 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Antoine De Groote enlightened us with:
I hope I don't upset anybody by comparing Python to Ruby (again). Is
there something like Ruby's retry keyword in Python?
Please don't assume that everybody knows Ruby through and through...

Sybren
--
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/
Sep 29 '06 #2

P: n/a
Sybren Stuvel wrote:
Antoine De Groote enlightened us with:
I hope I don't upset anybody by comparing Python to Ruby (again). Is
there something like Ruby's retry keyword in Python?

Please don't assume that everybody knows Ruby through and through...
In ruby, the equivalent to try...except is begin...rescue. In the
rescue section you can ask it to retry the begin section. So, for
example:

b=0
begin
puts 1/b
rescue
b=1
retry # <- this little guy
end

I don't think python has any equivalent (could be wrong). You can do
something like this however:

b=0
def do_stuff():
print 1/b
try:
do_stuff()
except:
b=1
do_stuff()

Regards,
Jordan

Sep 29 '06 #3

P: n/a
MonkeeSage schrieb:
Sybren Stuvel wrote:
>Antoine De Groote enlightened us with:
>>I hope I don't upset anybody by comparing Python to Ruby (again). Is
there something like Ruby's retry keyword in Python?
Please don't assume that everybody knows Ruby through and through...

In ruby, the equivalent to try...except is begin...rescue. In the
rescue section you can ask it to retry the begin section. So, for
example:

b=0
begin
puts 1/b
rescue
b=1
retry # <- this little guy
end
Not sure I like that...

begin
do_something_with_sideeffects
do_something_wrong
rescue
retry
end

could produce quite a bit of unexpected results if used uncautiously.

Diez
Sep 29 '06 #4

P: n/a
MonkeeSage wrote:
I don't think python has any equivalent (could be wrong).
a trivial combination of while and try/except/else does the trick:

n = 0

while 1:
try:

# do something
print n
n = n + 1
# make sure it fails a couple of times
if n < 10:
raise ValueError

except ValueError:
pass # retry
else:
break # done

Sep 29 '06 #5

P: n/a
In ruby, the equivalent to try...except is begin...rescue. In the
rescue section you can ask it to retry the begin section. So, for
example:

b=0
begin
puts 1/b
rescue
b=1
retry # <- this little guy
end

Well, it's all a matter of how you look at it. I personally
prefer to see that there's some sort of looping going on.
Something crazy like this contrived example, it would be best to
just check to see if b==0 beforehand. However, for the sake of
example, one could do something like

b = 0

for d in [b, 1]:
try:
print 1/d
break
except ZeroDivisionError:
pass
which would allow you to have a number of things to try...to give
a little more plausibility to the idea, the original ruby might
have been something like (forgive my ignorance of ruby syntax)

url = 'http://www.example.com'
begin
get_webpage(url)
rescue
url = 'http://www2.example.com'
retry
end
which would try a backup server if the main one is down. The
above python variant would allow an arbitrary number of servers
to be listed

servers = ['http://www%i.example.com' % i for i in xrange(1,5)]
for server in servers:
try:
u = urllib.urlopen(server)
break
except IOError:
pass
else:
raise NoAvailableServersException()

This comes full-circle on another thread asking about the use of
"else:" after a for/while loop...yes, nice to have.

The above python code will try to open each of the servers in
turn until it gets one that it can open. If it can't open any of
them, it raises some concocted NoAvailableServersException. To
get the above to succeed, you can add

servers[3] = 'http://www.example.com'

which will actually exist, and thus will fall out of the loop
with "u" successfully set to the connection from which you can read.

I have to agree with Diez, that this sounds like some
ambiguous/dangerous behaviour could ensue from such a language
construct. I'd just as soon specify my desired behavior explicitly.

Just a few ideas.

-tkc


Sep 29 '06 #6

P: n/a
Everyone wrote:
[cool stuff]
Ps. I've only used retry a handful of times in about 3 or 4 years of
using ruby; I wasn't advocating it or criticizing python, just trying
to explain the OPs request.

Regards,
Jordan

Sep 29 '06 #7

P: n/a
Sybren Stuvel wrote:
Antoine De Groote enlightened us with:
>>I hope I don't upset anybody by comparing Python to Ruby (again). Is
there something like Ruby's retry keyword in Python?


Please don't assume that everybody knows Ruby through and through...
I didn't see any such assumption in that post. *I* don't know much Ruby,
but it seems like a perfectly reasonable question, as I know many
readers *are* proficient in that language.

This is one of those cases where not posting at all would have saved
everyone some time ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 30 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.