[issue39939] Add str methods to remove prefixes or suffixes

2020-03-20 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Just posted it.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-20 Thread STINNER Victor


STINNER Victor  added the comment:

Where should I leave comments on the PEP? Do you plan to post it on python-dev 
soon?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-20 Thread STINNER Victor


STINNER Victor  added the comment:

> https://github.com/python/peps/pull/1332

Thank you. And good luck for handling incoming discussions on the PEP ;-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-20 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

https://github.com/python/peps/pull/1332

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-20 Thread Walter Dörwald

Walter Dörwald  added the comment:

IMHO the names don't fit Pythons current naming scheme, so what about naming 
them "lchop" and "rchop"?

--
nosy: +doerwalter

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-20 Thread STINNER Victor


STINNER Victor  added the comment:

The PEP is a good start. Can you try to convert it to a PR on 
https://github.com/python/peps/ ? It seems like the next available PEP number 
is 616. I would prefer to leave comments on a PR.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-19 Thread Dennis Sweeney


Change by Dennis Sweeney :


Added file: https://bugs.python.org/file48989/pep-.rst

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-19 Thread Dennis Sweeney


Change by Dennis Sweeney :


Removed file: https://bugs.python.org/file48983/pep-.rst

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-19 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Here is a draft PEP -- I believe it needs a Core Developer sponsor now?

--
Added file: https://bugs.python.org/file48983/pep-.rst

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-18 Thread Guido van Rossum


Guido van Rossum  added the comment:

Sounds good.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-18 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

If no one has started, I can draft such a PEP.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-16 Thread STINNER Victor


STINNER Victor  added the comment:

The proposed change will affect many builtin types: bytes, bytearray, str, but 
also other types like collections.UserString. Would it make sense to summarize 
what has been said in the python-ideas thread into a PEP? It may good to 
specify things like:

>>> x = "A"*10**6
>>> x.cutprefix("B") is x
True

The specification can be just "that's an implementation detail" or "CPython 
implementation specific" :-)

I don't expect such PEP to be long nor controversial, but it may help to write 
it down.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-15 Thread Guido van Rossum


Guido van Rossum  added the comment:

I stopped following the discussion at some point, but I think this is worth 
adding it -- I have seen this done over and over again, and apparently lots of 
other people have felt the need too.

I think these names are fine, and about the best we can do (keeping in line 
with the "feel" of the rest of the string API).

I like the behavior of returning a copy of the string if there's no match (as 
opposed to failing, which was also brought up).  If the original object is 
immutable this should return the original object, but that should be considered 
a CPython optimization (IIRC all the string methods are pretty careful about 
that), but not required by the spec.

FWIW the pseudo code has a copy/paste error: In cutsuffix() it should use 
endswith() rather than startswith().

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-15 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Guido, do you support this API expansion?

--
nosy: +gvanrossum, rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-12 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Yes:

>>> x = "A"*10**6
>>> x.cutprefix("B") is x
True
>>> x.cutprefix("") is x
True

>>> y = b"A"*10**6
>>> y.cutprefix(b"B") is y
True
>>> y.cutprefix(b"") is y
True

>>> z = bytearray(b"A")*10**6
>>> z.cutprefix(b"B") is z
False
>>> z.cutprefix(b"") is z
False

I'm not sure whether this should be part of the spec or an implementation 
detail. The (str/bytes).replace method docs don't clarify this, but they have 
the same behavior:

>>> x = "A"*10**6
>>> x.replace("B", "C") is x
True
>>> x.replace("", "") is x
True

>>> y = b"A"*10**6
>>> y.replace(b"B", b"C") is y
True
>>> y.replace(b"", b"") is y
True

>>> z = bytearray(b"A")*10**6
>>> z.replace(b"B", b"C") is z
False
>>> z.replace(b"", b"") is z
False

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-12 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

To be clear, are you only making a copy of the unchanged object if it is a 
mutable bytearray, not str or bytes?

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-11 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-11 Thread STINNER Victor


Change by STINNER Victor :


--
nosy: +vstinner

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-11 Thread Eric V. Smith


Change by Eric V. Smith :


--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-11 Thread Dennis Sweeney


Change by Dennis Sweeney :


--
keywords: +patch
pull_requests: +18292
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/18939

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39939] Add str methods to remove prefixes or suffixes

2020-03-11 Thread Dennis Sweeney


New submission from Dennis Sweeney :

Following discussion here ( 
https://mail.python.org/archives/list/python-id...@python.org/thread/RJARZSUKCXRJIP42Z2YBBAEN5XA7KEC3/
 ), there is a proposal to add new methods str.cutprefix and str.cutsuffix to 
alleviate the common misuse of str.lstrip and str.rstrip.

I think sticking with the most basic possible behavior

def cutprefix(self: str, prefix: str) -> str:
if self.startswith(prefix):
return self[len(prefix):]
# return a copy to work for bytearrays
return self[:]

def cutsuffix(self: str, suffix: str) -> str:
if self.startswith(suffix):
# handles the "[:-0]" issue
return self[:len(self)-len(suffix)]
return self[:]

would be best (refusing to guess in the face of ambiguous multiple arguments). 
Someone can do, e.g.

>>> 'foo.tar.gz'.cutsuffix('.gz').cutsuffix('.tar')
'foo'

to cut off multiple suffixes. More complicated behavior for multiple arguments 
could be added later, but it would be easy to make a mistake in prematurely 
generalizing right now.

In bikeshedding method names, I think that avoiding the word "strip" would be 
nice so users can have a consistent feeling that "'strip' means character sets; 
'cut' means substrings".

--
components: Interpreter Core
messages: 363958
nosy: Dennis Sweeney
priority: normal
severity: normal
status: open
title: Add str methods to remove prefixes or suffixes
type: enhancement
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com