473,509 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

retry in exception

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
7 5239
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9717
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
8
2017
by: Adil Akram | last post by:
There are situations where you want to retry/rerun the same code again (if user wants) for unlimited times for a particular exception thrown. For example in situations like there's no CD in the...
3
1355
by: kimiraikkonen | last post by:
Hi, It may a simple question but i needed to verify. I hava a for-next loop and when an exception occurs, i don't want my program to the jump out of try-catch block and i want it to retry the...
1
3409
by: Nimral | last post by:
Hi folks, is there a smart way to develop efficiently that helps me in the following situation: a sub calls a subsub, which calls a subsubsub, and so on. Within a subsubsub an error occurs...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.