Re: [Tutor] Error Handling in python
@All Thanks a lot, Yes set -e It work fine. This is what I am looking for but I got some extra things to learn :) . Thanks you again Thanks Jitendra On Thu, Jul 24, 2014 at 6:10 PM, Wolfgang Maier wolfgang.ma...@biologie.uni-freiburg.de wrote: On 24.07.2014 14:37, Chris “Kwpolska” Warrick wrote: It’s recommended to switch to the [[ syntax anyways, some people consider [ deprecated. Also, [ is actually /bin/[ while [[ lives in your shell (and is therefore faster). About the equals sign, == is the preferred syntax, and = is also considered deprecated (zsh explicitly says so, bash says “only for POSIX compatibility”. I see. There is always something to learn, thanks (even if it's not Python-related as Steven points out correctly) :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Error Handling in python
Hi All My shell script is not throwing any error when I am having some error in Python code. test.py ~~ def main(): print Test #some case error need to be thrown raise Exception(Here is error) if __name__ == __main__ main() ~~ second.py ~~ def main(): print Second function is called if __name__ == __main__ main() ~~ ~ shellTest.sh ~~~ python test.py python second.py ~~~ In this case, I dont want to run my second.py Even I am throwing error from my test.py, but still second.py is getting executed, which i dont want, Thanks Regards Jitendra ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On Thu, Jul 24, 2014 at 1:35 PM, jitendra gupta jitu.ic...@gmail.com wrote: Hi All My shell script is not throwing any error when I am having some error in Python code. In this case, I dont want to run my second.py Even I am throwing error from my test.py, but still second.py is getting executed, which i dont want, You must expilicitly ask your shell to do exit if something fails. Like this: ~ shellTest.sh ~~~ #!/bin/bash set -e python test.py python second.py ~~~ I explicitly set the shell to /bin/bash on the shebang line, and then set the -e option to fail when a command exits with a non-zero status. You should always have a shebang line in your shell files, and execute them as ./shellTest.sh (after chmod +x shellTest.sh); moreover, one for Python files is recommended, like this: #!/usr/bin/env python -- Chris “Kwpolska” Warrick http://chriswarrick.com/ PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On 24.07.2014 13:35, jitendra gupta wrote: Hi All My shell script is not throwing any error when I am having some error in Python code. test.py ~~ def main(): print Test #some case error need to be thrown raise Exception(Here is error) if __name__ == __main__ main() ~~ second.py ~~ def main(): print Second function is called if __name__ == __main__ main() ~~ ~ shellTest.sh ~~~ python test.py python second.py ~~~ In this case, I dont want to run my second.py Even I am throwing error from my test.py, but still second.py is getting executed, which i dont want, Your shell script calls runs the two Python scripts separately, that is, it first starts a Python interpreter telling it to run test.py . When that is done (with whatever outcome !), it starts the interpreter a second time telling it to run second.py now. The exception stops the execution of test.py, of course, and causes the interpreter to return, but your shell script is responsible for checking the exit status of the first script if it wants to run the second call only conditionally. Try something like this (assuming bash): python test.py if [ $? = 0 ]; then python second.py fi as your shell script. By the way, both Python scripts you posted contain a syntax error, but I leave spotting it up to you. Best, Wolfgang ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On Thu, Jul 24, 2014 at 05:05:24PM +0530, jitendra gupta wrote: Hi All My shell script is not throwing any error when I am having some error in Python code. This is a question about the shell, not about Python. I'm not an expert on shell scripting, but I'll try to give an answer. ~ shellTest.sh ~~~ python test.py python second.py One fix is to check the return code of the first python process: [steve@ando ~]$ python -c pass [steve@ando ~]$ echo $? 0 [steve@ando ~]$ python -c raise Exception Traceback (most recent call last): File string, line 1, in module Exception [steve@ando ~]$ echo $? 1 Remember that to the shell, 0 means no error and anything else is an error. So your shell script could look like this: python test.py if [ $? -eq 0 ] then python second.py fi Another way (probably better) is to tell the shell to automatically exit if any command fails: set -e python test.py python second.py Hope this helps, -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On Thu, Jul 24, 2014 at 2:01 PM, Wolfgang Maier wolfgang.ma...@biologie.uni-freiburg.de wrote: Try something like this (assuming bash): python test.py if [ $? = 0 ]; then python second.py fi as your shell script. The [ ] and = should be doubled. But all this is not needed, all you need is: python test.py python second.py However, you need to explicitly stack all the commands you want to execute this way — so, if there are more things, `set -e` might also be of use. (you would need an even uglier tree for `if`s.) -- Chris “Kwpolska” Warrick http://chriswarrick.com/ PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On 24.07.2014 14:09, Chris “Kwpolska” Warrick wrote: On Thu, Jul 24, 2014 at 2:01 PM, Wolfgang Maier wolfgang.ma...@biologie.uni-freiburg.de wrote: Try something like this (assuming bash): python test.py if [ $? = 0 ]; then python second.py fi as your shell script. The [ ] and = should be doubled. ?? why that ? But all this is not needed, all you need is: python test.py python second.py I agree, that's far more elegant in this case. However, you need to explicitly stack all the commands you want to execute this way — so, if there are more things, `set -e` might also be of use. (you would need an even uglier tree for `if`s.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On Thu, Jul 24, 2014 at 2:14 PM, Wolfgang Maier wolfgang.ma...@biologie.uni-freiburg.de wrote: On 24.07.2014 14:09, Chris “Kwpolska” Warrick wrote: On Thu, Jul 24, 2014 at 2:01 PM, Wolfgang Maier wolfgang.ma...@biologie.uni-freiburg.de wrote: Try something like this (assuming bash): python test.py if [ $? = 0 ]; then python second.py fi as your shell script. The [ ] and = should be doubled. ?? why that ? Double brackets can do more: http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces -- Chris “Kwpolska” Warrick http://chriswarrick.com/ PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On 24.07.2014 14:19, Chris “Kwpolska” Warrick wrote: python test.py if [ $? = 0 ]; then python second.py fi as your shell script. The [ ] and = should be doubled. ?? why that ? Double brackets can do more: http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces But more is not required here. What am I missing ? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On Thu, Jul 24, 2014 at 2:23 PM, Wolfgang Maier wolfgang.ma...@biologie.uni-freiburg.de wrote: On 24.07.2014 14:19, Chris “Kwpolska” Warrick wrote: python test.py if [ $? = 0 ]; then python second.py fi as your shell script. The [ ] and = should be doubled. ?? why that ? Double brackets can do more: http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces But more is not required here. What am I missing ? It’s recommended to switch to the [[ syntax anyways, some people consider [ deprecated. Also, [ is actually /bin/[ while [[ lives in your shell (and is therefore faster). About the equals sign, == is the preferred syntax, and = is also considered deprecated (zsh explicitly says so, bash says “only for POSIX compatibility”. -- Chris “Kwpolska” Warrick http://chriswarrick.com/ PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling in python
On 24.07.2014 14:37, Chris “Kwpolska” Warrick wrote: It’s recommended to switch to the [[ syntax anyways, some people consider [ deprecated. Also, [ is actually /bin/[ while [[ lives in your shell (and is therefore faster). About the equals sign, == is the preferred syntax, and = is also considered deprecated (zsh explicitly says so, bash says “only for POSIX compatibility”. I see. There is always something to learn, thanks (even if it's not Python-related as Steven points out correctly) :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error handling
Michael, On Sat, 2012-03-24 at 15:20 -0700, Michael Lewis wrote: [...] It is perhaps worth noting that in Python 3, the syntax has changed: import os, errno try: os.makedirs('a/b/c') except OSError, e: except OSError as e : if e.errno != errno.EEXIST: raise This as syntax works in 2.6 and 2.7 so is probably the syntax to use unless you have to use very old versions of Python. I think the as syntax makes it clearer that e is a variable referring to the instance of OSError that causes the except clause to execute if it does. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error handling
On 25/03/2012 08:22, Russel Winder wrote: Michael, On Sat, 2012-03-24 at 15:20 -0700, Michael Lewis wrote: [...] It is perhaps worth noting that in Python 3, the syntax has changed: import os, errno try: os.makedirs('a/b/c') except OSError, e: except OSError as e : if e.errno != errno.EEXIST: raise This as syntax works in 2.6 and 2.7 so is probably the syntax to use unless you have to use very old versions of Python. I think the as syntax makes it clearer that e is a variable referring to the instance of OSError that causes the except clause to execute if it does. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor It's worth noting that PEP 3151 has been implemented in Python 3.3 see http://docs.python.org/dev/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy Quoting from the above link. Thanks to the new exceptions, common usages of the errno can now be avoided. For example, the following code written for Python 3.2: from errno import ENOENT, EACCES, EPERM try: with open(document.txt) as f: content = f.read() except IOError as err: if err.errno == ENOENT: print(document.txt file is missing) elif err.errno in (EACCES, EPERM): print(You are not allowed to read document.txt) else: raise can now be written without the errno import and without manual inspection of exception attributes: try: with open(document.txt) as f: content = f.read() except FileNotFoundError: print(document.txt file is missing) except PermissionError: print(You are not allowed to read document.txt) -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Error Handling
In the below block, why is the if statement e.errno != errno.EEXIST? Why can the errno be both before and after the .? import os, errno try: os.makedirs('a/b/c') except OSError, e: if e.errno != errno.EEXIST: raise -- Michael J. Lewis mjole...@gmail.com 415.815.7257 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Handling
On Sun, Mar 25, 2012 at 12:54 PM, Michael Lewis mjole...@gmail.com wrote: In the below block, why is the if statement e.errno != errno.EEXIST? Why can the errno be both before and after the .? import os, errno try: os.makedirs('a/b/c') except OSError, e: if e.errno != errno.EEXIST: raise errno is a dictionary of standard errors. You can learn about it in the python shell by importing errno then typing help(errno) or dir(errno). So, errno.EEXIST is just the number 17 as it turns out e is an exception object. It has an attribute called errno which identifies what the error number is. So this is just comparing what your actual error is to the EEXIST number. If it isn't that, then you 'raise' -- Michael J. Lewis mjole...@gmail.com 415.815.7257 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Error handling
Hi everyone, I am having a bit of trouble understanding what is going on below. What does the e in except OSError, e: do? Any other help you can provide regarding errno would be extremely appreciated. I've done help() and dir() on it, but I am not really understanding what's going on with e.errno != errno.EEXIST: Thanks. import os, errno try: os.makedirs('a/b/c') except OSError, e: if e.errno != errno.EEXIST: raise I am in the process of writing a script to move files from one directory to another. I am supplying both the source and destination directories at runtime. I want to create the destination file on the fly; however, if it already exists, I want to handle that error/ignore that error. Also, if the the same file exists in both the source and destination directory, I'd like to override the one in the destination directory with the one in the source directory. I am having trouble with the error handling portion. Here is a pastebin for my source code: http://pastebin.com/EU578xQs ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error handling
I am having a bit of trouble understanding what is going on below. What does the e in except OSError, e: do? Any other help you can provide regarding errno would be extremely appreciated. I've done help() and dir() on it, but I am not really understanding what's going on with e.errno != errno.EEXIST: Basically, that `except` block is catching all exceptions of type OSError, and storing the exception in variable `e`. This variable does not have to be called `e`, but that's the most commonly-used variable name. Once you have the exception stored (in this case in the variable `e`), you can then see what type of exception, using the `errno` property of the exception. You can read about the different types here: http://docs.python.org/library/errno.html import os, errno try: os.makedirs('a/b/c') except OSError, e: if e.errno != errno.EEXIST: raise In this particular section, it's catching any OSError, and then if it turns out that the error was File Exists, it is raising that exception again, to be either caught by an encapsulating try block, or which will bring the program to a halt with an exception shown by the interpreter. Is that the behavior you are going for? Any more confusion? -- Colton Myers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error handling
On Sat, Mar 24, 2012 at 3:51 PM, Colton Myers colton.my...@gmail.comwrote: I am having a bit of trouble understanding what is going on below. What does the e in except OSError, e: do? Any other help you can provide regarding errno would be extremely appreciated. I've done help() and dir() on it, but I am not really understanding what's going on with e.errno != errno.EEXIST: Basically, that `except` block is catching all exceptions of type OSError, and storing the exception in variable `e`. This variable does not have to be called `e`, but that's the most commonly-used variable name. Once you have the exception stored (in this case in the variable `e`), you can then see what type of exception, using the `errno` property of the exception. You can read about the different types here: http://docs.python.org/library/errno.html import os, errnotry: os.makedirs('a/b/c')except OSError, e: if e.errno != errno.EEXIST: raise In this particular section, it's catching any OSError, and then if it turns out that the error was File Exists, it is raising that exception again, to be either caught by an encapsulating try block, or which will bring the program to a halt with an exception shown by the interpreter. Is that the behavior you are going for? Any more confusion? Why wouldn't it be errno.e instead of e.errno? -- Colton Myers -- Michael J. Lewis mjole...@gmail.com 415.815.7257 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error handling
That is because 'errno' is a property on the exception object called 'e', not the other way around. On Sun, Mar 25, 2012 at 7:12 AM, Michael Lewis mjole...@gmail.com wrote: On Sat, Mar 24, 2012 at 3:51 PM, Colton Myers colton.my...@gmail.comwrote: I am having a bit of trouble understanding what is going on below. What does the e in except OSError, e: do? Any other help you can provide regarding errno would be extremely appreciated. I've done help() and dir() on it, but I am not really understanding what's going on with e.errno != errno.EEXIST: Basically, that `except` block is catching all exceptions of type OSError, and storing the exception in variable `e`. This variable does not have to be called `e`, but that's the most commonly-used variable name. Once you have the exception stored (in this case in the variable `e`), you can then see what type of exception, using the `errno` property of the exception. You can read about the different types here: http://docs.python.org/library/errno.html import os, errnotry: os.makedirs('a/b/c')except OSError, e: if e.errno != errno.EEXIST: raise In this particular section, it's catching any OSError, and then if it turns out that the error was File Exists, it is raising that exception again, to be either caught by an encapsulating try block, or which will bring the program to a halt with an exception shown by the interpreter. Is that the behavior you are going for? Any more confusion? Why wouldn't it be errno.e instead of e.errno? -- Colton Myers -- Michael J. Lewis mjole...@gmail.com 415.815.7257 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Regards, Sithembewena Lloyd Dube ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error-handling for a large modular program
Our configuration language has evolved a bit more over the last few days and I've decided to right a recursive descent parser for it. Since this is a learning project for me, I'm writing the parser myself instead of using one of the available packages. I'll see how this affects the error handling. -- The ByteBaker : http://www.bytebaker.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error-handling for a large modular program
Yes I am aware of the various Python parser packages. But at the current moment the language is changing so I'm just writing a parser as I go along. It probably isn't very good as it combines syntactical analysis with some calculations as it goes along (which isn't really good practice). Once the language design is finalized, I might look into using a proper parser and performing the calculations separately. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error-handling for a large modular program
Shrutarshi Basu [EMAIL PROTECTED] wrote I'm currently working on a research project where we'll be developing a moderately complex piece of software. We're designing with extensibility in mind. One of the problems I can see right now is that our program can potentially create a large number of very different errors, some fatal, some not. That's not unusual. I have worked on projects with over a thousand exception classes (in C++ and Java). I've only implemented basic error handling through a number of try/except blocks and if/elses. Thats the best way to proceed but... However I feel that the code might become increasingly inelegant if I keep adding a try/except for every possible error (and breaking up the code to make it be a specific as possible about the errors). Is there some more generic, high-level approach I could use? How are you using try/except. One of the big advantages of try/except is that it does not break up your main code flow. All the excepts sit outside of that. If you put your main code in a function that raises the errors then the error handling can come out to a higher level with try: mainprocessingHere() except error1: ... except error2: ... ... except error999: You can also make things somewhat more manageable by designing your error heirarchy carefully to use inheritance to catch superclass errors then in the handlers using if/else to determine the solution - seeral subclasses may share a solution. But to be honest I prefer the explicit except error1,error2,error3: approach for that. One approach I have thought of is creating a error handler class where each class method corresponds to a particular error. An instance of this class would be created at the start of the program and each error would run a method in the class. But if you extend your program you have to modify your error handler class to add a new method and your main code will have dependencies on the class throughout. And if you reuse the class in other related projects they will be impacted too. By keeping each exception class distinct its easy to add new error types without affecting existing code too much and not affecting other projects at all. error handling code, without cluttering the actual working code. I'm concerned at the fact you seeem to be cluttering the code. The point of try/except is to keep the main code as uncluttered as possible. Of course sometimes you want to handle an error in-flow and an inline try/except of if/else is unavoidable but those are usually minority cases. still need try/catch blocks, but the catches wouldn't be anything more than method calls. They can still be that. Or simpler use vanilla functions. I'm not a believer in writing classes just as a hold-all for a set of functions. Thats what Python modules are for! But your functions should probably aim to handle several related error types. Since we have 3-4 people, each working on a different part of the program, we could each add to this error handler as needed. Yes, if its a module it can hold the exception classes and the handler functions. In fact if you want to be fancy you could put the handler in the exception class but personally I think thats a bad idea since the handler might be diffrent in other projects. I prefer to keep the handlers and exceptions separate. Since this is my first real world application that I'm writing as a team, I have no idea how error handling is actually done in large applications. Its not uncommon in large apps for the error handling to be more than half the code. When new grads or other inexperienced programmers join a big project for the first time they are often amazed at how much error code there is. The key is to keep it together and as far away from the main flow as possible and that's what try/except gives you. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error-handling for a large modular program
Shrutarshi Basu wrote: the front end of the program is essentially a parser for a moderately complex configuration language, which means that there are a variety of syntax/semantics errors possible. In my experience with parsers there is little or no need for try-except blocks. Could you give us an example of how you'd use try-except in your parser? Also are you aware that there are a lot of parser programs out there (some in Python) that might save you time / effort? -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Error-handling for a large modular program
I'm currently working on a research project where we'll be developing a moderately complex piece of software. We're designing with extensibility in mind. One of the problems I can see right now is that our program can potentially create a large number of very different errors, some fatal, some not. Part of this is because the front end of the program is essentially a parser for a moderately complex configuration language, which means that there are a variety of syntax/semantics errors possible. Since the project is in the beginning stages, I've only implemented basic error handling through a number of try/except blocks and if/elses. However I feel that the code might become increasingly inelegant if I keep adding a try/except for every possible error (and breaking up the code to make it be a specific as possible about the errors). Is there some more generic, high-level approach I could use? One approach I have thought of is creating a error handler class where each class method corresponds to a particular error. An instance of this class would be created at the start of the program and each error would run a method in the class. This would allow us to have elaborate error handling code, without cluttering the actual working code. We'd still need try/catch blocks, but the catches wouldn't be anything more than method calls. Since we have 3-4 people, each working on a different part of the program, we could each add to this error handler as needed. Since this is my first real world application that I'm writing as a team, I have no idea how error handling is actually done in large applications. So any suggestions would be appreciated. Thanks for your help, Shrutarshi -- The ByteBaker : http://www.bytebaker.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor