Re: [Tutor] Which version of python should i use?

2013-05-21 Thread Albert-Jan Roskam
> Subject: Re: [Tutor] Which version of python should i use?

> 
> Hello Amal,
> 
> On Mon, May 20, 2013 at 11:24 PM, Amal Thomas  
> wrote:
>>  Thank you very much..!! I am starting to learn python for my Bioinformatics
>>  work, so I would look for the version that has libraries helpful for me..
> 
> Do you already have any libraries in mind (or aware of) that  you
> would want to use? I came across this link:
> http://intro-prog-bioinfo-2012.wikispaces.com/, which doesn't seem to
> use any specific tools other than the "generic" libraries that are
> pretty much common in any scientific work involving Python: SciPy,
> Numpy, etc.


Also check out Python(x, y) (http://code.google.com/p/pythonxy/): "Python(x,y) 
is a free scientific and engineering development software for numerical 
computations, data analysis and data visualization based on Python programming 
language, Qt graphical user interfaces and Spyder interactive scientific 
development environment."

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


Re: [Tutor] reducing lists within list to their set of unique values

2013-05-21 Thread Treder, Robert
> Message: 6
> Date: Tue, 21 May 2013 09:45:17 +1000
> From: Steven D'Aprano 
> To: tutor@python.org
> Subject: Re: [Tutor] reducing lists within list to their set of unique
>   values
> Message-ID: <519ab58d.9020...@pearwood.info>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> On 21/05/13 08:49, Treder, Robert wrote:
>> Hi python folks,
>>
>> I have a list of lists that looks something like this:
>>
>> tst = [ [], ['test'], ['t1', 't2'], ['t1', 't1', 't2'] ]
>>
>> I want to change the empty sets to a blank string, i.e., '' and the lists 
>> with repeat values to the unique set of values. So I have done the >> 
>> following:
>>
> for t in tst:
>>  if len(t) == 0:
>>  tst.__setitem__(tst.index(t), '')
>>  else:
>>  tst.__setitem__(tst.index(t), set(t))
>
>
> As a general rule, if you are writing double-underscore special methods like 
> __setitem__ directly, you're doing it wrong. (There are
> exceptions, but consider them "for experts".)
>
> So instead of tst.__setitem__(a, b) you should write tst[a] = b.
>
> But that's still the wrong way to do this! You're doing a lot of extra work 
> with the calls to tst.index. You won't notice for a short list like > the 
> example above, but for a long list, this will get really, really slow.
>
> The way to do this is to keep track of the index as you walk over the list, 
> and not recalculate it by searching the list:
>
>
> for index, item in enumerate(tst):
> if item == []:
> item = ""
> else:
> item = list(set(item))
> tst[index] = item
>
>
> Notice that I call set() to get the unique values, then list() again to turn 
> it back into a list. This does the job you want, but it is not 
> guaranteed to keep the order:
>
> py> L = ['b', 'd', 'c', 'a', 'b']
> py> list(set(L))
> ['a', 'c', 'b', 'd']
>
>
> If keeping the order is important, you cannot use set, and you'll need 
> another way to extract only the unique values. Ask if you need help on 
> that.

Thanks, Steven. Very helpful. It looks like the order is only changed on the 
inner list that set() is applied to, not on the outer list since the outer list 
order is controlled by index. For this application I don't care about the order 
of the inner lists. However there are other applications where that will be 
import. Can you please describe the alternate method for extracting the unique 
values that maintains order. 

Thanks, 
Bob

>
>
>
>> What I get in return is
>>
> tst
>> ['', set(['test']), set(['t2', 't1']), set(['t2', 't1'])]
>>
>> The empty list is fine but the other lists seem to be expressions rather 
>> than values. What do I need to do to simply get the values back 
>> liike the following?
>>
>> ['', ['test'], ['t2', 't1'], ['t2', 't1']]
>
>
> They are values. It is just that they are *sets* rather than *lists*. When 
> printed, lists have a nice compact representation using square 
> brackets [], but unfortunately sets do not. However, if you upgrade to Python 
> 3, they have been upgraded to look a little nicer:
>
>
> # Python 2:
> set(['a', 'c', 'b', 'd'])
> 
> # Python 3
> {'d', 'b', 'c', 'a'}
>
>
> Notice that the order of the items is not guaranteed, but apart from that, 
> the two versions are the same despite the difference in print 
> representation.
>
> -- 
> Steven






NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or 
views contained herein are not intended to be, and do not constitute, advice 
within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and 
Consumer Protection Act. If you have received this communication in error, 
please destroy all electronic and paper copies and notify the sender 
immediately. Mistransmission is not intended to waive confidentiality or 
privilege. Morgan Stanley reserves the right, to the extent permitted under 
applicable law, to monitor electronic communications. This message is subject 
to terms available at the following link: 
http://www.morganstanley.com/disclaimers. If you cannot access these links, 
please notify us by reply message and we will send the contents to you. By 
messaging with Morgan Stanley you consent to the foregoing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reducing lists within list to their set of unique values

2013-05-21 Thread Oscar Benjamin
On 21 May 2013 14:31, Treder, Robert  wrote:
> Steven wrote:
>>
>> py> L = ['b', 'd', 'c', 'a', 'b']
>> py> list(set(L))
>> ['a', 'c', 'b', 'd']
>>
>>
>> If keeping the order is important, you cannot use set, and you'll need 
>> another way to extract only the unique values. Ask if you need help on
>> that.
>
> Thanks, Steven. Very helpful. It looks like the order is only changed on the 
> inner list that
> set() is applied to, not on the outer list since the outer list order is 
> controlled by index.
> For this application I don't care about the order of the inner lists. However 
> there are other
> applications where that will be import. Can you please describe the alternate 
> method for
> extracting the unique values that maintains order.

There isn't necessarily a uniquely defined ordering. Here's a function
that preserves the order of the first occurrences of each element in
each list:

def uniquify(original):
new = []
seen = set()
for item in original:
if item not in seen:
new.append(item)
seen.add(item)
return new

>>> uniquify([1, 2, 3, 1, 2, 5])
[1, 2, 3, 5]


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


[Tutor] Hi Folks...Need help with a modified version of the Subset sum problem.

2013-05-21 Thread spiff007
Hi there Tutor folks

I need your help with a modified version of the subset sum problem [
http://en.wikipedia.org/wiki/Subset_sum_problem].

The problem i am facing is a bit hard to describe (as most complex problem
always are :D ), so please bear with my longish articulation :)

Here it goes :

After scrubbing some input data, i have two lists 'minutes' (elements are
positive integers) & 'names' (elements are strings). They are the exact
same length and each element of one corresponds exactly with the respective
element of the other.

The elements in the minutes list are not unique; a sample list is :
minutes = [60, 45, 30, 45, 45, 5, 60, 45, 30, 30, 45, 60, 60, 45, 30, 30,
60, 30, 30 ]
names = ['abc', 'ghi', 'jkl', 'def', 'zab', 'wux', ... ]

Now i need to pick some elements from the 'minutes' list (i.e. a subset of
minutes) which add up to a certain sum ('target_sum1').
I have to then do some processing with the *respective elements from the
'names' list, corresponding to, the elements i just picked from the
'minutes' list which sum upto target_sum1.*

I now have to remove these elements i picked (which add up to 'target_sum1'
) from the 'minutes' list, and essentially do a second pass, picking some
elements, which add up to a certain other sum ('target_sum2'). Again, *retrieve
the elements from the 'names' list*, *corresponding to the elements i just
picked which sum upto 'target_sum2'*,  & do some processing on them.  And
so  on.

Picking a subset of numbers, which add up to a certain target sum, is a
well-known problem[1] & i have found the following code for it [2] :

I also have the code on a github gist
https://gist.github.com/anonymous/5620886

def subset_sum_recursive(numbers,target,partial):
s = sum(partial)

#check if the partial sum is equals to target
if s == target:
print "sum(%s)=%s"%(partial,target)
if s >= target:
return # if we reach the number why bother to continue

for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i+1:]
subset_sum_recursive(remaining,target,partial + [n])

def subset_sum(numbers,target):
#we need an intermediate function to start the recursion.
#the recursion starts with an empty list  as partial solution.
subset_sum_recursive(numbers,target,list())

if __name__ == "__main__":
minutes = [3,9,8,4,5,7,10]
target_sum = 15
subset_sum(minutes,target_sum)

#Outputs:
#sum([3, 8, 4])=15
#sum([3, 5, 7])=15
#sum([8, 7])=15
#sum([5, 10])=15


This above code returns the elements of the 'minutes' list(subset of the
'minutes' list)  which add up to 'target_sum'.

*I am stuck on this point : *
*I am unable to determine the list indices, of the elements of 'minutes'
which add upto 'target_sum1', so i can retrieve the corresponding elements
from the 'names' list, & do my processing on them.*
*
*
*I also need the list indices to remove the elements which add upto
target_sum1, and then run a "second pass", to determine the elements which
add upto 'target_sum2'.*

Any & all explanations/links/code
snippets/thoughts/ideas/suggestions/feedback/comments/ of the Python tutor
community would be greatly appreciated.

Thanks a ton,
calvin
spiff...@gmail.com



References

1. http://en.wikipedia.org/wiki/Subset_sum_problem
2. http://stackoverflow.com/a/4633515/559456
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi Folks...Need help with a modified version of the Subset sum problem.

2013-05-21 Thread Peter Otten
spiff007 wrote:

> Hi there Tutor folks
> 
> I need your help with a modified version of the subset sum problem [
> http://en.wikipedia.org/wiki/Subset_sum_problem].
> 
> The problem i am facing is a bit hard to describe (as most complex problem
> always are :D ), so please bear with my longish articulation :)
> 
> Here it goes :
> 
> After scrubbing some input data, i have two lists 'minutes' (elements are
> positive integers) & 'names' (elements are strings). They are the exact
> same length and each element of one corresponds exactly with the
> respective element of the other.
> 
> The elements in the minutes list are not unique; a sample list is :
> minutes = [60, 45, 30, 45, 45, 5, 60, 45, 30, 30, 45, 60, 60, 45, 30, 30,
> 60, 30, 30 ]
> names = ['abc', 'ghi', 'jkl', 'def', 'zab', 'wux', ... ]
> 
> Now i need to pick some elements from the 'minutes' list (i.e. a subset of
> minutes) which add up to a certain sum ('target_sum1').
> I have to then do some processing with the *respective elements from the
> 'names' list, corresponding to, the elements i just picked from the
> 'minutes' list which sum upto target_sum1.*
> 
> I now have to remove these elements i picked (which add up to
> 'target_sum1' ) from the 'minutes' list, and essentially do a second pass,
> picking some elements, which add up to a certain other sum
> ('target_sum2'). Again, *retrieve the elements from the 'names' list*,
> *corresponding to the elements i just
> picked which sum upto 'target_sum2'*,  & do some processing on them.  And
> so  on.
> 
> Picking a subset of numbers, which add up to a certain target sum, is a
> well-known problem[1] & i have found the following code for it [2] :
> 
> I also have the code on a github gist
> https://gist.github.com/anonymous/5620886
> 
> def subset_sum_recursive(numbers,target,partial):
> s = sum(partial)
> 
> #check if the partial sum is equals to target
> if s == target:
> print "sum(%s)=%s"%(partial,target)
> if s >= target:
> return # if we reach the number why bother to continue
> 
> for i in range(len(numbers)):
> n = numbers[i]
> remaining = numbers[i+1:]
> subset_sum_recursive(remaining,target,partial + [n])
> 
> def subset_sum(numbers,target):
> #we need an intermediate function to start the recursion.
> #the recursion starts with an empty list  as partial solution.
> subset_sum_recursive(numbers,target,list())
> 
> if __name__ == "__main__":
> minutes = [3,9,8,4,5,7,10]
> target_sum = 15
> subset_sum(minutes,target_sum)
> 
> #Outputs:
> #sum([3, 8, 4])=15
> #sum([3, 5, 7])=15
> #sum([8, 7])=15
> #sum([5, 10])=15
> 
> 
> This above code returns the elements of the 'minutes' list(subset of the
> 'minutes' list)  which add up to 'target_sum'.
> 
> *I am stuck on this point : *
> *I am unable to determine the list indices, of the elements of 'minutes'
> which add upto 'target_sum1', so i can retrieve the corresponding elements
> from the 'names' list, & do my processing on them.*

I think the easiest approach is to combine the two lists into a single one

pairs = zip(names, minutes)

or, if you really need the indices

pairs = list(enumerate(minutes))

and invoke subset_sum() with these pairs instead of minutes alone.
Of course you have to modify the sum() call to unpack the pairs:

s = sum(value for index, value in partial)

> *I also need the list indices to remove the elements which add upto
> target_sum1, and then run a "second pass", to determine the elements which
> add upto 'target_sum2'.*
> 
> Any & all explanations/links/code
> snippets/thoughts/ideas/suggestions/feedback/comments/ of the Python tutor
> community would be greatly appreciated.
> 
> Thanks a ton,
> calvin
> spiff...@gmail.com
> 
> 
> 
> References
> 
> 1. http://en.wikipedia.org/wiki/Subset_sum_problem
> 2. http://stackoverflow.com/a/4633515/559456


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


[Tutor] Configuring Emacs for Python development?

2013-05-21 Thread boB Stepp
Okay. Since I first joined this list I have played around (as time
permitted) with these editors/IDEs: PyCharm, Eclipse with PyDev,
Notepad++, Emacs, IDLE, PyScripter, IdleX, Sublime Text, and possibly
others. Of course, I have far from mastered any of them, but I think
that I have a sense of what each is capable of plus an idea of their
quirks/bugs. I have found that I do not entirely like any of them. I
suppose that is normal. Of the above list I like Notepad++, PyCharm
and Emacs the best. I have decided to eliminate Notepad++ due to it
being tied entirely to Windows and that it does not have the power of
Emacs. I liked PyCharm enough that when a special Earth Day sale came
out, I purchased a license for it. Eventually, I think that I will
gravitate to this if I start creating truly large projects, but I
cannot use it at work. And it seems to be overkill for just beginning
in Python. And I don't like the idea of creating a project for what
amounts to one-off scripts.

So I am led to Emacs. I like the ability to keep my fingers on the
keyboard for almost everything I might want to do. And PyCharm has an
ever-improving Emacs keybinding mode for when I do start using it. It
SHOULD be available at work on the two Solaris 10 systems I do most of
my work on, but I found out today that while the binary for Emacs
exists on both systems, not all of the dependencies are installed.
However, I am hoping to get permission to install a fresh, latest,
greatest version of Emacs on these systems, but even if I don't
Googling has revealed that Emacs can edit remote files via something
called Tramp. So it looks like I could install Emacs on my Windows PC
at work and edit from there. Likewise at home I am on Windows 7-64 bit
Pro and have already installed GNU Emacs 24.3.

When I last mentioned Emacs there was at least one offer to help me
optimally configure Emacs for Python development on Windows. I am
currently using version 3.3. This does not appear straightforward to
me. When I Google for this topic, people have taken different
approaches and I do not have the background or experience to judge
which would work best for me. Additionally this latest version of
Emacs apparently has made some improvements to the Python.el mode and
it is not clear to me what suggestions will or will not work with
this.

Anyone care to help me with this?

As always, thanks!
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] nose error

2013-05-21 Thread Jim Mooney
I'm trying a nose test on a simple prog. Once I got past the huge
traceback in Wing that made me think nose didn't work I realized the
prog tested okay. But I noticed at the end of the traceback nose says
this:

"c:\Python27\Lib\site-packages\nose-1.3.0-py2.7.egg\nose\core.py",
line 200, in runTests
sys.exit(not self.success)
SystemExit: False

As i said, nose works and says Okay on the asserts (or not if I give
things a bad value). I was just wondering what they SystemExit: False
meant, or is that standard?

Jim

"When I got to high school I realized my name would always present
problems." --Dick Hertz
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] still nosing around

2013-05-21 Thread Jim Mooney
Okay, why is nose saying this run is OK when I purposely screwed it up?

import nose

def stand_and_deliver():
return 52

def stupid_error():
'''This is my stupid error'''
assert stand_and_deliver() == 17

if __name__ == '__main__':
nose.runmodule()

nose result:

Ran 0 tests in 0.000s

OK


-- 
Jim Mooney

"When I got to high school I realized my name would always present
problems." --Dick Hertz
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] still nosing around

2013-05-21 Thread Mark Lawrence

On 22/05/2013 01:25, Jim Mooney wrote:

Okay, why is nose saying this run is OK when I purposely screwed it up?

import nose

def stand_and_deliver():
 return 52

def stupid_error():
 '''This is my stupid error'''
 assert stand_and_deliver() == 17

if __name__ == '__main__':
 nose.runmodule()

nose result:

Ran 0 tests in 0.000s

OK




Do you ever bother to investigate anything before posing a question?  I 
know nothing about nose, but when I see output telling me that precisely 
nothing has been run, I'd hazard a guess that stupid_error has to be 
called test_stupid_error or similar.  I'll leave you to check the nose 
docs to confirm whether or not my guess is correct.


--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


Re: [Tutor] still nosing around

2013-05-21 Thread Steven D'Aprano

On 22/05/13 10:25, Jim Mooney wrote:

Okay, why is nose saying this run is OK when I purposely screwed it up?

import nose

def stand_and_deliver():
 return 52

def stupid_error():
 '''This is my stupid error'''
 assert stand_and_deliver() == 17

if __name__ == '__main__':
 nose.runmodule()

nose result:

Ran 0 tests in 0.000s

OK



"Ran 0 tests" -- does that give you a clue? Nose is not running any tests. I 
have never used nose, so I don't know how you write tests for it, but whatever you are 
supposed to do, you haven't done it.

Let's see what Google has to say... this looks promising.

http://nose.readthedocs.org/en/latest/testing.html


As far as I can tell from about 30 seconds reading that page, your tests have 
to inherit from unittest.TestCase, or they have to be in a function, class or 
module called TestMatch.

(There must be something useful about nose, but frankly I don't see the benefit 
of running it instead of just using unittest directly.)


Personally, I recommend you start with doctests rather than nose or unittest. 
Change your module to this:

=== cut ===

def stand_and_deliver():
"""Always returns 52.

>>> stand_and_deliver()
52

"""
return 52


def stupid_error():
"""This is my stupid error.

>>> stupid_error()
17

"""
return stand_and_deliver() + 1000


if __name__ == '__main__':
import doctest
failed, run = doctest.testmod()
if failed == 0:
print("Successfully ran %d doc tests." % run)


=== cut ===



Run that file, and you should get 1 failed test out of 2, and a description of 
how it failed.





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


Re: [Tutor] nose error

2013-05-21 Thread eryksun
On Tue, May 21, 2013 at 8:09 PM, Jim Mooney  wrote:
>
> "c:\Python27\Lib\site-packages\nose-1.3.0-py2.7.egg\nose\core.py",
> line 200, in runTests
> sys.exit(not self.success)
> SystemExit: False
>
> As i said, nose works and says Okay on the asserts (or not if I give
> things a bad value). I was just wondering what they SystemExit: False
> meant, or is that standard?

Python's bool type is a subclass of int, so the function that handles
system exit converts False to 0. In terms of process exit codes, 0
means success. On Windows the exit code is set in "errorlevel":

>>> raise SystemExit(False)

C:\>echo %errorlevel%
0

>>> raise SystemExit(True)

C:\>if errorlevel 1 echo spam
spam
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] still nosing around

2013-05-21 Thread Steven D'Aprano

On 22/05/13 10:50, Mark Lawrence wrote:


Do you ever bother to investigate anything before posing a question?



That's rather harsh. The OP is doing a fine job at investigating new tools, and he's far 
more adventurous than I was at his level of expertise. (Or now, for that matter...) I 
think we can forgive him the occasional "whoops" moment.



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


Re: [Tutor] Tutor Digest, Vol 111, Issue 72

2013-05-21 Thread Jim Mooney
> Do you ever bother to investigate anything before posing a question?

Actually, I did notice that tests were zero, but the book I am using
does not mention needing the word 'test' as part of the regex. There
is only so much time in a day and so many books I can buy (and not all
comprehensive, apparently.)

That is why this list is an excellent ancillary to save me time.
Otherwise, learning Py will take me a lot more time than I can budget.
I'm not reading all the docs right now - which I used to and may in
the future - because I'm trying a different method of just running a
lot of progs to get up to speed. I figure once I "grok" the language,
docs will be a lot easier. There is a certain logic to every language
so you can anticipate what is right, but I'm not there yet.

But that brings up a point. Does this mean that if I have to test a
module with a lot of subroutines I have to rename every subroutine
with 'test' appended?

Try replying with a bit less emotion ;')

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


[Tutor] challenge-chapter 2

2013-05-21 Thread Andrew Triplett
I am on chapter two for Python Programming working on the challenges and the 
question is:

1. Create a list of legal and illegal variable names. Describe why each is 
either legal or illegal. Next, create a list of "good" and "bad" legal variable 
names. Describe why each is either a good or bad choice for a variable name.

Can you help me solve this problem?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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 an asymptotic learning curve, so I go off on a lot
of confused tangents, but I find it makes things go a lot faster after
a certain point.)

-- 
Jim Mooney

"When I got to high school I realized my name would always present
problems." --Dick Hertz
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 had the bald
names, which didn't tell me much. But I found a concise and basic
explanation of each error at
http://python.about.com/od/pythonstandardlibrary/a/lib_exceptions.htm

Although it's still a bit circular at my level. I won't be sure what
errors to raise until I see enough errors, but that will come with
experience, I guess. Now I just have to figure how to print the list
out without the ads ;')

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


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. Since I'm new at this the error lists I saw just had the bald
> names, which didn't tell me much. But I found a concise and basic
> explanation of each error at
> http://python.about.com/od/pythonstandardlibrary/a/lib_exceptions.htm
>
> Although it's still a bit circular at my level. I won't be sure what
> errors to raise until I see enough errors, but that will come with
> experience, I guess. Now I just have to figure how to print the list
> out without the ads ;')
>
> Jim
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

One thing I tend to do is fire up the interpreter and deliberately
write something that should fail, then I can see what error is raised.
For example, when I suggested the open() test, I fired up the
interpreter and tried

>>> open(dave, "r")
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'dave' is not defined

>>> open("dave2", "r")
Traceback (most recent call last):
  File "", line 1, in 
IOError: [Errno 2] No such file or directory: 'dave2'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor