Re: Why does Python show the whole array?

2009-04-09 Thread Lawrence D'Oliveiro
In message , Peter Otten wrote:

> Lawrence D'Oliveiro wrote:
> 
>> In message , Gilles Ganault
>> wrote:
>> 
>>> test = "t...@gmail.com"
>>> isp = ["gmail.com", "yahoo.com"]
>>> for item in isp:
>>> if test.find(item):
>>> print item
>>> === output
>>> gmail.com
>>> yahoo.com
>>> ===
>> 
>> This is why conditional constructs should not accept any values other
>> than True and False.
> 
> So you think
> 
> if test.find(item) == True: ...
> 
> would have been better?

That won't work either. Can anyone tell us what he's done wrong?

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread John Machin
On Apr 10, 2:36 am, John Posner  wrote:
> Hrvoje Niksic wrote:
>
>  >  if test.contains(item)     # would return a Boolean value
>  >
>
>  >> That's a string method, not a function in the string module.
>
> Oops, of course.
>
>   import operator
>   operator.contains('foo', 'o')
>
> That's pretty good, and IMHO a bit better than John Machin's suggestion
> to use the __contains__() method. (I have this prejudice that using the
> __XXX__ methods in "everyday code" is cheating.)

I "suggested" no such thing. You asked whether anyone had discussed
such a thing. A reply pointing out that such a thing exists already is
in no way inciting people to use it.

>
> Given how common string maniuplations are, I guess I'm surprised that
> Python hasn't yet made "contains()" into both a "string"-module function
> *and* a string-object method.

Perhaps because there's already a good way to do it: stringa in stringb
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Re: Why does Python show the whole array?

2009-04-09 Thread John Posner

> Peter Otten wrote:

>> Could you explain why you prefer 'contains(belly, beer)'
>> or 'belly.contains(beer)' over 'beer in belly'? The last form may be 
a bit

>> harder to find in the documentation, but once a newbie has learned about
>> it he'll find it easy to remember.

andrew cooke wrote:

> i don't know why i get involved in this type of discussion, but


I sense growing exasperation, and I do *not* have strong feelings on 
this matter, so I promise this will be my last message on this topic.


I don't really prefer 'contains(belly, beer)' over 'beer in belly'. 
Rather, it's a completeness/consistency argument: a language that 
includes these methods for string objects:


  belly.startswith(arg)
  belly.endswith(arg)

... should also include:

  belly.contains(arg)

The lack of this method increases the likelihood that a user will 
mistakenly use the find() method as a predicate.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Why does Python show the whole array?

2009-04-09 Thread andrew cooke
Peter Otten wrote:
> John Posner wrote:
>
>> Given how common string maniuplations are, I guess I'm surprised that
>> Python hasn't yet made "contains()" into both a "string"-module function
>> *and* a string-object method.
>
> Could you explain why you prefer 'contains(belly, beer)'
> or 'belly.contains(beer)' over 'beer in belly'? The last form may be a bit
> harder to find in the documentation, but once a newbie has learned about
> it
> he'll find it easy to remember.

i don't know why i get involved in this type of discussion, but

while i agree that "in" is the correct answer, i am not sure it's obvious
or easy to remember once you know it.  perhaps to a newbie with a sweet,
innocent and unsullied mind.  but for an already corrupted programmer like
myself, it's not easy to remember - i keep forgetting it!  i think it's
because i associate "in" with iteration, and assume everything else will
be method calls or functions (and from earlier discussions here, it's
clear some people are even more blinkered, and think everything should be
methods)

so yeah, "in" is right, but arguments about what is natural and easy
aren't really worth wasting bits over...

andrew


--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Why does Python show the whole array?

2009-04-09 Thread Peter Otten
John Posner wrote:

> Given how common string maniuplations are, I guess I'm surprised that
> Python hasn't yet made "contains()" into both a "string"-module function
> *and* a string-object method.

Could you explain why you prefer 'contains(belly, beer)'
or 'belly.contains(beer)' over 'beer in belly'? The last form may be a bit
harder to find in the documentation, but once a newbie has learned about it
he'll find it easy to remember.

Peter
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Why does Python show the whole array?

2009-04-09 Thread John Posner

Hrvoje Niksic wrote:

>  if test.contains(item) # would return a Boolean value
>

>> That's a string method, not a function in the string module.

Oops, of course.

 import operator
 operator.contains('foo', 'o')

That's pretty good, and IMHO a bit better than John Machin's suggestion 
to use the __contains__() method. (I have this prejudice that using the 
__XXX__ methods in "everyday code" is cheating.)


Given how common string maniuplations are, I guess I'm surprised that 
Python hasn't yet made "contains()" into both a "string"-module function 
*and* a string-object method.



--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Hrvoje Niksic
John Posner  writes:

> Q: Has anyone on the python-dev list ever proposed a "string"-module
> function that does the job of the "in" operator? Maybe this:
>
>  if test.contains(item) # would return a Boolean value

That's a string method, not a function in the string module.  If you
want a function, use operator.contains:

>>> import operator
>>> operator.contains('foo', 'o')
True
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Tim Rowe
2009/4/9 Miles :

> Clearly, any comparison with a boolean literal should be illegal.  ;)

Hey, we could have strict type checking at compile time of /all/
operations, couldn't we? Anybody care to join me over at the Ada list?
;-)

-- 
Tim Rowe
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread John Machin
On Apr 10, 12:35 am, John Posner  wrote:
> Lawrence D'Oliveiro wrote:
>
>  > Fine if it only happened once. But it's a commonly-made mistake. At some
>  > point you have to conclude that not all those people are stupid, there
>  > really is something wrong with the design.
>
> I think "something wrong with the design" is overstating the case a bit,
> and is likely to elicit some negative reactions in this forum. But I
> agree with your point, to this extent: this situation illustrates the
> way in which Python "overloads" the number zero:
>
>   Realm A:  "0" indicates the first position in a sequence
>   Realm B:   "0" indicates the Boolean value "False"
>
> You just need to remember that the find() function works in Realm A, and
> the "in" operator works in Realm B.
>
> Q: Has anyone on the python-dev list ever proposed a "string"-module
> function that does the job of the "in" operator? Maybe this:
>
>   if test.contains(item)     # would return a Boolean value

Yes.

command_prompt> \python23\python -c "print 'frobozz'.__contains_
_('obo')"
True

Was first introduced in Python 2.2, with only a single character
allowed in the arg.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Why does Python show the whole array?

2009-04-09 Thread John Posner

Lawrence D'Oliveiro wrote:

> Fine if it only happened once. But it's a commonly-made mistake. At some
> point you have to conclude that not all those people are stupid, there
> really is something wrong with the design.

I think "something wrong with the design" is overstating the case a bit, 
and is likely to elicit some negative reactions in this forum. But I 
agree with your point, to this extent: this situation illustrates the 
way in which Python "overloads" the number zero:


 Realm A:  "0" indicates the first position in a sequence
 Realm B:   "0" indicates the Boolean value "False"

You just need to remember that the find() function works in Realm A, and 
the "in" operator works in Realm B.


Q: Has anyone on the python-dev list ever proposed a "string"-module 
function that does the job of the "in" operator? Maybe this:


 if test.contains(item) # would return a Boolean value

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Terry Reedy

Lawrence D'Oliveiro wrote:
In message , Gilles Ganault 
wrote:



test = "t...@gmail.com"
isp = ["gmail.com", "yahoo.com"]
for item in isp:
if test.find(item):
print item
=== output
gmail.com
yahoo.com
===


This is why conditional constructs should not accept any values other than 
True and False.


Nonesense.  Just use 'if item in test:' which tells you all the info you 
are using.   str.find returns an index that you ignore, so don't use it.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Lawrence D'Oliveiro
In message <7e7a386f-d336-4186-822d-
c6af0a581...@e38g2000vbe.googlegroups.com>, John Machin wrote:

> On Apr 9, 4:53 pm, Lawrence D'Oliveiro  central.gen.new_zealand> wrote:
>
>> This is why conditional constructs should not accept any values other
>> than True and False.
> 
> An alternative viewpoint: This is what happens when one user doesn't
> understand a long-standing idiom and/or doesn't read the manual.
> That's all. It doesn't constitute evidence that such a restriction
> would be generally beneficial.

Fine if it only happened once. But it's a commonly-made mistake. At some 
point you have to conclude that not all those people are stupid, there 
really is something wrong with the design.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Steven D'Aprano
On Thu, 09 Apr 2009 18:53:13 +1200, Lawrence D'Oliveiro wrote:

> This is why conditional constructs should not accept any values other
> than True and False.

I once tried this:

for i in alist.sort():

and got an error I didn't understand because I failed to read the docs. 
Clearly for loops shouldn't take sequences but only an integer count, 
because that would have prevented my error.



-- 
Steven
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Niklas Norrthon
On 9 Apr, 09:49, Miles  wrote:
> On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote:
> > Lawrence D'Oliveiro wrote:
> >> This is why conditional constructs should not accept any values other than
> >> True and False.
>
> > So you think
>
> > if test.find(item) == True: ...
>
> > would have been better?
>
> Clearly, any comparison with a boolean literal should be illegal.  ;)
>

So you think

truth_value = True
if test.find(item) == truth_value: ...

would have been better? :-)
(couldn't resist...)

/Niklas Norrthon

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread John Machin
On Apr 9, 4:53 pm, Lawrence D'Oliveiro  wrote:
> In message , Gilles Ganault
> wrote:
>
> > test = "t...@gmail.com"
> > isp = ["gmail.com", "yahoo.com"]
> > for item in isp:
> > if test.find(item):
> > print item
> > === output
> > gmail.com
> > yahoo.com
> > ===
>
> This is why conditional constructs should not accept any values other than
> True and False.

An alternative viewpoint: This is what happens when one user doesn't
understand a long-standing idiom and/or doesn't read the manual.
That's all. It doesn't constitute evidence that such a restriction
would be generally beneficial.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Miles
On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote:
> Lawrence D'Oliveiro wrote:
>> This is why conditional constructs should not accept any values other than
>> True and False.
>
> So you think
>
> if test.find(item) == True: ...
>
> would have been better?

Clearly, any comparison with a boolean literal should be illegal.  ;)

-Miles

P.S. ... really, though.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-09 Thread Peter Otten
Lawrence D'Oliveiro wrote:

> In message , Gilles Ganault
> wrote:
> 
>> test = "t...@gmail.com"
>> isp = ["gmail.com", "yahoo.com"]
>> for item in isp:
>> if test.find(item):
>> print item
>> === output
>> gmail.com
>> yahoo.com
>> ===
> 
> This is why conditional constructs should not accept any values other than
> True and False.

So you think

if test.find(item) == True: ...

would have been better? 

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Lawrence D'Oliveiro
In message , Gilles Ganault 
wrote:

> test = "t...@gmail.com"
> isp = ["gmail.com", "yahoo.com"]
> for item in isp:
> if test.find(item):
> print item
> === output
> gmail.com
> yahoo.com
> ===

This is why conditional constructs should not accept any values other than 
True and False.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread andrew cooke
Gilles Ganault wrote:
> On Wed, 08 Apr 2009 12:11:55 +0200, Ulrich Eckhardt
>  wrote:
>>find() returns the index where it is found or -1 if it is not found. Both
>> an
>>index>0 or a -1 evaluate to True when used as conditional expression.
>
> Thanks everyone. I shouldn't have assumed that "if test.find(item):"
> was necessarily enough to mean True.
>
> for item in isp:
>   #GOOD if item in test:
>   if test.find(item) > 0:

the post you quote has an error.  you probably want > -1 there.  0 is
returned if the two strings match at the start.  the problem is that the
method return isn't consistent with implicit conversion to boolean; python
does convert non-zero to True.

andrew

>   print test
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Gilles Ganault
On Wed, 08 Apr 2009 12:11:55 +0200, Ulrich Eckhardt
 wrote:
>find() returns the index where it is found or -1 if it is not found. Both an
>index>0 or a -1 evaluate to True when used as conditional expression.

Thanks everyone. I shouldn't have assumed that "if test.find(item):"
was necessarily enough to mean True.

for item in isp:
#GOOD if item in test:
if test.find(item) > 0:
print test
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Qilong Ren




Remeber the return value of find function of a string is -1 when it  
fails, which is True.


Try:
  for item in isp:
 if item in test:
   print item




From: Gilles Ganault 
Date: April 8, 2009 5:56:34 PM CST
To: python-list@python.org
Subject: Why does Python show the whole array?


Hello

I'd like to go through a list of e-mail addresses, and extract those
that belong to well-known ISP's. For some reason I can't figure out,
Python shows the whole list instead of just e-mails that match:

=== script
test = "t...@gmail.com"
isp = ["gmail.com", "yahoo.com"]
for item in isp:
if test.find(item):
print item
=== output
gmail.com
yahoo.com
===

Any idea why I'm also getting "yahoo.com"?

Thank you.



--
http://mail.python.org/mailman/listinfo/python-list


--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Dave Angel



Gilles Ganault wrote:

Hello

I'd like to go through a list of e-mail addresses, and extract those
that belong to well-known ISP's. For some reason I can't figure out,
Python shows the whole list instead of just e-mails that match:

=== script
test = "t...@gmail.com"
isp = ["gmail.com", "yahoo.com"]
for item in isp:
if test.find(item):
print item
=== output
gmail.com
yahoo.com
=== 


Any idea why I'm also getting "yahoo.com"?

Thank you.

  
Look up the definition of string.find().  It returns a -1 for failure, 
not 0.  So your test should presumably be

  if test.find(item) != -1:


--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Ben Finney
Gilles Ganault  writes:

> I'd like to go through a list of e-mail addresses, and extract those
> that belong to well-known ISP's. For some reason I can't figure out,
> Python shows the whole list instead of just e-mails that match:
> 
> === script
> test = "t...@gmail.com"
> isp = ["gmail.com", "yahoo.com"]
> for item in isp:
>   if test.find(item):
>   print item
> === output
> gmail.com
> yahoo.com
> === 
> 
> Any idea why I'm also getting "yahoo.com"?

You've had answers on the “why” question.

Here's a suggestion for a better way that doesn't involve ‘find’:

>>> known_domains = ["example.com", "example.org"]
>>> test_address = "t...@example.com"
>>> for domain in known_domains:
... if test_address.endswith("@" + domain):
... print domain
... 
example.com
>>> 

If all you want is a boolean “do any of these domains match the
address”, it's quicker and simpler to feed an iterator to ‘any’
(first introduced in Python 2.5), which short-cuts by exiting on the
first item that produces a True result:

>>> known_domains = ["example.com", "example.org"]
>>> test_address = "t...@example.com"
>>> any(test_address.endswith("@" + domain) for domain in known_domains)
True
>>> test_address = "tin...@example.net"
>>> any(test_address.endswith("@" + domain) for domain in known_domains)
False

-- 
 \  “For my birthday I got a humidifier and a de-humidifier. I put |
  `\  them in the same room and let them fight it out.” —Steven Wright |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Albert Hopkins
On Wed, 2009-04-08 at 12:01 +0200, Peter Otten wrote:
> Gilles Ganault wrote:
> 
> > I'd like to go through a list of e-mail addresses, and extract those
> > that belong to well-known ISP's. For some reason I can't figure out,
> > Python shows the whole list instead of just e-mails that match:
> > 
> > === script
> > test = "t...@gmail.com"
> > isp = ["gmail.com", "yahoo.com"]
> > for item in isp:
> > if test.find(item):
> > print item
> > === output
> > gmail.com
> > yahoo.com
> > === 
> > 
> > Any idea why I'm also getting "yahoo.com"?
> 
> Because str.find() returns the position of the search string if found and -1
> if it is not found:
> 
> >>> "abc".find("bc")
> 1
> >>> "abc".find("ab")
> 0
> >>> "abc".find("x")
> -1
> 
> Use
> 
> if test.find(item) != -1: ...
> 
> or
> 
> if item in test: ...
> 
> to make your example work.

Or you could also use the .endswith() method

if test[test.find('@')+1:].endswith(item):


--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Ulrich Eckhardt
Gilles Ganault wrote:
> test = "t...@gmail.com"
> isp = ["gmail.com", "yahoo.com"]
> for item in isp:
> if test.find(item):
> print item
> === output
> gmail.com
> yahoo.com
> ===
> 
> Any idea why I'm also getting "yahoo.com"?

find() returns the index where it is found or -1 if it is not found. Both an
index>0 or a -1 evaluate to True when used as conditional expression.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does Python show the whole array?

2009-04-08 Thread Peter Otten
Gilles Ganault wrote:

> I'd like to go through a list of e-mail addresses, and extract those
> that belong to well-known ISP's. For some reason I can't figure out,
> Python shows the whole list instead of just e-mails that match:
> 
> === script
> test = "t...@gmail.com"
> isp = ["gmail.com", "yahoo.com"]
> for item in isp:
> if test.find(item):
> print item
> === output
> gmail.com
> yahoo.com
> === 
> 
> Any idea why I'm also getting "yahoo.com"?

Because str.find() returns the position of the search string if found and -1
if it is not found:

>>> "abc".find("bc")
1
>>> "abc".find("ab")
0
>>> "abc".find("x")
-1

Use

if test.find(item) != -1: ...

or

if item in test: ...

to make your example work.

Peter
--
http://mail.python.org/mailman/listinfo/python-list