From: News123 on
Jason Friedman wrote:
> $ python
> Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> "x.vsd-dir".rstrip("-dir")
> 'x.vs'
>
> I expected 'x.vsd' as a return value.

This is kind of similiar to the question, that I posted recently.
"nicer way to remove prefix of a string if it exists"



if you want to remove '-dir' at the end of the string if it exists and
leave the string as it is if it is not followed by '-dir',

then you could do:

def rmv_suffix(suffix,txt):
if txt.endswith(suffix):
return txt[:-len(suffix)]
return txt

>>> rmv_suffix('-dir','abcd')
'abcd'
>>> rmv_suffix('-dir','abcd-dir')
'abcd'
>>> rmv_suffix('-dir','abcd-dir-and then more')
'abcd-dir-and then more'
>>>


the other solution would involve regular expressions:


import re
>>> re.sub('-dir$','','abcd')
'abcd'
>>> re.sub('-dir$','','abcd-dir')
'abcd'
>>> re.sub('-dir$','','abcd-dirand more')
'abcd-dirand more'
>>>



From: Chris Rebert on
On Fri, Jul 16, 2010 at 10:27 AM, MRAB <python(a)mrabarnett.plus.com> wrote:
> Jason Friedman wrote:
>>
>> $ python
>> Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
>> [GCC 4.4.1] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>
>>>>> "x.vsd-dir".rstrip("-dir")
>>
>> 'x.vs'
>>
>> I expected 'x.vsd' as a return value.
>
> .strip, .lstrip and .rstrip treat their argument like a set of
> characters and remove any of those characters from the end(s) of the
> string.

It's a pity that str.strip() doesn't actually take a set() of length-1
strings, which would make its behavior more obvious and cut down on
this perennial question.

Cheers,
Chris
--
http://blog.rebertia.com
From: MRAB on
Chris Rebert wrote:
> On Fri, Jul 16, 2010 at 10:27 AM, MRAB <python(a)mrabarnett.plus.com> wrote:
>> Jason Friedman wrote:
>>> $ python
>>> Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
>>> [GCC 4.4.1] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> "x.vsd-dir".rstrip("-dir")
>>> 'x.vs'
>>>
>>> I expected 'x.vsd' as a return value.
>> .strip, .lstrip and .rstrip treat their argument like a set of
>> characters and remove any of those characters from the end(s) of the
>> string.
>
> It's a pity that str.strip() doesn't actually take a set() of length-1
> strings, which would make its behavior more obvious and cut down on
> this perennial question.
>
Even better, a set (or tuple) of strings. It's the kind of thing that
could've been done in Python 3, with Python 2's .strip(string) becoming
..strip(set(string)), but it didn't occur to me until too late. :-(
From: Mark Lawrence on
On 17/07/2010 23:17, MRAB wrote:
> Chris Rebert wrote:
>> On Fri, Jul 16, 2010 at 10:27 AM, MRAB <python(a)mrabarnett.plus.com>
>> wrote:
>>> Jason Friedman wrote:
>>>> $ python
>>>> Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
>>>> [GCC 4.4.1] on linux2
>>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>>> "x.vsd-dir".rstrip("-dir")
>>>> 'x.vs'
>>>>
>>>> I expected 'x.vsd' as a return value.
>>> .strip, .lstrip and .rstrip treat their argument like a set of
>>> characters and remove any of those characters from the end(s) of the
>>> string.
>>
>> It's a pity that str.strip() doesn't actually take a set() of length-1
>> strings, which would make its behavior more obvious and cut down on
>> this perennial question.
>>
> Even better, a set (or tuple) of strings. It's the kind of thing that
> could've been done in Python 3, with Python 2's .strip(string) becoming
> .strip(set(string)), but it didn't occur to me until too late. :-(

Maybe 3.2 which is still in alpha, if not 3.3?

Kindest regards.

Mark Lawrence.

From: News123 on
Mark Lawrence wrote:
> On 17/07/2010 23:17, MRAB wrote:
>> Chris Rebert wrote:
>>> On Fri, Jul 16, 2010 at 10:27 AM, MRAB <python(a)mrabarnett.plus.com>
>>> wrote:
>>>> Jason Friedman wrote:
>>>
>>> It's a pity that str.strip() doesn't actually take a set() of length-1
>>> strings, which would make its behavior more obvious and cut down on
>>> this perennial question.
>>>
>> Even better, a set (or tuple) of strings. It's the kind of thing that
>> could've been done in Python 3, with Python 2's .strip(string) becoming
>> .strip(set(string)), but it didn't occur to me until too late. :-(
>
> Maybe 3.2 which is still in alpha, if not 3.3?
>
> Kindest regards.
>
> Mark Lawrence.
>

It could even be introduced without breaking compatibility.

if being defined as
str.rstrip([iterable])
so you could either call
string.rstrip( [ '-dir' ] )
or as
string.rstrip( '-dir' )


However I wouldn't be sure, that it really reduces the amount of
questions being asked.

In order to reduce the ambiguities one had to have two distinct functions.
If one wouldn't want to break backwards-compatibility, then the new
names would be for stripping off prefixes / suffixes and could be
str.strip_prefix(prefixes) / str.rstrip_suffix(suffixes)


I'd love to have this functionality, though I can live with importing my
self written function.