Re: [Tutor] Testing for empty list

2009-10-19 Thread Todd Matsumoto
The while loop will print each index of the list. In a way it checks that if the list is empty by printing the items. As far as I know there isn't any 'True' or 'False' output from a list. If you want to do something if mylist is empty you can check it like this: if not mylist: ... do someth

Re: [Tutor] Testing for empty list

2009-10-19 Thread Andre Engels
On Mon, Oct 19, 2009 at 3:29 AM, Wayne wrote: > Hi, I think I recall seeing this here, but I wanted to make sure I'm > correct. > Is the best way to test for an empty list just test for the truth value? > I.e. > mylist = [1,2,3] > while mylist: >    print mylist.pop() Whether it is the 'best' way

Re: [Tutor] Testing for empty list

2009-10-19 Thread Alan Gauld
"Todd Matsumoto" wrote The while loop will print each index of the list. No, the while does nothing with list indexes, that is entirely down to the programmer. The while loop simply repeats for as long as its test expression evaluates to True. As far as I know there isn't any 'True' or 'Fal

Re: [Tutor] Testing for empty list

2009-10-19 Thread Luke Paireepinart
On Mon, Oct 19, 2009 at 2:26 AM, Todd Matsumoto wrote: > The while loop will print each index of the list. No it's printing each element of the list, not the index. > In a way it checks that if the list is empty by printing the items. As far > as I know there isn't any 'True' or 'False' output

Re: [Tutor] Testing for empty list

2009-10-19 Thread Dave Angel
Wayne wrote: Hi, I think I recall seeing this here, but I wanted to make sure I'm correct. Is the best way to test for an empty list just test for the truth value? I.e. mylist = [1,2,3] while mylist: print mylist.pop() Thanks, Wayne My take is simple: Use the above form if you *know*

Re: [Tutor] Testing for empty list

2009-10-19 Thread Todd Matsumoto
I don't understand how the while loop efficiently tests if the list is empty. Why would going through the entire list be a good test to simply see find out if the list is empty or not. Wouldn't you want to test the list itself, rather than the contents of it? Cheers, T Original-Nachr

Re: [Tutor] Testing for empty list

2009-10-19 Thread Stefan Behnel
Hi, please don't top-post. Todd Matsumoto wrote: > I don't understand how the while loop efficiently tests if the list is > empty. It doesn't. It only tests a condition. And the result of the condition is determined by the list itself, which knows if it's empty or not. Stefan

Re: [Tutor] introspecting an object for method's name?

2009-10-19 Thread Kent Johnson
On Sunday, October 18, 2009, Serdar Tumgoren wrote: > Hi everyone, > I'm trying to create a generic logging function, and I'm able to get > at the name of the module and class using the built-in attributes > __module__, __class__, and __name__. > > But I wasn't sure how to also grab the name of th

Re: [Tutor] introspecting an object for method's name?

2009-10-19 Thread Tim Golden
Kent Johnson wrote: On Sunday, October 18, 2009, Serdar Tumgoren wrote: Hi everyone, I'm trying to create a generic logging function, and I'm able to get at the name of the module and class using the built-in attributes __module__, __class__, and __name__. But I wasn't sure how to also grab th

Re: [Tutor] introspecting an object for method's name?

2009-10-19 Thread Serdar Tumgoren
> Or just use the built-in logging module which lets you specify > the containing function name as one of its formatting keywords: > > http://docs.python.org/library/logging.html#formatter-objects > Aha! I had skimmed the logging docs, but hadn't gone far enough down to notice the %(funcName)s for

Re: [Tutor] introspecting an object for method's name?

2009-10-19 Thread Kent Johnson
On Mon, Oct 19, 2009 at 8:40 AM, Tim Golden wrote: > Or just use the built-in logging module which lets you specify > the containing function name as one of its formatting keywords: > > http://docs.python.org/library/logging.html#formatter-objects Sweet! And much better than rolling your own log

Re: [Tutor] Testing for empty list

2009-10-19 Thread Katt
Wayne wrote: Hi, I think I recall seeing this here, but I wanted to make sure I'm correct. Is the best way to test for an empty list just test for the truth value? I.e. mylist = [1,2,3] while mylist: print mylist.pop() Thanks, Wayne My take is simple: Use the above form if you *know* th

[Tutor] Python List Help

2009-10-19 Thread Mike Sweany
Hi all, I am a PHP developer that just started learning Python for a specific application and the transition has been pretty easily assisted by google, but I just don’t see the issue with this one? I’ve got a list that created and populate in a loop that shows the correct info when I print

Re: [Tutor] Testing for empty list

2009-10-19 Thread Rich Lovely
2009/10/19 Katt > > Hello all, > > Just a newbie question, but when would you test for an empty list?  Is it > part of a code error detection or an error check to make sure that there is > user input? > > Couldn't you just use something like: > > while len(mylist) > 0: >   continue program > els

[Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Eduardo Vieira
Hello, The other day I was making a script and decided to use a list compreehension and I found out that this code: mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] returned this: ['JOHN', 'CANADA', 'RIGHT'] I was expecting this: ['JOH

[Tutor] updating Unix config file

2009-10-19 Thread Matt Herzog
Hi All. The below script seems to work well enough to use but I'm wondering if I'm doing the file edit stuff in a "kosher" manner. I was just reading the Lutz O'Reilly "Learning" book and remembered that strings are immutable. So how was I able to change the strings for my dotted quad? I did no

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Sander Sweers
2009/10/19 Eduardo Vieira : > mylist = ['John', 'Canada', 25, 32, 'right'] > a = [item.upper() for item in mylist if type(item) == type('good')] Usually it is recommended to use hasattr() instead of type() hasattr(s, 'upper') > returned this: ['JOHN', 'CANADA', 'RIGHT'] > I was expecting this

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Wayne
On Mon, Oct 19, 2009 at 11:39 AM, Eduardo Vieira wrote: > Hello, > The other day I was making a script and decided to use a list > compreehension and I found out that this code: > mylist = ['John', 'Canada', 25, 32, 'right'] > a = [item.upper() for item in mylist if type(item) == type('good')] > r

Re: [Tutor] Python List Help

2009-10-19 Thread Dave Angel
Mike Sweany wrote: Hi all, I am a PHP developer that just started learning Python for a specific application and the transition has been pretty easily assisted by google, but I just don’t see the issue with this one? I’ve got a list that created and populate in a loop that shows the cor

Re: [Tutor] Testing for empty list

2009-10-19 Thread Alan Gauld
"Katt" wrote Just a newbie question, but when would you test for an empty list? When you are processing a list such that you are deleting items as you go. When the list is empty stop processing! And Python helps you do that by treating an empty list as a False boolean value so you can do

Re: [Tutor] updating Unix config file

2009-10-19 Thread Alan Gauld
"Matt Herzog" wrote remembered that strings are immutable. So how was I able to change the strings for my dotted quad? You didn't. LASTKNOWN = '173.48.204.168' lns = cf.readlines() lns = "".join(lns) lns = re.sub(LASTKNOWN, CURRENT, lns) I assume this is the line in question? s

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
"Sander Sweers" wrote mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] Usually it is recommended to use hasattr() instead of type() hasattr(s, 'upper') Nope, they do completely different things I think you might be thinkin

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Sander Sweers
2009/10/19 Alan Gauld : >> Usually it is recommended to use hasattr() instead of type() >>   hasattr(s, 'upper') > > Nope, they do  completely different things > I think you might be thinking of isinstance() which can be used instead of > type(). I see you use hasattr as a means of testing for a me

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Emile van Sebille
On 10/19/2009 12:20 PM Alan Gauld said... "Sander Sweers" wrote mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] Usually it is recommended to use hasattr() instead of type() hasattr(s, 'upper') Nope, they do completely di

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread vince spicer
On Mon, Oct 19, 2009 at 1:58 PM, Emile van Sebille wrote: > On 10/19/2009 12:20 PM Alan Gauld said... > > >> "Sander Sweers" wrote >> >> mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] >>> >>> Usually it is recommended t

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread vince spicer
On Mon, Oct 19, 2009 at 2:14 PM, vince spicer wrote: > > > On Mon, Oct 19, 2009 at 1:58 PM, Emile van Sebille wrote: > >> On 10/19/2009 12:20 PM Alan Gauld said... >> >> >>> "Sander Sweers" wrote >>> >>> mylist = ['John', 'Canada', 25, 32, 'right'] > a = [item.upper() for item in mylist if

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Emmanuel Ruellan
On Mon, Oct 19, 2009 at 9:20 PM, Alan Gauld wrote: > > > "Sander Sweers" wrote > > mylist = ['John', 'Canada', 25, 32, 'right'] >>> a = [item.upper() for item in mylist if type(item) == type('good')] >>> >> >> Usually it is recommended to use hasattr() instead of type() >> hasattr(s, 'upper')

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Douglas Philips
On or about 2009 Oct 19, at 3:57 PM, Sander Sweers indited: I missed that the try: did not return anything. I was thinking more of something like this. def upperfy(item): try: item.upper() return item except AttributeError: return item Thanks for correcting me! Depe

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Eduardo Vieira
On Mon, Oct 19, 2009 at 1:20 PM, Alan Gauld wrote: > > "Sander Sweers" wrote > >>> mylist = ['John', 'Canada', 25, 32, 'right'] >>> a = [item.upper() for item in mylist if type(item) == type('good')] >> >> Usually it is recommended to use hasattr() instead of type() >>   hasattr(s, 'upper') > > N

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread ALAN GAULD
Is there any compelling reason to write: > >[item.upper() for item in my_list if isinstance(item, basestring)] > >rather than the following? > >[item.upper() for item in my_list if hasattr(item, 'upper')]What happens if >you have an object in your list that has an 'upper' merthod that, say, reboo

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
"Emile van Sebille" wrote a = [item.upper() if type(item) == str else item for item in mylist] should do it I think. or even a = [ str(item).upper() for item in mylist ] That was my first attempt but the OP wanted his integers preserved as integers whereas this would convert them to

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
"vince spicer" wrote Lambda can save the day to keep everything on one line, and leave variable type the same: mylist = ['John', 'Canada', 25, 32, 'right'] new_list = [(lambda y: y.upper() if hasattr(y, 'upper') else y)(a) for a in mylist ] >> ['JACK', 'CANADA', 25, 32, 'RIGHT'] Vince

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
Ooops, hit send by mistake... "vince spicer" wrote Lambda can save the day to keep everything on one line, and leave variable type the same: mylist = ['John', 'Canada', 25, 32, 'right'] new_list = [(lambda y: y.upper() if hasattr(y, 'upper') else y)(a) for a in mylist ] >> ['JACK', 'CANA

Re: [Tutor] updating Unix config file

2009-10-19 Thread Matt Herzog
On Mon, Oct 19, 2009 at 08:07:47PM +0100, Alan Gauld wrote: > > "Matt Herzog" wrote > > >remembered that strings are immutable. > >So how was I able to change the strings for my dotted quad? > > You didn't. > > >LASTKNOWN = '173.48.204.168' > > lns = cf.readlines() > > lns = "".join(lns)

Re: [Tutor] updating Unix config file

2009-10-19 Thread Matt Herzog
On Mon, Oct 19, 2009 at 07:51:06PM -0400, Matt Herzog wrote: > On Mon, Oct 19, 2009 at 08:07:47PM +0100, Alan Gauld wrote: > > > > "Matt Herzog" wrote > > > > >remembered that strings are immutable. > > >So how was I able to change the strings for my dotted quad? > > > > You didn't. > > > >

[Tutor] Running Python on a Calculator

2009-10-19 Thread Corey Richardson
Hey tutors. I have a TI-84 plus, and I am making some math tools, and I don't know the native language of the Ti-84, and was wondering, has anyone worked with a version of Python that can run on that small of a processor? Thanks in advance, ~Corey ___

Re: [Tutor] Python List Help

2009-10-19 Thread Mike Sweany
Thanks for the reply and Tips Dave... I think the formatting came out weird because I didn’t specify plain text when I created the email. I'm using Google App Engine and it's internal debugger shows what I posted earlier when the code breaks, I agree that what I am seeing from the debugger makes