Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie question: if var1 == var2:

Reply
Thread Tools

newbie question: if var1 == var2:

 
 
greg
Guest
Posts: n/a
 
      12-11-2008
Kirk Strauser wrote:
> At 2008-12-11T19:49:23Z, Steve Holden <(E-Mail Removed)> writes:
>
>> item = item[:-1]

>
> It's easy - and broken. Bad things happen if you're using something other
> than '\n' for EOL.


Or if the last line of your file doesn't end
with a newline.

--
Greg

 
Reply With Quote
 
 
 
 
Jason Scheirer
Guest
Posts: n/a
 
      12-11-2008
On Dec 11, 3:49*pm, John Machin <(E-Mail Removed)> wrote:
> On Dec 12, 10:31*am, "Rhodri James" <(E-Mail Removed)>
> wrote:
>
>
>
> > On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <(E-Mail Removed)> *
> > wrote:

>
> > > Kirk Strauser wrote:
> > >> At 2008-11-29T04:02:11Z, Mel <(E-Mail Removed)> writes:

>
> > >>> You could try

>
> > >>> for item in fname:
> > >>> * * item = item.strip()

>
> > >> This is one case where I really miss Perl's "chomp" function. *It *
> > >> removes a
> > >> trailing newline and nothing else, so you don't have to worry about *
> > >> losing
> > >> leading or trailing spaces if those are important to you.

>
> > > ... and it's so hard to write

>
> > > * * *item = item[:-1]

>
> > Tsk. *That would be "chop". *"chomp" would be

>
> > * * *if item[-1] == '\n':
> > * * * * *item = item[:-1]

>
> Better:
> if item and item[-1] == '\n':
> * * return item[:-1]
> return item


Best:

return item \
if not (item and item.endswith('\n')) \
else item[:-1]

Though really you should be using item.rstrip()
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      12-12-2008
Jason Scheirer wrote:
> On Dec 11, 3:49 pm, John Machin <(E-Mail Removed)> wrote:
>> On Dec 12, 10:31 am, "Rhodri James" <(E-Mail Removed)>
>> wrote:
>>
>>
>>
>>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <(E-Mail Removed)>
>>> wrote:
>>>> Kirk Strauser wrote:
>>>>> At 2008-11-29T04:02:11Z, Mel <(E-Mail Removed)> writes:
>>>>>> You could try
>>>>>> for item in fname:
>>>>>> item = item.strip()
>>>>> This is one case where I really miss Perl's "chomp" function. It
>>>>> removes a
>>>>> trailing newline and nothing else, so you don't have to worry about
>>>>> losing
>>>>> leading or trailing spaces if those are important to you.
>>>> ... and it's so hard to write
>>>> item = item[:-1]
>>> Tsk. That would be "chop". "chomp" would be
>>> if item[-1] == '\n':
>>> item = item[:-1]

>> Better:
>> if item and item[-1] == '\n':
>> return item[:-1]
>> return item

>
> Best:
>
> return item \
> if not (item and item.endswith('\n')) \
> else item[:-1]
>
> Though really you should be using item.rstrip()
>

Why not just:

item[:-1] if item.endswith('\n') else item
 
Reply With Quote
 
Rhodri James
Guest
Posts: n/a
 
      12-12-2008
On Thu, 11 Dec 2008 23:49:10 -0000, John Machin <(E-Mail Removed)>
wrote:

> On Dec 12, 10:31*am, "Rhodri James" <(E-Mail Removed)>
> wrote:
>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <(E-Mail Removed)> *
>> wrote:
>> > ... and it's so hard to write

>>
>> > * * *item = item[:-1]

>>
>> Tsk. *That would be "chop". *"chomp" would be
>>
>> * * *if item[-1] == '\n':
>> * * * * *item = item[:-1]

>
> Better:
> if item and item[-1] == '\n':
> return item[:-1]
> return item


If we aren't going for rstrip, yes, but I was trying to stick to Steve's
metier.

--
Rhodri James *-* Wildebeeste Herder to the Masses
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-12-2008
On Dec 12, 11:39*am, MRAB <(E-Mail Removed)> wrote:
> Jason Scheirer wrote:
> > On Dec 11, 3:49 pm, John Machin <(E-Mail Removed)> wrote:
> >> On Dec 12, 10:31 am, "Rhodri James" <(E-Mail Removed)>
> >> wrote:

>
> >>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <(E-Mail Removed)> *
> >>> wrote:
> >>>> Kirk Strauser wrote:
> >>>>> At 2008-11-29T04:02:11Z, Mel <(E-Mail Removed)> writes:
> >>>>>> You could try
> >>>>>> for item in fname:
> >>>>>> * * item = item.strip()
> >>>>> This is one case where I really miss Perl's "chomp" function. *It *
> >>>>> removes a
> >>>>> trailing newline and nothing else, so you don't have to worry about *
> >>>>> losing
> >>>>> leading or trailing spaces if those are important to you.
> >>>> ... and it's so hard to write
> >>>> * * *item = item[:-1]
> >>> Tsk. *That would be "chop". *"chomp" would be
> >>> * * *if item[-1] == '\n':
> >>> * * * * *item = item[:-1]
> >> Better:
> >> if item and item[-1] == '\n':
> >> * * return item[:-1]
> >> return item

>
> > Best:

>
> > return item \
> > * * * *if not (item and item.endswith('\n')) \
> > * * * *else item[:-1]

>
> > Though really you should be using item.rstrip()

>
> Why not just:
>
> * * *item[:-1] if item.endswith('\n') else item


Some possible reasons:
* because you might be supporting old versions of Python (my offering
runs on 1.5)
* because the "<true_value> if <condition> else <false_value>" syntax
gives you the screaming dry Edgar Britts
* because you'd prefer not to have the overhead of a method lookup and
method call
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      12-12-2008
John Machin wrote:
> On Dec 12, 11:39 am, MRAB <(E-Mail Removed)> wrote:
>> Jason Scheirer wrote:
>>> On Dec 11, 3:49 pm, John Machin <(E-Mail Removed)> wrote:
>>>> On Dec 12, 10:31 am, "Rhodri James" <(E-Mail Removed)>
>>>> wrote:
>>>>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <(E-Mail Removed)>
>>>>> wrote:
>>>>>> Kirk Strauser wrote:
>>>>>>> At 2008-11-29T04:02:11Z, Mel <(E-Mail Removed)> writes:
>>>>>>>> You could try
>>>>>>>> for item in fname:
>>>>>>>> item = item.strip()
>>>>>>> This is one case where I really miss Perl's "chomp" function. It
>>>>>>> removes a
>>>>>>> trailing newline and nothing else, so you don't have to worry about
>>>>>>> losing
>>>>>>> leading or trailing spaces if those are important to you.
>>>>>> ... and it's so hard to write
>>>>>> item = item[:-1]
>>>>> Tsk. That would be "chop". "chomp" would be
>>>>> if item[-1] == '\n':
>>>>> item = item[:-1]
>>>> Better:
>>>> if item and item[-1] == '\n':
>>>> return item[:-1]
>>>> return item
>>> Best:
>>> return item \
>>> if not (item and item.endswith('\n')) \
>>> else item[:-1]
>>> Though really you should be using item.rstrip()

>> Why not just:
>>
>> item[:-1] if item.endswith('\n') else item

>
> Some possible reasons:
> * because you might be supporting old versions of Python (my offering
> runs on 1.5)
> * because the "<true_value> if <condition> else <false_value>" syntax
> gives you the screaming dry Edgar Britts
> * because you'd prefer not to have the overhead of a method lookup and
> method call
>

OK:

if item[-1:] == '\n':
return item[:-1]
return item
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-12-2008
On Dec 12, 1:11*pm, MRAB <(E-Mail Removed)> wrote:
> John Machin wrote:
> > On Dec 12, 11:39 am, MRAB <(E-Mail Removed)> wrote:
> >> Jason Scheirer wrote:
> >>> On Dec 11, 3:49 pm, John Machin <(E-Mail Removed)> wrote:
> >>>> On Dec 12, 10:31 am, "Rhodri James" <(E-Mail Removed)>
> >>>> wrote:
> >>>>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <(E-Mail Removed)> *
> >>>>> wrote:
> >>>>>> Kirk Strauser wrote:
> >>>>>>> At 2008-11-29T04:02:11Z, Mel <(E-Mail Removed)> writes:
> >>>>>>>> You could try
> >>>>>>>> for item in fname:
> >>>>>>>> * * item = item.strip()
> >>>>>>> This is one case where I really miss Perl's "chomp" function. *It *
> >>>>>>> removes a
> >>>>>>> trailing newline and nothing else, so you don't have to worry about *
> >>>>>>> losing
> >>>>>>> leading or trailing spaces if those are important to you.
> >>>>>> ... and it's so hard to write
> >>>>>> * * *item = item[:-1]
> >>>>> Tsk. *That would be "chop". *"chomp" would be
> >>>>> * * *if item[-1] == '\n':
> >>>>> * * * * *item = item[:-1]
> >>>> Better:
> >>>> if item and item[-1] == '\n':
> >>>> * * return item[:-1]
> >>>> return item
> >>> Best:
> >>> return item \
> >>> * * * *if not (item and item.endswith('\n')) \
> >>> * * * *else item[:-1]
> >>> Though really you should be using item.rstrip()
> >> Why not just:

>
> >> * * *item[:-1] if item.endswith('\n') else item

>
> > Some possible reasons:
> > * because you might be supporting old versions of Python (my offering
> > runs on 1.5)
> > * because the "<true_value> if <condition> else <false_value>" syntax
> > gives you the screaming dry Edgar Britts
> > * because you'd prefer not to have the overhead of a method lookup and
> > method call

>
> OK:
>
> * * *if item[-1:] == '\n':
> * * * * *return item[:-1]
> * * *return item


I'll pay that
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      12-12-2008
Kirk Strauser wrote:
> At 2008-12-12T15:35:11Z, "J. Cliff Dyer" <(E-Mail Removed)> writes:
>
>> Python has a version equally good:
>>
>> def chomp(s):
>> return s.rstrip('\r\n')
>>
>> You'll hardly miss Perl at all.

>
> I haven't missed Perl in years! I just wish there was a basestring.stripeol
> method because I seem to end up writing the inline version of "chomp" every
> time I iterate across a file.
>

Python is open source. You could suggest adding it and/or provide a patch.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(type)*(var1)-like casting grishin-mailing-lists@minselhoz.samara.ru C Programming 5 03-16-2010 04:15 PM
how to do: var1 = var2.new(.....) (var2 contains classname) Luca Scaljery Ruby 5 02-14-2007 08:51 PM
why only var1+=var2,why not var1=+var2 malli C++ 7 10-21-2005 06:52 PM
How to replace: #if VAR1 == 100 ... #elif VAR2 = 1 ... #endif, With #ifdef VAR1 ... ? ... #endif Christopher M. Lusardi C++ 1 11-19-2004 05:33 AM
How to replace: #if VAR1 == 100 ... #elif VAR2 = 1 ... #endif, With #ifdef VAR1 ... ? ... #endif Christopher M. Lusardi C Programming 1 11-19-2004 05:33 AM



Advertisments