Re: [Tutor] try, except syntax

2018-08-02 Thread Steven D'Aprano
On Thu, Aug 02, 2018 at 12:19:41PM -0700, Marc Tompkins wrote: > What you want in that case is an assertion: > > try: > assert type(uvc)==float > except AssertionError as e: > print(e, msg) > > An assertion says "The following statement is True. If it isn't, I'm going > to throw an exce

Re: [Tutor] try, except syntax

2018-08-02 Thread Alan Gauld via Tutor
On 02/08/18 15:29, Shall, Sydney wrote: > uvc = 2 > msg = "Bad argument provided, the value of uvc must be a float." > > try: >     type(uvc) == float > except TypeError as e: >     print(e, msg) The try block contains an expression that will always evaluate to True or False and thus never rai

Re: [Tutor] try, except syntax

2018-08-02 Thread Marc Tompkins
try... except is meant to catch errors: places where your program would otherwise crash. It does NOT work as a truth check. In your example: > try: > type(uvc) == float > except TypeError as e: > print(e, msg) > > "type(uvc)==float" resolves to a standalone True or False, not an exception

Re: [Tutor] try, except syntax

2018-08-02 Thread Oscar Benjamin
On 2 August 2018 at 15:29, Shall, Sydney wrote: > > try: > type(uvc) == float > except TypeError as e: > print(e, msg) Let's try this interactively: >>> uvc = 2 >>> type(uvc) >>> type(uvc) == float False So calling type(uvc) doesn't raise an error. It returns the type "int". You then c

[Tutor] try, except syntax

2018-08-02 Thread Shall, Sydney
MAC OS X 10.13.6 Anaconda python 3.6.5 Anaconda IPython 6.4.0 I am having difficulty in using the try, except form. I copy and paste a minimal program to illustrate my problem. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Aug  2 15:41:15 2018 @author: sydney """ uvc = 2

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-23 Thread boB Stepp
On Wed, May 22, 2013 at 8:47 PM, Steven D'Aprano wrote: > On 23/05/13 02:09, boB Stepp wrote: > >> I would like to ask some general questions here. Problems can arise >> from bugs in the operating system, bugs in the programming language(s) >> being used, bugs in packages/modules being used, bugs

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Steven D'Aprano
On 23/05/13 02:09, boB Stepp wrote: I would like to ask some general questions here. Problems can arise from bugs in the operating system, bugs in the programming language(s) being used, bugs in packages/modules being used, bugs in any third party packages being used, etc. Also, whenever any one

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Dave Angel
On 05/22/2013 03:03 PM, Albert-Jan Roskam wrote: Subject: Re: [Tutor] try..except - what about that ton of **Error statements? On 23/05/13 02:09, boB Stepp wrote: I was not aware that hardware damage could be caused by poor programming. I am curious; can you give some examples of how

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Albert-Jan Roskam
> Subject: Re: [Tutor] try..except - what about that ton of **Error statements? > > On 23/05/13 02:09, boB Stepp wrote: > >> I was not aware that hardware damage could be caused by poor >> programming. I am curious; can you give some examples of how this >> mi

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Marc Tompkins
On Wed, May 22, 2013 at 9:50 AM, boB Stepp wrote: > I guess I'll pick the first alternative. However, this brings to mind > my biggest gripe as a user of software, particularly here at work > where the programmer obviously has no medical background: cryptic > error messages that might be meaningf

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Steven D'Aprano
On 23/05/13 02:09, boB Stepp wrote: I was not aware that hardware damage could be caused by poor programming. I am curious; can you give some examples of how this might occur? Does your computer have a DVD drive? Or Blu-Ray? Is it region-locked? Some region-locked drives let you change the re

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread boB Stepp
On Wed, May 22, 2013 at 9:45 AM, Steven D'Aprano wrote: > On 22/05/13 23:31, boB Stepp wrote: >> >> On Wed, May 22, 2013 at 7:50 AM, Steven D'Aprano >> wrote: [...] >>> 3) your job as a programmer is *not* to stop your program from raising an >>> error, but to make it behave correctly -- someti

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread boB Stepp
Thanks, Steve, for your last two posts. You have made things much clearer for me. On Wed, May 22, 2013 at 9:49 AM, Steven D'Aprano wrote: > On 22/05/13 23:37, boB Stepp wrote: >> >> On Wed, May 22, 2013 at 8:07 AM, Steven D'Aprano >> wrote: >>> [...] >> >> Being a novice programmer, I am inter

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Steven D'Aprano
On 22/05/13 23:37, boB Stepp wrote: On Wed, May 22, 2013 at 8:07 AM, Steven D'Aprano wrote: On 22/05/13 16:20, Jim Mooney wrote: [...} A very important quote from Chris Smith: "I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Steven D'Aprano
On 22/05/13 23:31, boB Stepp wrote: On Wed, May 22, 2013 at 7:50 AM, Steven D'Aprano wrote: On 22/05/13 15:46, Jim Mooney wrote: [...] But don't do this in real code! In real code, the rules you should apply are: 1) never hide programming errors by catching exceptions; 2) errors shoul

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread boB Stepp
On Wed, May 22, 2013 at 8:07 AM, Steven D'Aprano wrote: > On 22/05/13 16:20, Jim Mooney wrote: >>> [...} > A very important quote from Chris Smith: > > "I find it amusing when novice programmers believe their main job is > preventing programs from crashing. ... More experienced programmers reali

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread boB Stepp
On Wed, May 22, 2013 at 7:50 AM, Steven D'Aprano wrote: > On 22/05/13 15:46, Jim Mooney wrote: >> [...] > > But don't do this in real code! In real code, the rules you should apply > are: > > > 1) never hide programming errors by catching exceptions; > > 2) errors should only be caught if you ca

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Steven D'Aprano
On 22/05/13 16:20, Jim Mooney wrote: Keep the try block small. For example if it's for a call to open(filename, "r") the only possible errors (assuming correct syntax) are NameError for using an undefined variable and IOError for specifying a file which doesnt exist. Jim, I don't know who you

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Steven D'Aprano
On 22/05/13 15:46, Jim Mooney wrote: I'm looking at Try..Except Try: Except SomethingError as err: The list of error statements is huge. How do I know which error statement to put in place of SomethingError (or multiple errors for that matter)? Or is it best to just leave Something

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-22 Thread Marc Tompkins
On Tue, May 21, 2013 at 11:25 PM, Bod Soutar wrote: > On 22 May 2013 07:20, Jim Mooney wrote: > >> Keep the try block small. For example if it's for a call to > >> open(filename, "r") the only possible errors (assuming correct syntax) > >> are NameError for using an undefined variable and IOErro

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-21 Thread Bod Soutar
On 22 May 2013 07:20, Jim Mooney wrote: >> Keep the try block small. For example if it's for a call to >> open(filename, "r") the only possible errors (assuming correct syntax) >> are NameError for using an undefined variable and IOError for >> specifying a file which doesnt exist. > > Thanks. Sin

Re: [Tutor] try..except - what about that ton of **Error statements?

2013-05-21 Thread Jim Mooney
> Keep the try block small. For example if it's for a call to > open(filename, "r") the only possible errors (assuming correct syntax) > are NameError for using an undefined variable and IOError for > specifying a file which doesnt exist. Thanks. Since I'm new at this the error lists I saw just ha

[Tutor] try..except - what about that ton of **Error statements?

2013-05-21 Thread Jim Mooney
I'm looking at Try..Except Try: Except SomethingError as err: The list of error statements is huge. How do I know which error statement to put in place of SomethingError (or multiple errors for that matter)? Or is it best to just leave SomethingError blank until I know more? ( I have a

Re: [Tutor] Try except really better than if?

2011-01-10 Thread Alan Gauld
"Steven D'Aprano" wrote Wrapping an atomic block of code, so long as it actually *can* be treated as atomic (including backing out any changes if it fails), is a good use of try...except. That's exactly what I meant by atomic. A (smallish) group of instructions that must be treated as a gro

Re: [Tutor] Try except really better than if?

2011-01-10 Thread Steven D'Aprano
Alan Gauld wrote: However, using exceptions like if/else blocks is bad practice. I'm afraid I disagree strongly with that! You need to use them like a commit in SQL - to protect a whole block of code. Putting try/except around every line of code defeats the purpose, wrap atomic blocks of cod

Re: [Tutor] Try except really better than if?

2011-01-10 Thread Karim
Steven many thanks for this useful hint! I will modify my code. Many thanks! Regards Karim On 01/10/2011 11:32 AM, Steven D'Aprano wrote: Karim wrote: Thank you Steven, Modulok and Alan for your precious and detailed explanations! I understood that I must not overuse try-except statement

Re: [Tutor] Try except really better than if?

2011-01-10 Thread Steven D'Aprano
Karim wrote: Thank you Steven, Modulok and Alan for your precious and detailed explanations! I understood that I must not overuse try-except statement and usually when the exception could happen exceptionally. By the way I have this piece of code using elementTree standard module and accord

Re: [Tutor] Try except really better than if?

2011-01-10 Thread Karim
Thanks Alan this reassures me! Regards Karim On 01/10/2011 10:39 AM, ALAN GAULD wrote: By the way I have this piece of code using elementTree standard module and according to Alan this is bad code I guess: Which just proves there are no absolute rules in programming :-) *try

Re: [Tutor] Try except really better than if?

2011-01-10 Thread ALAN GAULD
By the way I have this piece of code using elementTree standard module >and according to Alan this is bad code I guess: > > Which just proves there are no absolute rules in programming :-) try: devdb.setDescription(dev.attrib['description']) >except KeyError: pass >try: devdb.setSymbolName

Re: [Tutor] Try except really better than if?

2011-01-10 Thread Karim
Thank you Steven, Modulok and Alan for your precious and detailed explanations! I understood that I must not overuse try-except statement and usually when the exception could happen exceptionally. By the way I have this piece of code using elementTree standard module and according to Alan th

Re: [Tutor] Try except really better than if?

2011-01-09 Thread Alan Gauld
"Karim" wrote I am using more and more try except statement. But I discussed with a very experienced C++ programmer. And he told me that at least in C++ using too many try -catch statements are making the code slower. That's true, but Bjarne Stroustrup put them in for a reason - and they are

Re: [Tutor] Try except really better than if?

2011-01-09 Thread Modulok
On 1/9/11, Karim wrote: > > Hello all, > > I am using more and more try except statement. But I discussed with a > very experienced C++ programmer. > And he told me that at least in C++ using too many try -catch statements > are making the code slower. And it should > does the same for python (C i

Re: [Tutor] Try except really better than if?

2011-01-09 Thread Steven D'Aprano
Karim wrote: Hello all, I am using more and more try except statement. But I discussed with a very experienced C++ programmer. And he told me that at least in C++ using too many try -catch statements are making the code slower. And it should does the same for python (C implementation). He

[Tutor] Try except really better than if?

2011-01-09 Thread Karim
Hello all, I am using more and more try except statement. But I discussed with a very experienced C++ programmer. And he told me that at least in C++ using too many try -catch statements are making the code slower. And it should does the same for python (C implementation). My simple question:

Re: [Tutor] try except block for multiple statements

2008-12-07 Thread Kent Johnson
On Sun, Dec 7, 2008 at 12:25 PM, Lie Ryan <[EMAIL PROTECTED]> wrote: > texts = ["a = %s\n" % plan.a, > "b = %s\n" % plan.b, > "c = %s\n" % plan.c, > "d = %s\n" % plan.d >] > > for text in texts: >try: >fo.write(text) >except AttributeError: >

Re: [Tutor] try except block for multiple statements

2008-12-07 Thread Lie Ryan
On Mon, 01 Dec 2008 20:44:20 -0500, Bryan Fodness wrote: > I would like to use a try except to see if a value exists. But, when I > use the following, if a does not exist it exits. I understand why this > does this, but is there a way to get b,c, and d if a does not exist > without using a try e

Re: [Tutor] try except block for multiple statements

2008-12-01 Thread bob gailer
Bryan Fodness wrote: I would like to use a try except to see if a value exists. But, when I use the following, if a does not exist it exits. I understand why this does this, but is there a way to get b,c, and d if a does not exist without using a try except for every statement? try: fo

Re: [Tutor] try except block for multiple statements

2008-12-01 Thread John Fouhy
On 02/12/2008, Bryan Fodness <[EMAIL PROTECTED]> wrote: > I would like to use a try except to see if a value exists. But, when I use > the following, if a does not exist it exits. I understand why this does > this, but is there a way to get b,c, and d if a does not exist without using > a try exc

[Tutor] try except block for multiple statements

2008-12-01 Thread Bryan Fodness
I would like to use a try except to see if a value exists. But, when I use the following, if a does not exist it exits. I understand why this does this, but is there a way to get b,c, and d if a does not exist without using a try except for every statement? try: fo.write("a = %s\n" %plan.a)

Re: [Tutor] try & except

2006-08-08 Thread Alan Gauld
> I'm playing around with try: and except: i have a code like this >try: >bibl=os.listdir("c:\\klientdata\\") >except: >wx.MessageBox("Kunde inte läsa käll ... > > So far i get how to use it but i would like to be able to execute > the try block again after

Re: [Tutor] try & except

2006-08-08 Thread John Fouhy
On 09/08/06, Magnus Wirström <[EMAIL PROTECTED]> wrote: > Hi > > I'm playing around with try: and except: i have a code like this >try: >bibl=os.listdir("c:\\klientdata\\") >except: >wx.MessageBox("Kunde inte läsa käll > biblioteket.","Säkerhetskopiering",wx.

[Tutor] try & except

2006-08-08 Thread Magnus Wirström
Hi I'm playing around with try: and except: i have a code like this try: bibl=os.listdir("c:\\klientdata\\") except: wx.MessageBox("Kunde inte läsa käll biblioteket.","Säkerhetskopiering",wx.OK|wx.ICON_ERROR) (yeah ... it's swedish :) ) So far i get how t

Re: [Tutor] try except continue (fwd)

2005-08-24 Thread Smith, Jeff
D] Sent: Wednesday, August 24, 2005 2:29 PM To: Tutor Subject: Re: [Tutor] try except continue (fwd) -- Forwarded message -- Date: Wed, 24 Aug 2005 11:24:44 -0700 From: [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] try excep

Re: [Tutor] try except continue (fwd)

2005-08-24 Thread Danny Yoo
-- Forwarded message -- Date: Wed, 24 Aug 2005 11:24:44 -0700 From: [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] try except continue hi Danny, I finally had a chance to review your explanation of continue you wrote a

Re: [Tutor] try except continue

2005-08-01 Thread Alan G
-- code here - def f(): if error: raise ValueError else: print "I'm in f()" def g(): print "I'm in g()" def h(): error = True print "error is: ", error n = 5 while n: print "n is: ", n n -= 1 try: f() g() except ValueError:

Re: [Tutor] try except continue

2005-08-01 Thread tpc247
hi guys, so I've been running through Alan's code, and for a while I suspected that the try block must be the first line of code after the loop in order to be executed. The reason I say this is I wanted to see for myself Alan's assertion that the continue skips to the next iteration, instead of co

Re: [Tutor] try except continue

2005-07-29 Thread Ewald Ertl
Hi! I think you just want to execute the rest of the function's in someProcedure(). Perhaps this could be the solution, what you want: >>> def someProcedure(): ... for func in [ someFunc00, someFunc01, someFunc02, someFunc03 ]: ... try: ... func() ...

Re: [Tutor] try except continue

2005-07-29 Thread Alan G
> I don't believe I am ignoring the error. If you log it and then immediately continue then you are ignoring it so far as the program is concerned. The logging is only significant to you after the program completes. > the error and pick up where it left off and keep going. > I gather you think th

Re: [Tutor] try except continue

2005-07-28 Thread Danny Yoo
> I see the continue statement as a way for me to do this, and thus > intimately linked with exception handling. Hi Tpc, I think you're getting too caught up with the word "continue" and its concept as you use it in human language, rather than what it really does in the Python language. I sho

Re: [Tutor] try except continue

2005-07-28 Thread tpc247
hi Alan and Danny, I think I understand what you guys are saying. I don't believe I am ignoring the error. Ideally what I'd like is for the procedural program to run through its steps and if it encounters an error, to log the error and pick up where it left off and keep going. I gather you thin

Re: [Tutor] try except continue

2005-07-28 Thread Danny Yoo
On Thu, 28 Jul 2005 [EMAIL PROTECTED] wrote: > ideally what I'd like is for my procedural program, when it runs through > its steps and encounters an error, to log the error and pick up where it > left off and keep going. > > so for the execution of someProcedure(), I want it to print the error

Re: [Tutor] try except continue

2005-07-28 Thread Alan G
> program, when it runs through its steps and encounters an error, to > log > the error and pick up where it left off and keep going. According > to this > link, 'continue' is allowed within an except or finally: Thats true but only if the try block is inside a loop. Consider: error = True de

Re: [Tutor] try except continue

2005-07-28 Thread tpc247
hi Danny, well from my previous message: ideally what I'd like is for my procedural program, when it runs through its steps and encounters an error, to log the error and pick up where it left off and keep going. so for the execution of someProcedure(), I want it to print the error and continue ru

Re: [Tutor] try except continue

2005-07-28 Thread Danny Yoo
On Thu, 28 Jul 2005 [EMAIL PROTECTED] wrote: > import testTryCatch > try: > testTryCatch.someProcedure() > except: > print "encountered error" > continue > > but I get the following error: > > SyntaxError: 'continue' not properl

[Tutor] try except continue

2005-07-28 Thread tpc
hey guys, so I've been trying to get my head around the try except statement in Python, and ideally what I'd like is for my procedural program, when it runs through its steps and encounters an error, to log the error and pick up where it left off and keep going. According to this link, 'continue'