Re: Checking if elements are empty

2007-09-11 Thread Steve Holden
Neil Cerutti wrote:
> On 2007-09-10, Steve Holden <[EMAIL PROTECTED]> wrote:
>>> I have a quibble not with the functionality of the boolean check,
>>> but with its expressiveness. if y[0] == "" expresses more, i.e.,
>>> that I expect y[0] to contain a Python byte string.
>> I have a quibble with a test that will raise an exception when
>> the anticipated condition is true. Your test is patently
>> absurd, as you would have discovered had you bothered to try
>> it:
> 
> "if y[0] == "" expresses more, i.e., that I expect y[0] to
> contain a Python byte string."
> 
That isn't unreasonable immediately after

y = "...".split(...)

though, is it? I was talking tosh above.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

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


Re: Checking if elements are empty

2007-09-11 Thread Steve Holden
Hamilton, William wrote:
>> From: Steve Holden
>> Neil Cerutti wrote:
>>> On 2007-09-10, Chris Mellon <[EMAIL PROTECTED]> wrote:
 On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> Agreed; but I prefer 'if y[0] == ""', absent more context and
> better names.
 Probably should use u"" if you're going to take that route, as
 this will fail spuriously if y[0] contains a unicode string
 that can't be implicitly converted to ascii. Personally, I
 prefer the boolean check and I'll let operations fail elsewhere
 if there's a type mismatch.
>>> I have a quibble not with the functionality of the boolean check,
>>> but with its expressiveness. if y[0] == "" expresses more, i.e.,
>>> that I expect y[0] to contain a Python byte string.
>>>
>> I have a quibble with a test that will raise an exception when the
>> anticipated condition is true. Your test is patently absurd, as you
>> would have discovered had you bothered to try it:
>>
>>  >>> y = ""
>>  >>> if y[0] == "":
>> ...   print "True"
>> ... else:
>> ...   print "False"
>> ...
>> Traceback (most recent call last):
>>File "", line 1, in 
>> IndexError: string index out of range
>>  >>>
>>
>> Further, when the string is *not* the null string the test will always
>> return False, as you will be comparing two strings of unequal length.
>>
>> So, absent a solution that works, len(y) == 0 looks pretty good.
> 
> 
> Going back to the OP, the problem is taking a string such as
 x = '  \t"ff'
> then splitting that string like this
 y = x.split('\t')
> 
> The question is, does the first element of the list y contain an empty
> string or not?  In this case, the logic in the following conditional is
> perfectly valid.
 if y[0] == "":
> ...print "True"
> ... else
> ...print "False"
> 
> (len(y[0]) == 0) would also work, and is the solution you originally
> gave the OP.  
> 
Aah, list of strings, right. Thanks.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

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


RE: Checking if elements are empty

2007-09-11 Thread Hamilton, William
> From: Steve Holden
> Neil Cerutti wrote:
> > On 2007-09-10, Chris Mellon <[EMAIL PROTECTED]> wrote:
> >> On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> >>> Agreed; but I prefer 'if y[0] == ""', absent more context and
> >>> better names.
> >> Probably should use u"" if you're going to take that route, as
> >> this will fail spuriously if y[0] contains a unicode string
> >> that can't be implicitly converted to ascii. Personally, I
> >> prefer the boolean check and I'll let operations fail elsewhere
> >> if there's a type mismatch.
> >
> > I have a quibble not with the functionality of the boolean check,
> > but with its expressiveness. if y[0] == "" expresses more, i.e.,
> > that I expect y[0] to contain a Python byte string.
> >
> I have a quibble with a test that will raise an exception when the
> anticipated condition is true. Your test is patently absurd, as you
> would have discovered had you bothered to try it:
> 
>  >>> y = ""
>  >>> if y[0] == "":
> ...   print "True"
> ... else:
> ...   print "False"
> ...
> Traceback (most recent call last):
>File "", line 1, in 
> IndexError: string index out of range
>  >>>
> 
> Further, when the string is *not* the null string the test will always
> return False, as you will be comparing two strings of unequal length.
> 
> So, absent a solution that works, len(y) == 0 looks pretty good.


Going back to the OP, the problem is taking a string such as
>>> x = '  \t"ff'
then splitting that string like this
>>> y = x.split('\t')

The question is, does the first element of the list y contain an empty
string or not?  In this case, the logic in the following conditional is
perfectly valid.
>>> if y[0] == "":
...print "True"
... else
...print "False"

(len(y[0]) == 0) would also work, and is the solution you originally
gave the OP.  


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


Re: Checking if elements are empty

2007-09-11 Thread Neil Cerutti
On 2007-09-10, Steve Holden <[EMAIL PROTECTED]> wrote:
>> I have a quibble not with the functionality of the boolean check,
>> but with its expressiveness. if y[0] == "" expresses more, i.e.,
>> that I expect y[0] to contain a Python byte string.
>
> I have a quibble with a test that will raise an exception when
> the anticipated condition is true. Your test is patently
> absurd, as you would have discovered had you bothered to try
> it:

"if y[0] == "" expresses more, i.e., that I expect y[0] to
contain a Python byte string."

-- 
Neil Cerutti
Bach's death is attributed to the end of the Baroque era. --Music Lit Essay
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-11 Thread Wildemar Wildenburger
Steve Holden wrote:
> Neil Cerutti wrote:
>  >>> y = ""
>  >>> if y[0] == "":
> ...   print "True"
> ... else:
> ...   print "False"
> ...
> Traceback (most recent call last):
>   File "", line 1, in 
> IndexError: string index out of range
>  >>>
> 
Uhm, weren't we talking about a list of strings?

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


Re: Checking if elements are empty

2007-09-11 Thread Piet van Oostrum
> "Chris Mellon" <[EMAIL PROTECTED]> (CM) wrote:

>CM> On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:

>>> Agreed; but I prefer 'if y[0] == ""', absent more context and
>>> better names.

>CM> Probably should use u"" if you're going to take that route, as this
>CM> will fail spuriously if y[0] contains a unicode string that can't be
>CM> implicitly converted to ascii. Personally, I prefer the boolean check
>CM> and I'll let operations fail elsewhere if there's a type mismatch.

>>> x=u"€"
>>> x == ""
False
>>> 

-- 
Piet van Oostrum <[EMAIL PROTECTED]>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-10 Thread Steve Holden
Neil Cerutti wrote:
> On 2007-09-10, Chris Mellon <[EMAIL PROTECTED]> wrote:
>> On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
>>> Agreed; but I prefer 'if y[0] == ""', absent more context and
>>> better names.
>> Probably should use u"" if you're going to take that route, as
>> this will fail spuriously if y[0] contains a unicode string
>> that can't be implicitly converted to ascii. Personally, I
>> prefer the boolean check and I'll let operations fail elsewhere
>> if there's a type mismatch.
> 
> I have a quibble not with the functionality of the boolean check,
> but with its expressiveness. if y[0] == "" expresses more, i.e.,
> that I expect y[0] to contain a Python byte string.
> 
I have a quibble with a test that will raise an exception when the 
anticipated condition is true. Your test is patently absurd, as you 
would have discovered had you bothered to try it:

 >>> y = ""
 >>> if y[0] == "":
...   print "True"
... else:
...   print "False"
...
Traceback (most recent call last):
   File "", line 1, in 
IndexError: string index out of range
 >>>

Further, when the string is *not* the null string the test will always 
return False, as you will be comparing two strings of unequal length.

So, absent a solution that works, len(y) == 0 looks pretty good.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

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


Re: Checking if elements are empty

2007-09-10 Thread Neil Cerutti
On 2007-09-10, Chris Mellon <[EMAIL PROTECTED]> wrote:
> On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
>> Agreed; but I prefer 'if y[0] == ""', absent more context and
>> better names.
>
> Probably should use u"" if you're going to take that route, as
> this will fail spuriously if y[0] contains a unicode string
> that can't be implicitly converted to ascii. Personally, I
> prefer the boolean check and I'll let operations fail elsewhere
> if there's a type mismatch.

I have a quibble not with the functionality of the boolean check,
but with its expressiveness. if y[0] == "" expresses more, i.e.,
that I expect y[0] to contain a Python byte string.

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


Re: Checking if elements are empty

2007-09-10 Thread Chris Mellon
On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> On 2007-09-10, Chris Mellon <[EMAIL PROTECTED]> wrote:
> > On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> >> On 2007-09-08, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote:
> >> > Lawrence D'Oliveiro wrote:
> >> > if y[0]:
> >>  Not a good idea.
> >> >>> Why not?
> >> >>
> >> >> Because there is a situation where your version of the test
> >> >> will fail even if the first element of y is non-null.
> >> >
> >> > Such as? Seriously people, a little more verbosity wouldn't
> >> > hurt here. This isn't a mystery game.
> >>
> >> >>> if "": True
> >> ...
> >> >>> if 0: True
> >> ...
> >> >>> if []: True
> >> ...
> >> >>> if {}: True
> >> ...
> >> >>> if None: True
> >> ...
> >>
> >> That's may not be all the possibilities. Lots of Python objects
> >> can evaluate to false in a boolean context.
> >
> > All of these except 0 and None will also return False if you
> > check the length. In fact, I am not aware of any builtin where
> > len() will return 0, but it will be true in a boolean context.
> >
> > If you're advocating being more specific in the hope of
> > catching errors, this is a pretty terrible example. The OP
> > wasn't checking for "null", whatever that means - he was
> > checking for the empty string, in a list known (or intended) to
> > contain only strings. The simple boolean check is totally
> > appropriate.
>
> Agreed; but I prefer 'if y[0] == ""', absent more context and
> better names.
>

Probably should use u"" if you're going to take that route, as this
will fail spuriously if y[0] contains a unicode string that can't be
implicitly converted to ascii. Personally, I prefer the boolean check
and I'll let operations fail elsewhere if there's a type mismatch.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-10 Thread Neil Cerutti
On 2007-09-10, Chris Mellon <[EMAIL PROTECTED]> wrote:
> On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
>> On 2007-09-08, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote:
>> > Lawrence D'Oliveiro wrote:
>> > if y[0]:
>>  Not a good idea.
>> >>> Why not?
>> >>
>> >> Because there is a situation where your version of the test
>> >> will fail even if the first element of y is non-null.
>> >
>> > Such as? Seriously people, a little more verbosity wouldn't
>> > hurt here. This isn't a mystery game.
>>
>> >>> if "": True
>> ...
>> >>> if 0: True
>> ...
>> >>> if []: True
>> ...
>> >>> if {}: True
>> ...
>> >>> if None: True
>> ...
>>
>> That's may not be all the possibilities. Lots of Python objects
>> can evaluate to false in a boolean context.
>
> All of these except 0 and None will also return False if you
> check the length. In fact, I am not aware of any builtin where
> len() will return 0, but it will be true in a boolean context.
>
> If you're advocating being more specific in the hope of
> catching errors, this is a pretty terrible example. The OP
> wasn't checking for "null", whatever that means - he was
> checking for the empty string, in a list known (or intended) to
> contain only strings. The simple boolean check is totally
> appropriate.

Agreed; but I prefer 'if y[0] == ""', absent more context and
better names.

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


Re: Checking if elements are empty

2007-09-10 Thread Chris Mellon
On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> On 2007-09-08, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote:
> > Lawrence D'Oliveiro wrote:
> > if y[0]:
>  Not a good idea.
> >>> Why not?
> >>
> >> Because there is a situation where your version of the test
> >> will fail even if the first element of y is non-null.
> >
> > Such as? Seriously people, a little more verbosity wouldn't
> > hurt here. This isn't a mystery game.
>
> >>> if "": True
> ...
> >>> if 0: True
> ...
> >>> if []: True
> ...
> >>> if {}: True
> ...
> >>> if None: True
> ...
>
> That's may not be all the possibilities. Lots of Python objects
> can evaluate to false in a boolean context.
>

All of these except 0 and None will also return False if you check the
length. In fact, I am not aware of any builtin where len() will return
0, but it will be true in a boolean context.

If you're advocating being more specific in the hope of catching
errors, this is a pretty terrible example. The OP wasn't checking for
"null", whatever that means - he was checking for the empty string, in
a list known (or intended) to contain only strings. The simple boolean
check is totally appropriate.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-10 Thread Neil Cerutti
On 2007-09-08, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote:
> Lawrence D'Oliveiro wrote:
> if y[0]:
 Not a good idea.
>>> Why not?
>> 
>> Because there is a situation where your version of the test
>> will fail even if the first element of y is non-null.
>
> Such as? Seriously people, a little more verbosity wouldn't
> hurt here. This isn't a mystery game.

>>> if "": True
...
>>> if 0: True
...
>>> if []: True
...
>>> if {}: True
...
>>> if None: True
...

That's may not be all the possibilities. Lots of Python objects
can evaluate to false in a boolean context.

-- 
Neil Cerutti
The Pastor would appreciate it if the ladies of the congregation would lend
him their electric girdles for the pancake breakfast next Sunday morning.
--Church Bulletin Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-07 Thread Wildemar Wildenburger
Lawrence D'Oliveiro wrote:
 if y[0]:
>>> Not a good idea.
>> Why not?
> 
> Because there is a situation where your version of the test will fail even
> if the first element of y is non-null.

Such as? Seriously people, a little more verbosity wouldn't hurt here. 
This isn't a mystery game.

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


Re: Checking if elements are empty

2007-09-07 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Wildemar
Wildenburger wrote:

> Lawrence D'Oliveiro wrote:
>
>> In message <[EMAIL PROTECTED]>, Chris
>> Mellon wrote:
>> 
>>> On 9/5/07, Steve Holden <[EMAIL PROTECTED]> wrote:
 Doran, Harold wrote:
>
> Is there a way to check if the first element of y is null?
> 
 len(y[0]) == 0

>>> Better spelled as
>>>
>>> if y[0]:
>> 
>> Not a good idea.
> 
> Why not?

Because there is a situation where your version of the test will fail even
if the first element of y is non-null.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-07 Thread Wildemar Wildenburger
Steven D'Aprano wrote:
> On Fri, 07 Sep 2007 11:12:05 +0200, Wildemar Wildenburger wrote:
> 
>> Lawrence D'Oliveiro wrote:
>>> In message <[EMAIL PROTECTED]>, Chris
>>> Mellon wrote:
>>>
 On 9/5/07, Steve Holden <[EMAIL PROTECTED]> wrote:
> Doran, Harold wrote:
>> Is there a way to check if the first element of y is null?
>>
> len(y[0]) == 0
>
 Better spelled as

 if y[0]:
>>> Not a good idea.
>> Why not?
> 
> 
> What happens if y is an empty list?
> 
> 
An exception pops up, of course ;).

It all depends on the (intended) semantics of the program; the original 
question sounds like that list is supposed to have at least one element 
at that point, so there is no problem. And if it's not then that has to 
be dealt with, but that is not the problem here.
So the usual way to check if the first element of a list y is True in a 
boolean context is indeed "if y[0]:" (or for False of course "if not 
y[0]:").

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


Re: Checking if elements are empty

2007-09-07 Thread Steven D'Aprano
On Fri, 07 Sep 2007 11:12:05 +0200, Wildemar Wildenburger wrote:

> Lawrence D'Oliveiro wrote:
>> In message <[EMAIL PROTECTED]>, Chris
>> Mellon wrote:
>> 
>>> On 9/5/07, Steve Holden <[EMAIL PROTECTED]> wrote:
 Doran, Harold wrote:
> Is there a way to check if the first element of y is null?
>
 len(y[0]) == 0

>>> Better spelled as
>>>
>>> if y[0]:
>> 
>> Not a good idea.
> 
> Why not?


What happens if y is an empty list?


-- 
Steven.

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


Re: Checking if elements are empty

2007-09-07 Thread Wildemar Wildenburger
Lawrence D'Oliveiro wrote:
> In message <[EMAIL PROTECTED]>, Chris Mellon
> wrote:
> 
>> On 9/5/07, Steve Holden <[EMAIL PROTECTED]> wrote:
>>> Doran, Harold wrote:
 Is there a way to check if the first element of y is null?

>>> len(y[0]) == 0
>>>
>> Better spelled as
>>
>> if y[0]:
> 
> Not a good idea.

Why not?

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


Re: Checking if elements are empty

2007-09-06 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Chris Mellon
wrote:

> On 9/5/07, Steve Holden <[EMAIL PROTECTED]> wrote:
>> Doran, Harold wrote:
> 
>> >
>> > Is there a way to check if the first element of y is null?
>> >
>>
>> len(y[0]) == 0
>>
>> would be the obvious way, assuming "null" means "the null string".
>>
> 
> Better spelled as
> 
> if y[0]:

Not a good idea.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-05 Thread O.R.Senthil Kumaran
> Doran, Harold wrote:
> 
> I presume you meant
> 
> x = '  \t\'ff'
> 
> 
> > Is there a way to check if the first element of y is null?
> > 

You can use startswith() method of string objects.

if x.startswith(' '):
print True


-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-05 Thread Daniel Larsson
On 9/5/07, Chris Mellon <[EMAIL PROTECTED]> wrote:
>
> On 9/5/07, Steve Holden <[EMAIL PROTECTED]> wrote:
> > Doran, Harold wrote:
>
> > >
> > > Is there a way to check if the first element of y is null?
> > >
> >
> > len(y[0]) == 0
> >
> > would be the obvious way, assuming "null" means "the null string".
> >
>
> Better spelled as
>
> if y[0]:


Even better spelled as

if not y[0]:

;-)

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

Re: Checking if elements are empty

2007-09-05 Thread Chris Mellon
On 9/5/07, Steve Holden <[EMAIL PROTECTED]> wrote:
> Doran, Harold wrote:

> >
> > Is there a way to check if the first element of y is null?
> >
>
> len(y[0]) == 0
>
> would be the obvious way, assuming "null" means "the null string".
>

Better spelled as

if y[0]:
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if elements are empty

2007-09-05 Thread Steve Holden
Doran, Harold wrote:
> Dear list:
> 
> Suppose I have a string as follows
> 
> x = '  \t'ff'
> >>> x = '  \t'ff'
   File "", line 1
 x = '  \t'ff'
^
SyntaxError: invalid syntax
>>>

I presume you meant

x = '  \t\'ff'


> I can split this up as
> 
> y = x.split('\t')
> 
> Which gives
> 
> [ '  ', 'ff']
> 
> len(y)
> 2
> 
> Is there a way to check if the first element of y is null?
> 

len(y[0]) == 0

would be the obvious way, assuming "null" means "the null string".

If whitespace is significant that'll do, if it isn't then you may need 
to use the strip() method to remove it before your scheck.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

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