Re: string find mystery

2009-09-03 Thread Hendrik van Rooyen
On Thursday 03 September 2009 07:10:37 Helvin wrote:
> Hi,
>
> I have come across this very strange behaviour. Check this code:
>
> if file_str.find('Geometry'):
> #if file_str.endswith('Data_Input_Geometry.txt'):
> print 'I found geometry'
> elif file_str.find('Material'):
> print 'I found material'
> The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
> \Data_Input_Material.txt',
> the first if statement if fulfilled, that seemingly says that in this
> file_str, python actually finds the word 'Geometry'.
> I know this, because the line: 'I found geometry' is printed. However,
> if instead of using file_str.find(), I use file_str.endswith(), it
> does not exhibit this strange behaviour.
>
> Obviously, I want the elif line to be true, instead of the first if
> statement.
>
> Does anyone know why this is happening?
> Help much appreciated!

The interactive Interpreter is your friend:

s = "a;kljghkahklahdfgkjahdfhadafjd;l"
s.find("banana")
-1
bool(_)
True

- Hendrik


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


Re: string find mystery

2009-09-03 Thread Tim Chase

I have come across this very strange behaviour. Check this code:

if file_str.find('Geometry'):


While the "anser" is to compare the results of .find() with -1, 
but the more Pythonic answer is just to use "in":


  if "Geometry" in file_str:

which reads a lot more cleanly, IMHO.

-tkc




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


Re: string find mystery

2009-09-02 Thread John Yeung
On Sep 3, 1:45 am, Sean DiZazzo  wrote:
> string.find() returns the index at which the given word is found
> within the string.  If the string is not found it returns -1.  So, no
> matter what you do, string.find() will evaluate to "True"

It will evaluate as false if the substring is found at the beginning
(position 0).

> You could use it like this:  if file_str.find("Geometry") != -1:
>
> but you probably want to use: if "Geometry" in file_str:

This is good advice, however.

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


Re: string find mystery

2009-09-02 Thread John Yeung
On Sep 3, 1:10 am, Helvin  wrote:
>         if file_str.find('Geometry'):
>         #if file_str.endswith('Data_Input_Geometry.txt'):
>             print 'I found geometry'
> The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
> \Data_Input_Material.txt',
> the first if statement if fulfilled, that seemingly says that in this
> file_str, python actually finds the word 'Geometry'.
>
> Does anyone know why this is happening?

Yep.  You should read the documentation on the find method.  It
returns -1 when the substring is not found.  Also, if 'Geometry' had
been found at the beginning, it would have returned 0.

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


Re: string find mystery

2009-09-02 Thread Sean DiZazzo
On Sep 2, 10:10 pm, Helvin  wrote:
> Hi,
>
> I have come across this very strange behaviour. Check this code:
>
>         if file_str.find('Geometry'):
>         #if file_str.endswith('Data_Input_Geometry.txt'):
>             print 'I found geometry'
>         elif file_str.find('Material'):
>             print 'I found material'
> The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
> \Data_Input_Material.txt',
> the first if statement if fulfilled, that seemingly says that in this
> file_str, python actually finds the word 'Geometry'.
> I know this, because the line: 'I found geometry' is printed. However,
> if instead of using file_str.find(), I use file_str.endswith(), it
> does not exhibit this strange behaviour.
>
> Obviously, I want the elif line to be true, instead of the first if
> statement.
>
> Does anyone know why this is happening?
> Help much appreciated!
>
> Very puzzled,
> Helvin

string.find() returns the index at which the given word is found
within the string.  If the string is not found it returns -1.  So, no
matter what you do, string.find() will evaluate to "True"

You could use it like this:  if file_str.find("Geometry") != -1:

but you probably want to use: if "Geometry" in file_str:

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


Re: string find mystery

2009-09-02 Thread Stephen Hansen
On Wed, Sep 2, 2009 at 10:33 PM, Helvin Lui  wrote:

> Thanks!  I just realised that too, but I used the condition:.find() >
> 0 But I think your's is better.
> Simple programming knowledge...  > <
>

Ah, but != 0 vs > 0 isn't a question of better, but correctness: because if
.find() returns 0, that's a successful result. That means it successfully
found the string-- at index 0, meaning the beginning of the string. This is
a function of Python indexing sequences starting with 0. Some people have
argued that this -1 behavior on .find is "unpythonic" from time to time, but
the other solutions just aren't very good for various reasons (e.g.,
returning None).

An alternate is .index() which raises ValueError on not-found which
side-steps the oddity if the -1.

On the blog:

 if (myString.find('bye') != -1):

It's generally bad-form to surround the expression passed to the "if"
statement in parens (unless necessary for implicit line wrapping purposes)
:) It's considered more Pythonic and clear to just do:

if myString.find('bye') != -1:

Although of course, that's a style and preference issue and so it's really
up to you.

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


Re: string find mystery

2009-09-02 Thread Helvin Lui
Thanks!  I just realised that too, but I used the condition:.find() > 0 But
I think your's is better.
Simple programming knowledge...  > <
I made a blog post:
http://learnwithhelvin.blogspot.com/2009/09/1-is-true-if-loops.html



On Thu, Sep 3, 2009 at 5:19 PM, Stephen Hansen wrote:

> The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
>> \Data_Input_Material.txt',
>> the first if statement if fulfilled, that seemingly says that in this
>> file_str, python actually finds the word 'Geometry'.
>> I know this, because the line: 'I found geometry' is printed. However,
>> if instead of using file_str.find(), I use file_str.endswith(), it
>> does not exhibit this strange behaviour.
>>
>>
> The problem is str.find returns -1 on failure; and -1 is a true value. Only
> 0, empty string, empty sequences, etc, are false values.
>
> So, you have to test, 'if find_str.find(pattern) != -1:'
>
> HTH,
>
> --S
>



-- 
Helvin

"Though the world may promise me more, I'm just made to be filled with the
Lord."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string find mystery

2009-09-02 Thread Stephen Hansen
>
> The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
> \Data_Input_Material.txt',
> the first if statement if fulfilled, that seemingly says that in this
> file_str, python actually finds the word 'Geometry'.
> I know this, because the line: 'I found geometry' is printed. However,
> if instead of using file_str.find(), I use file_str.endswith(), it
> does not exhibit this strange behaviour.
>
>
The problem is str.find returns -1 on failure; and -1 is a true value. Only
0, empty string, empty sequences, etc, are false values.

So, you have to test, 'if find_str.find(pattern) != -1:'

HTH,

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


string find mystery

2009-09-02 Thread Helvin
Hi,

I have come across this very strange behaviour. Check this code:

if file_str.find('Geometry'):
#if file_str.endswith('Data_Input_Geometry.txt'):
print 'I found geometry'
elif file_str.find('Material'):
print 'I found material'
The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
\Data_Input_Material.txt',
the first if statement if fulfilled, that seemingly says that in this
file_str, python actually finds the word 'Geometry'.
I know this, because the line: 'I found geometry' is printed. However,
if instead of using file_str.find(), I use file_str.endswith(), it
does not exhibit this strange behaviour.

Obviously, I want the elif line to be true, instead of the first if
statement.

Does anyone know why this is happening?
Help much appreciated!

Very puzzled,
Helvin
-- 
http://mail.python.org/mailman/listinfo/python-list