Re: [Python-ideas] Revisiting str.rreplace()

2018-07-21 Thread Chris Barker - NOAA Federal via Python-ideas
I starting g reading this thread in the middle, on a phone.

But was very confused for a while because I didn’t notice that there
were two ‘r’s at the beginning of .rreplace

Just sayin’

-CHB

Sent from my iPhone

> On Jul 19, 2018, at 9:29 AM, Paul Moore  wrote:
>
>> On 19 July 2018 at 16:25, Eric V. Smith  wrote:
>> It currently does something: it replaces all instances, just as if you
>> hadn't supplied a count (see my example below). You can't change its
>> behavior.
>
> ... without a deprecation cycle. Which is of course not worth it for
> something which could much more easily be done by adding an rreplace
> function - which is the real point of the comment.
> Paul
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Paul Moore
On 19 July 2018 at 16:25, Eric V. Smith  wrote:
> It currently does something: it replaces all instances, just as if you
> hadn't supplied a count (see my example below). You can't change its
> behavior.

... without a deprecation cycle. Which is of course not worth it for
something which could much more easily be done by adding an rreplace
function - which is the real point of the comment.
Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Eric V. Smith

On 7/19/2018 11:22 AM, Calvin Spealman wrote:
If its treated as a missing parameter, and currently doesn't do 
anything, then it wouldn't be used... right? and it could be safe to add 
behavior for it... right?


It currently does something: it replaces all instances, just as if you 
hadn't supplied a count (see my example below). You can't change its 
behavior.


Eric



On Thu, Jul 19, 2018 at 11:17 AM, Eric V. Smith > wrote:


On 7/19/2018 10:01 AM, Calvin Spealman wrote:

As an alternative suggestion: What if the count parameter to
str.replace() counted from the right with negative values? That
would be consistent with other things like indexing and slicing.


We couldn't make this change because negative values already have a
meaning: it's interpreted as a missing parameter:

 >>> 'abab'.replace('a', 'z')
'zbzb'
 >>> 'abab'.replace('a', 'z', 0)
'abab'
 >>> 'abab'.replace('a', 'z', 1)
'zbab'
 >>> 'abab'.replace('a', 'z', -1)
'zbzb'
 >>> 'abab'.replace('a', 'z', -2)
'zbzb'
 >>> 'abab'.replace('a', 'z', -100)
'zbzb'

I think .rreplace() is the better design.

Eric



___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread MRAB

On 2018-07-19 16:22, Calvin Spealman wrote:
If its treated as a missing parameter, and currently doesn't do 
anything, then it wouldn't be used... right? and it could be safe to add 
behavior for it... right?



Are you sure that it wouldn't break some existing code?

Plus, we already have .find/.rfind, .index/.rindex, etc, so 
.replace/.rreplace would be a better fit.


On Thu, Jul 19, 2018 at 11:17 AM, Eric V. Smith > wrote:


On 7/19/2018 10:01 AM, Calvin Spealman wrote:

As an alternative suggestion: What if the count parameter to
str.replace() counted from the right with negative values? That
would be consistent with other things like indexing and slicing.


We couldn't make this change because negative values already have a
meaning: it's interpreted as a missing parameter:

 >>> 'abab'.replace('a', 'z')
'zbzb'
 >>> 'abab'.replace('a', 'z', 0)
'abab'
 >>> 'abab'.replace('a', 'z', 1)
'zbab'
 >>> 'abab'.replace('a', 'z', -1)
'zbzb'
 >>> 'abab'.replace('a', 'z', -2)
'zbzb'
 >>> 'abab'.replace('a', 'z', -100)
'zbzb'

I think .rreplace() is the better design.


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Calvin Spealman
If its treated as a missing parameter, and currently doesn't do anything,
then it wouldn't be used... right? and it could be safe to add behavior for
it... right?

On Thu, Jul 19, 2018 at 11:17 AM, Eric V. Smith  wrote:

> On 7/19/2018 10:01 AM, Calvin Spealman wrote:
>
>> As an alternative suggestion: What if the count parameter to
>> str.replace() counted from the right with negative values? That would be
>> consistent with other things like indexing and slicing.
>>
>
> We couldn't make this change because negative values already have a
> meaning: it's interpreted as a missing parameter:
>
> >>> 'abab'.replace('a', 'z')
> 'zbzb'
> >>> 'abab'.replace('a', 'z', 0)
> 'abab'
> >>> 'abab'.replace('a', 'z', 1)
> 'zbab'
> >>> 'abab'.replace('a', 'z', -1)
> 'zbzb'
> >>> 'abab'.replace('a', 'z', -2)
> 'zbzb'
> >>> 'abab'.replace('a', 'z', -100)
> 'zbzb'
>
> I think .rreplace() is the better design.
>
> Eric
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Eric V. Smith

On 7/19/2018 10:01 AM, Calvin Spealman wrote:
As an alternative suggestion: What if the count parameter to 
str.replace() counted from the right with negative values? That would be 
consistent with other things like indexing and slicing.


We couldn't make this change because negative values already have a 
meaning: it's interpreted as a missing parameter:


>>> 'abab'.replace('a', 'z')
'zbzb'
>>> 'abab'.replace('a', 'z', 0)
'abab'
>>> 'abab'.replace('a', 'z', 1)
'zbab'
>>> 'abab'.replace('a', 'z', -1)
'zbzb'
>>> 'abab'.replace('a', 'z', -2)
'zbzb'
>>> 'abab'.replace('a', 'z', -100)
'zbzb'

I think .rreplace() is the better design.

Eric
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Calvin Spealman
It would be consistent to apply it to other functions and I'd be in favour
of that, yes.

On Thu, Jul 19, 2018 at 10:06 AM, Eric Fahlgren 
wrote:

> On Thu, Jul 19, 2018 at 7:01 AM Calvin Spealman 
> wrote:
>
>> As an alternative suggestion: What if the count parameter to
>> str.replace() counted from the right with negative values? That would be
>> consistent with other things like indexing and slicing.
>>
>
> ​That could certainly be made to work, but I think it fails the
> discoverability test.  You'd have the [:]-syntax and replace working one
> way, and split/rsplit, find/rfind and so on working another way.  Unless
> you're proposing the negative index for the latter ones also?​
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Eric Fahlgren
On Thu, Jul 19, 2018 at 7:01 AM Calvin Spealman  wrote:

> As an alternative suggestion: What if the count parameter to str.replace()
> counted from the right with negative values? That would be consistent with
> other things like indexing and slicing.
>

​That could certainly be made to work, but I think it fails the
discoverability test.  You'd have the [:]-syntax and replace working one
way, and split/rsplit, find/rfind and so on working another way.  Unless
you're proposing the negative index for the latter ones also?​
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Calvin Spealman
As an alternative suggestion: What if the count parameter to str.replace()
counted from the right with negative values? That would be consistent with
other things like indexing and slicing.

On Thu, Jul 19, 2018 at 9:47 AM, Eric Fahlgren 
wrote:

> On Wed, Jul 18, 2018 at 8:20 PM Graham Gott  com> wrote:
>
>>
>> Thoughts? Support/oppose?
>>
>
> ​
> +1, along with an overall rework of str methods to make them more
> consistent.
>
> The find, replace and split families should all gain the same
> tuple-as-search-string that endswith and startswith use.  (Discussed in the
> last year, I'm too lazy to look up references...)
>
> ​
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Revisiting str.rreplace()

2018-07-19 Thread Eric Fahlgren
On Wed, Jul 18, 2018 at 8:20 PM Graham Gott 
wrote:

>
> Thoughts? Support/oppose?
>

​
+1, along with an overall rework of str methods to make them more
consistent.

The find, replace and split families should all gain the same
tuple-as-search-string that endswith and startswith use.  (Discussed in the
last year, I'm too lazy to look up references...)

​
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Revisiting str.rreplace()

2018-07-18 Thread Graham Gott
This was previously proposed here in 2014 <
https://mail.python.org/pipermail/python-ideas/2014-January/025091.html>,
but the discussion fizzled out. To me, str.rreplace() is an obvious and
necessary complement to str.replace(), just as str.rsplit() is a complement
to str.split(). It would make Python closer to the goal of the Zen of
Python: "There should be one-- and preferably only one --obvious way to do
it." To support its usefulness, this question has been asked on Stack
Overflow a number of times, (, <
https://stackoverflow.com/q/14496006>, )
with more than a trivial number of votes for the answers (>100 for the top
answer of the first two questions, and >50 for the third, not necessarily
mutually exclusive voters, but probably largely; even assuming the worst,
>100 is nothing to scoff at). While anonymous Stack Overflow votes are not
necessarily the best arbiter on what's a good idea, they at least show
interest.

My proposed behavior (though probably not the implementation details) would
be as follows (though the implementation would obviously be as a string
method, not a function, with 'self' replacing 's'):



def rreplace(s, old, new, count=-1):
'''
Return a copy with all occurrences of substring old replaced by new.

  count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences

Substrings are replaced starting at the end of the string and working
to the front.
'''
return new.join(s.rsplit(old, count))



Addressing some (paraphrased) issues from the previous discussion, in the
form of an FAQ:

rreplace() looks weird.
>

This is maintaining consistency with the trend of 'r' prefixes for
'reverse' methods. Introducing 'reverse_' as a prefix would be
inconsistent, but worse, it would also encourage backwards incompatible
changes into existing methods. I think such a prefix naming change warrants
its own separate discussion.

There are already ways to do this. Why should we add another string method?
>

My main motivation is having one clear and efficient way to do this. I
explain this in greater detail in my introduction above.

The default of count=-1 has the same behavior as str.replace(), right?
>

Actually, it doesn't have the same behavior, as MRAB pointed out in the
previous discussion <
https://mail.python.org/pipermail/python-ideas/2014-January/025102.html>.
The default of -1 also keeps the syntax consistent with str.rsplit()'s
syntax.

If we're adding this, shouldn't we also add bytes.rreplace,
> bytearray.rreplace, bytearray.rremove, tuple.rindex, list.rindex,
> list.rremove?
>

Honestly, I don't know. I would prefer not to dilute this discussion too
much, or start making a slippery slope argument, but if it's directly
relevant, I think any thoughts are welcome.

Couldn't we just add a traverse_backwards parameter to str.replace()? In
> fact, why don't we just get rid of str.rfind() and str.rindex() entirely
> and just add new parameters to str.find() and str.index()?
>

I think Steven D'Aprano explained this well in the previous discussion
here: <
https://mail.python.org/pipermail/python-ideas/2014-January/025132.html>.
And addressing counterarguments here: <
https://mail.python.org/pipermail/python-ideas/2014-January/025135.html>.
Basically, different functions for different purposes make the purpose
clearer (no confusing/complicated parameter names and functions), and
str.rreplace() and str.replace() are usually going to be used in situations
where the traversal direction is known at edit time, so there's no need to
make the method determine the direction at runtime.



Thoughts? Support/oppose?
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/