On 7/2/2019 12:56 PM, Terry Reedy wrote:
On 7/2/2019 12:16 AM, Glenn Linderman wrote:
On 7/1/2019 8:28 PM, Terry Reedy wrote:
I did not read much of the thread, but the proposal is to wrap a near-trivial expression (2 operations) or replace a current method call with a method call than is more or less the same len as what it replaces.
'prefix_more_suffix'[len('prefix'):] # if know s begins with p
'prefix_more_suffix'.strip_pre('prefix') # proposed
Your example isn't equivalent: .replace would replace multiple instances of prefix, not necessarily at the beginning. The .strip_pre (however spelled) would strip one instance, only if it is at the beginning.
I left out the slightly longer regex which does exactly that.
re.sub('^prefix', '', 'prefix_more_suffix')
re.sub('^prefix', '', 'Xprefix_more_suffix')
or with minor variation, deletes only suffixes.
re.sub('suffix$', '', 'prefix_more_suffix')
Are these special cases, which can be easily generalized to match more than one string, and to replace rather than delete, special enough to merit a new method each. This remains to be demonstrated.
The more I thought about this, the more I think a more functional goal is a variation of replace that works only on one end or the other, rather than a variation of strip. I outlined that in a different branch in this thread.
To me, a new parameter to str.replace would have a lower bar to acceptance.
two extra boolean parameters has been suggested; alternatives would be a single string or flag parameter. replace already accepts a count parameter for maximum number of replacements from the start. One could extend that to pass a negative count parameter to mean how many from the end, which is similar to negative indexing for slices, which wouldn't even need an extra parameter, but also wouldn't restrict it to the exact front or end. Together with one extra boolean to declare the replacement must be at the very front (for positive count) or very end (for negative count), would also allow removal of repeated prefixes or suffixes, although I'm not sure that would be a common case:
text = 'Hello Hello Hello Is this Fred?' text.replace('Hello ', '', 2, True )
would leave one Hello.
Of course it wouldn't have that effect with a non-empty replacement:
text.replace('Hello ', 'Greetings', 2, True )
would leave two Hello.
The other documentation issue I noticed is that the 2nd and 3rd parameters to startswith and endswith are not fully documented. Typically startswith and endswitch are in the logic prior to the strip/replace operation, and typically only use the first parameter, but looking at their documentation as part of this discussion, I found it lacking.
File an issue? Preferably with a suggested change.
To suggest a change, I'd first have run lots of experiments to figure out what they do... and what the edge conditions are... or, I'd have to read the implementation to figure it out. I've never used anything but the first parameter and reading the documentation for the other two raises more questions than it answers. But I'll open the issue, maybe someone can quickly answer the questions I raise, but the terseness of the description of the parameters leaves a lot of room for speculation.