[Tutor] Error Handling in python

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

2014-07-24 Thread Chris “Kwpolska” Warrick
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

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

2014-07-24 Thread Chris “Kwpolska” Warrick
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

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

2014-07-24 Thread Chris “Kwpolska” Warrick
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

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: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

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


[Tutor] Inquiry

2014-07-24 Thread Allahondoum Mbaibarem
Please I would like to know about the Security and the Reliability of
Python. Thank you
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Security and Reliability of Python

2014-07-24 Thread Allahondoum Mbaibarem
I'm new at python and I would like to have knowledge about the Security and
the Reliability factor of Python thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Read a file, Load a dictionary

2014-07-24 Thread Glenn Lester
I have been looking around for a way to read a comma delimited csv file and
then load it into a dictionary. So far any of my usual sources don't deal
with such simple stuff.

My current code is


# create a dictionary (dict) to store the order # and Remark
testVariables = {}

# Read the file and load the dict
input_file = open('test1Comma.csv', 'rU')
for line in input_file:


I'm stuck on the code that comes next. what line of code loads the dict?

my csv file has 2 lines shown below.

AUTO-TEST-0021,REMARK 1
AUTO-TEST-0022,REMARK 2

Thanks in advance for the help.
-- 
http://www.avant.ca/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Security and Reliability of Python

2014-07-24 Thread Emile van Sebille

On 7/24/2014 3:11 AM, Allahondoum Mbaibarem wrote:


I'm new at python and I would like to have knowledge about the Security
and the Reliability factor of Python thank you.


That's a pretty open-ended question.  It's as secure and reliable as 
what you write.  For most of us, it's as secure and reliable as we need 
it to be.


:)

Emile


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read a file, Load a dictionary

2014-07-24 Thread Emile van Sebille

On 7/24/2014 3:50 PM, Glenn Lester wrote:


I have been looking around for a way to read a comma delimited csv file


start with the csv module.

http://www.python.org/doc/current/lib/module-csv.html

DESCRIPTION
This module provides classes that assist in the reading and writing
of Comma Separated Value (CSV) files...



and then load it into a dictionary.



Emile


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read a file, Load a dictionary

2014-07-24 Thread Dave Angel
Glenn Lester gles...@avant.ca Wrote in message:

 
?

You forgot to make your message a text one, and also omitted your
 Python version.  So I'll respond from memory,  assuming you're
 using version 3.5

The csv reader can make a dictionary from each line of the csv
 file. So you can readily make a list of dicts.


 https://docs.python.org/3.5/library/csv.html

-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Security and Reliability of Python

2014-07-24 Thread Dave Angel
Allahondoum Mbaibarem allahondoum1...@gmail.com Wrote in message:
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor
 

Post here using text messages,  not html.

Wait 24 hours, not one, before slamming us with a duplicate query.
  And when refining an existing thread,  use reply-list, not a new
 message with a different subject line.

Any further responses, please use the original thread.

-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inquiry

2014-07-24 Thread Dave Angel
Allahondoum Mbaibarem allahondoum1...@gmail.com Wrote in message:

 
(use text messages here)

Python is no more secure than the code written in it.

It is very reliable,  according to the experience of thousands of
 users. Much of that comes from it being open-source; many eyes
 catch the bugs faster than anything proprietary.
 

Compared to what?


-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inquiry

2014-07-24 Thread Danny Yoo
On Jul 24, 2014 3:21 PM, Allahondoum Mbaibarem allahondoum1...@gmail.com
wrote:

 Please I would like to know about the Security and the Reliability of
Python.

This is somewhat outside the domain of python-tutor discussion.  You may
want to contact the folks at:

http://www.pythonsecurity.org

instead.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read a file, Load a dictionary

2014-07-24 Thread Deb Wyatt
 assuming you're
  using version 3.5
 

How do you get version 3.5?  Python.org shows 3.41 as being the latest.

Deb in WA, USA


FREE ONLINE PHOTOSHARING - Share your photos online with your friends and 
family!
Visit http://www.inbox.com/photosharing to find out more!


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



Re: [Tutor] Read a file, Load a dictionary

2014-07-24 Thread Dave Angel
Deb Wyatt codemon...@inbox.com Wrote in message:
  assuming you're
  using version 3.5
 
 
 How do you get version 3.5?  Python.org shows 3.41 as being the latest.
 
 

I could as easily figured 2.6.  My point is that people need to
 specify what version they're asking about. 

3.5 is a dev version.  Not for production nor for learning python 
 one gets it by fetching from the repository and doing their own
 compile. 


-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read a file, Load a dictionary

2014-07-24 Thread Steven D'Aprano
On Thu, Jul 24, 2014 at 10:25:59PM -0400, Dave Angel wrote:
 Deb Wyatt codemon...@inbox.com Wrote in message:
   assuming you're  using version 3.5
  
  How do you get version 3.5?  Python.org shows 3.41 as being the latest.
 
 I could as easily figured 2.6.  My point is that people need to
  specify what version they're asking about. 

Does the version number make much of a difference in this case? 

 3.5 is a dev version.  Not for production nor for learning python 
  one gets it by fetching from the repository and doing their own
  compile. 

Do you figure that many beginners to Python are doing that?

-- 
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Security and Reliability of Python

2014-07-24 Thread Steven D'Aprano
On Thu, Jul 24, 2014 at 10:11:41AM +, Allahondoum Mbaibarem wrote:

 I'm new at python and I would like to have knowledge about the Security and
 the Reliability factor of Python thank you.

Python is very reliable. The language has been around for over 20 years, 
and is in use in tens of thousands if not more sites. Python is actively 
maintained, so when problems are reported, they are dealt with promptly.

But of course it is a programming language, which means the reliablity 
of code you write depends on *your* skill at programming. If you write 
buggy code, Python cannot save you from your own errors.

However, unlike low-level languages like C, you should not be able to 
cause a core dump or operating-system crash from Python code. (If you 
ever do find one of those, except for the ctypes module which is 
special, it is a bug in Python and should be reported immediately. But 
you won't: I've been using Python for over 15 years and have never 
managed to cause a core dump from Python code.)

Likewise, in Python you cannot have dangling pointer errors, buffer 
overflows, or any of those similar critical errors which lead to 
security failures. The worst you can have is an uncaught exception, 
which causes the Python process to write a traceback to standard error 
and exit.

Python is only as secure as the code *you* write. If you write code 
where you accept text from untrusted people over the Internet and then 
execute it as code using eval() or exec(), then your code is vulnerable 
to code injection attacks. The solution to this is simple: don't use 
eval() or exec() on untrusted data. There is hardly ever a need to use 
eval() or exec() in your own code. In 15 years, I've only used them a 
handful of times, and then mostly for experiments.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Security and Reliability of Python

2014-07-24 Thread Danny Yoo
 Python is only as secure as the code *you* write. If you write code
 where you accept text from untrusted people over the Internet and then
 execute it as code using eval() or exec(), then your code is vulnerable
 to code injection attacks. The solution to this is simple: don't use
 eval() or exec() on untrusted data. There is hardly ever a need to use
 eval() or exec() in your own code. In 15 years, I've only used them a
 handful of times, and then mostly for experiments.


And we have to fight the good fight.  There are people out there who
think that eval() is fine to teach to beginners.  I do not understand
why.  As a concrete example that I came across today:

https://plus.google.com/111222510165686226339/posts/jQrn9vkGxHA

Such teaching makes me very sad.  We have to really fight this hard to
keep people from writing dangerous code.  It's a bit frustrating
because the teacher there obviously knows enough to be dangerous, yet
not enough to be respectfully cautious.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor