473,790 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suggestion for (re)try statement

Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,ti meout=0):
# things to try
except:
# what to do if failed

and having the following semantic:

for i in range(retrys):
try:
# things to try
except:
if i < retrys:
i += 1
sleep(timeout)
else:
# what to do if failed
else:
break

Of course, "break" may be the last statement in the
"try" branch, and "try"'s "else" may be ommited
completely.

Can't think of a syntax to keep it look like a
statement rather than a function.

Opinions? Is it worth for a PEP?

Sorin

_______________ _______________ ____
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
Oct 27 '05 #1
3 2260
Sori Schwimmer wrote:
Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,ti meout=0): <snip> sleep(timeout)

<snip>

At the very least, "timeout" is the wrong wording, "delay" would be more
appropriate. A timeout is usually associated with starting a task and
waiting for it to complete, and continuing if it fails to complete in a
given timeframe, typically also aborting the task at the same time (ie.
executing a database query, connecting to a server, waiting for an
event/lock, etc.).

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Oct 28 '05 #2
Sori Schwimmer wrote:
Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,ti meout=0):
# things to try
except:
# what to do if failed

and having the following semantic:

for i in range(retrys):
try:
# things to try
except:
if i < retrys:
i += 1
sleep(timeout)
else:
# what to do if failed
else:
break


The gold standard for language syntax changes is "compelling use cases"
- if introduced, how often will the construct be used? Is there a python
program out there (preferably in the standard library) which would be
*markedly* improved by the change? What is so repugnant about the
equivalent, currently valid way of writing it? -- Hypothetical and
theoretical arguments don't carry much weight in the Python community
("Practicali ty beats purity" and all that.)

And remember - your goal isn't ultimately to convince me or someother
person on comp.lang.pytho n, it's to convince Guido.
Oct 28 '05 #3
On 2005-10-27, Sori Schwimmer <sx***@yahoo.co m> wrote:
Hi,

I think that would be useful to have an improved
version of the "try" statement, as follows:

try(retrys=0,ti meout=0):
# things to try
except:
# what to do if failed

and having the following semantic:

for i in range(retrys):
try:
# things to try
except:
if i < retrys:
i += 1
sleep(timeout)
else:
# what to do if failed
else:
break
The "i += 1" line is almost certainly wrong.
Of course, "break" may be the last statement in the
"try" branch, and "try"'s "else" may be ommited
completely.
And that's pretty much exactly how I usually write it:

for i in range(retries):
try:
whatever
break
except retryableExcept ionList:
sleep(delay)
Can't think of a syntax to keep it look like a statement
rather than a function.

Opinions?
I don't see what's wrong with the for loop construct.
You can add an else: clause to the for loop to detect the case
where you ran out of retries:

for i in range(retries):
try:
whatever
break
except retryableExcept ionList:
sleep(delay)
else:
whatelse
Is it worth for a PEP?


I don't think you can come up with a syntax that is really that
much better than the for loop, but give it a go if you like.

--
Grant Edwards grante Yow! My BIOLOGICAL ALARM
at CLOCK just went off... It
visi.com has noiseless DOZE FUNCTION
and full kitchen!!
Oct 28 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1301
by: Rex | last post by:
I have 2 machines running MS SQL 2000 SP3 on separate network, Machine A undergoes transactional replication to Machine B, recently the network is not stable, so the replication stopped upon retry, and I have to re-sync it manually. I would like to know, 1. How many times of transactional replication wil re-try? 2. How long does it take? 3. Can(How) can I set a schedule task to re-sync replication manually? Thanks in advance.
19
2572
by: White Wolf | last post by:
Hi, I cannot believe my eyes, but so far I could not find out what is the name of the construct, which is built from one try block and the catch clauses (handlers). I am not looking for a grammar name, but something people can remember, like handler. But is not handler, because those are the catch clauses and the try does not belong there. I have looked at all my books and the standard, but I could not find a name. :-( Is there...
0
1249
by: Sori Schwimmer | last post by:
"i += 1" is wrong there. I had in my mind at first a "while" statement. Sorry... Sorin
3
335
by: Sori Schwimmer | last post by:
0) Sorry, I don't know how to post a reply in the same thread. 1) Grant Edwards wrote: > The "i += 1" line is almost certainly wrong. You're certainly write, as I acknowledged in a follow up "suggestion for (re)try statement - correction' 2) Rocco Morreti wrote: > What is so repugnant about the equivalent, currently valid way of writing it? Nothing "repugnant". We have in almost all procedural
37
2964
by: clintonG | last post by:
Has somebody written any guidelines regarding how to determine when try-catch blocks should be used and where their use would or could be considered superfluous? <%= Clinton Gallagher METROmilwaukee (sm) "A Regional Information Service" NET csgallagher AT metromilwaukee.com URL http://metromilwaukee.com/ URL http://clintongallagher.metromilwaukee.com/
5
14078
by: Rob R. Ainscough | last post by:
Coming from VB6 to VB.NET, it appears if I opt to use the Try Catch approach I don't have any way to RESUME from within my catch statement? What I often do is resolve the problem in my catch statement and then I want to resume at either the same statement that triggered the error or the following statement. Perhaps I should not be using Try...Catch approach? Any suggestions? Thanks, Rob
2
3278
by: John | last post by:
The ASP application inserts transaction records in transaction table with the system time as the primary key. However, it is possible to have primary key violation because the records in transaction table come from different sources. The application can show error message and the user can file a transaction again manually, but I want the application can have multiple re-tries to perform insert statement until there is no primary key...
28
3844
by: RickHodder | last post by:
I'm getting frustrated with using try...catch with local variables: The code below wont compile in .NET 1.1: I get the following error: "Use of unassigned local variable 'oProcessFileReader' " Is there a way around this error? <code> private void Test(string sFileName) {
5
2550
by: jobs | last post by:
re: try catch finally to close and dispose, but still want Application_error to fire 1. If catch an exception to to dispose and close of a ado connect, how can I allow the exception to still trigger my application_error event? 2. And/Or, Is there any way to close and dispose of connections left behind by methods that might have failed without closing connections in the application_error event?
13
4529
by: Gobinath | last post by:
Hi, Is there any overhead attached with the try catch block? In C# everything is an exception. So is it OK to have a try catch block for each function ??? or is there any other way. What is the correct /efficient programming practise?? Is there any other article available on the same? Thanks for your time.
0
9666
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
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10201
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...
0
9987
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...
1
7531
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
6770
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();...
1
4100
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
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.