Re: [Tutor] Error Handling in python

2014-07-25 Thread jitendra gupta
@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


Re: [Tutor] Error Handling in python

2014-07-24 Thread Wolfgang Maier

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 in python

2014-07-24 Thread Chris “Kwpolska” Warrick
On Thu, Jul 24, 2014 at 2:23 PM, Wolfgang Maier
 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 
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

2014-07-24 Thread Wolfgang Maier

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

2014-07-24 Thread Chris “Kwpolska” Warrick
On Thu, Jul 24, 2014 at 2:14 PM, Wolfgang Maier
 wrote:
> On 24.07.2014 14:09, Chris “Kwpolska” Warrick wrote:
>>
>> On Thu, Jul 24, 2014 at 2:01 PM, Wolfgang Maier
>>  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 
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

2014-07-24 Thread Wolfgang Maier

On 24.07.2014 14:09, Chris “Kwpolska” Warrick wrote:

On Thu, Jul 24, 2014 at 2:01 PM, Wolfgang Maier
 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

2014-07-24 Thread Chris “Kwpolska” Warrick
On Thu, Jul 24, 2014 at 2:01 PM, Wolfgang Maier
 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 
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

2014-07-24 Thread Steven D'Aprano
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 "", line 1, in 
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

2014-07-24 Thread Wolfgang Maier

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

2014-07-24 Thread Chris “Kwpolska” Warrick
On Thu, Jul 24, 2014 at 1:35 PM, jitendra gupta  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 
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