473,667 Members | 2,562 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 5244
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_wi th_sideeffects
do_something_wr ong
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 ZeroDivisionErr or:
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.co m'
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.c om' % i for i in xrange(1,5)]
for server in servers:
try:
u = urllib.urlopen( server)
break
except IOError:
pass
else:
raise NoAvailableServ ersException()

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 NoAvailableServ ersException. 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
9729
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: > > try: > x = could_raise_an_exception(23) > process(x) > except Exception, err: > deal_with_exception(err)
8
2027
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 drive or some file is locked by another process you want to display message dialog to user with "Retry" and "Cancel" buttons (As Windows shows "Retry" option if no CD in drive) and if user clicks "Retry" you want to run the same code again that...
3
1366
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 operation again. Here is what i meant: Try Dim x as integer
1
3412
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 which I handle, either using on error goto / err.number, or a try... catch block. I display a msgbox (abort,retry,ignore). Implementing retry and ignore is fairly easy using a loop structure. But how about "abort"?
0
8457
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8788
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8563
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8646
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7390
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4200
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
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 we have to send another system

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.