473,698 Members | 2,185 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 5246
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
9734
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
1367
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
3417
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
8668
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9152
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
8855
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
7708
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 projectplanning, coding, testing, and deploymentwithout 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...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
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
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.