Re: [Python-ideas] adding a trim convenience function
On Wed, Mar 5, 2008 at 1:30 PM, Matthew Russell <matt.horizon5@gmail.com> wrote:
Of couse that should of read: sites = list(item.strip("http://") for item in sites)
{l,r,}strip doesn't actually do what I'm talking about, which confused me for a long time. Consider this simple case:
'abaaabcd'.lstrip('ab') 'cd'
ltrim in this case would produce 'aaabcd'. On Wed, Mar 5, 2008 at 1:33 PM, Terry Reedy <tjreedy@udel.edu> wrote:
[site.replace('http://', '')for site in sites] ['www.google.com', 'python.org', 'www.yahoo.com']
Unfortunately that would break down in certain cases with a different strings, like 'foo bar foo'.replace('foo', ''), which just results in ' bar '.
Try another use case. I think str pretty well has the basic tools needed to construct whatever specific tools one needs.
Oh sure it can, considering that I can implement ltrim in three lines. This is just to reduce a common pattern in my code, and to remove rewriting it in multiple projects.
"Erick Tryzelaar" <idadesub@users.sourceforge.net> wrote in message | On Wed, Mar 5, 2008 at 1:33 PM, Terry Reedy <tjreedy@udel.edu> wrote: | > >>> [site.replace('http://', '')for site in sites] | > ['www.google.com', 'python.org', 'www.yahoo.com'] | | Unfortunately that would break down in certain cases with a different | strings, like 'foo bar foo'.replace('foo', ''), which just results in | ' bar '. I knew that, of course, but that objection does not apply to the use case you presented. I simply gave the simplest thing that worked, that passed your 'test'. Tal gave a more general answer. | > Try another use case. I think str pretty well has the basic tools needed | > to construct whatever specific tools one needs. | | Oh sure it can, considering that I can implement ltrim in three lines. | This is just to reduce a common pattern in my code, and to remove | rewriting it in multiple projects. Who many such uses are like your 'foo bar for'? tjr
On Thu, Mar 6, 2008 at 11:37 AM, Terry Reedy <tjreedy@udel.edu> wrote: fyi, I looked through a bunch of code, and it does seem that there is less need for this than I thought.
Who many such uses are like your 'foo bar for'?
The case I ran into is that I used in a fashion like 'abaaab'.lstrip('ab') before I understood exactly what strip did. The replace trick won't work for me because all of the instances where I used this were in an api, so I couldn't assume that the string i was trimming didn't have other instances of the prefix/suffix in the middle.
On Thu, Mar 6, 2008 at 3:16 PM, Erick Tryzelaar <idadesub@users.sourceforge.net> wrote:
On Thu, Mar 6, 2008 at 11:37 AM, Terry Reedy <tjreedy@udel.edu> wrote:
fyi, I looked through a bunch of code, and it does seem that there is less need for this than I thought.
Who many such uses are like your 'foo bar for'?
The case I ran into is that I used in a fashion like 'abaaab'.lstrip('ab') before I understood exactly what strip did. The replace trick won't work for me because all of the instances where I used this were in an api, so I couldn't assume that the string i was trimming didn't have other instances of the prefix/suffix in the middle.
What about adding an optional boolean parameter to str.*strip that treats the argument as either a set of characters (default, just like now) or an exact string ? Something like
'abaaab'.lstrip('ab') '' 'abaaab'.lstrip('ab', exact=True) 'aaab'
George
participants (3)
-
Erick Tryzelaar
-
George Sakkis
-
Terry Reedy