Re: [Python-ideas] Complicate str methods

2018-02-13 Thread Michel Desmoulin
Le 03/02/2018 à 23:04, Franklin? Lee a écrit : > Let s be a str. I propose to allow these existing str methods to take > params in new forms. > > s.replace(old, new): >     Allow passing in a collection of olds. >     Allow passing in a single argument, a mapping of olds to news. >     Allow the

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread Franklin? Lee
On Feb 8, 2018 13:06, "Serhiy Storchaka" wrote: 08.02.18 12:45, Franklin? Lee пише: > Could it be that re uses an optimization that can also be used in str? > CPython uses a modified Boyer-Moore for str.find: > https://github.com/python/cpython/blob/master/Objects/string > lib/fastsearch.h > htt

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread Franklin? Lee
On Thu, Feb 8, 2018 at 5:45 AM, Franklin? Lee wrote: > On Feb 7, 2018 17:28, "Serhiy Storchaka" wrote: > > The name of complicated str methods is regular expressions. For doing these > > operations efficiently you need to convert arguments in special optimized > > form. This is what re.compile()

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread Serhiy Storchaka
08.02.18 12:45, Franklin? Lee пише: On Feb 7, 2018 17:28, "Serhiy Storchaka" > wrote: Even for simple string search a regular expression can be more efficient than a str method. $ ./python -m timeit -s 'import re; p = re.compile("spam"); s = "spa"*100

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread INADA Naoki
I think it shouldn't be str's method. They should be separate class to reuse internal tree. There are some Aho Corasick implementation on PyPI. As far as I know, AC is longest match. On the other hand, Go's replacer (it's trie based too) is: > Replacements are performed in order, without overla

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread Franklin? Lee
On Thu, Feb 8, 2018 at 11:24 AM, Steve Dower wrote: > Easily fixed by installing one of the alternate regex libraries. MRAB's regex library, the most prominent alternative, does not use the linear-time search algorithm. The only libraries I know that do are the ones with re2, though I haven't loo

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread Steve Dower
Cc: Python-Ideas Subject: Re: [Python-ideas] Complicate str methods On Feb 7, 2018 17:28, "Serhiy Storchaka" wrote: 04.02.18 00:04, Franklin? Lee пише: Let s be a str. I propose to allow these existing str methods to take params in new forms. s.replace(old, new):      Allow pa

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread Sven R. Kunze
Same here. On 07.02.2018 22:57, Mike Miller wrote: +1 I have the need for one or two of these in every project (of a certain size) and have to come up with solutions each time with the re module or a loop. Not a fan of regex's for trivial tasks, or those that require a real parser. On 20

Re: [Python-ideas] Complicate str methods

2018-02-08 Thread Franklin? Lee
On Feb 7, 2018 17:28, "Serhiy Storchaka" wrote: 04.02.18 00:04, Franklin? Lee пише: Let s be a str. I propose to allow these existing str methods to take > params in new forms. > > s.replace(old, new): > Allow passing in a collection of olds. > Allow passing in a single argument, a map

Re: [Python-ideas] Complicate str methods

2018-02-07 Thread Serhiy Storchaka
04.02.18 00:04, Franklin? Lee пише: Let s be a str. I propose to allow these existing str methods to take params in new forms. s.replace(old, new):     Allow passing in a collection of olds.     Allow passing in a single argument, a mapping of olds to news.     Allow the olds in the mapping

Re: [Python-ideas] Complicate str methods

2018-02-07 Thread Mike Miller
+1 I have the need for one or two of these in every project (of a certain size) and have to come up with solutions each time with the re module or a loop. Not a fan of regex's for trivial tasks, or those that require a real parser. On 2018-02-03 14:04, Franklin? Lee wrote: __

Re: [Python-ideas] Complicate str methods

2018-02-07 Thread Neil Girdhar
On Saturday, February 3, 2018 at 8:10:38 PM UTC-5, Steven D'Aprano wrote: > > On Sun, Feb 04, 2018 at 10:54:53AM +1100, Chris Angelico wrote: > > > Picking up this one as an example, but this applies to all of them: > > the transformation you're giving here is dangerously flawed. If there > >

Re: [Python-ideas] Complicate str methods

2018-02-03 Thread Franklin? Lee
On Sat, Feb 3, 2018 at 6:43 PM, Terry Reedy wrote: > On 2/3/2018 5:04 PM, Franklin? Lee wrote: >> >> Let s be a str. I propose to allow these existing str methods to take >> params in new forms. > > > Thanks for the honest title. As you sort of indicate, these can all be done > with re module. H

Re: [Python-ideas] Complicate str methods

2018-02-03 Thread Steven D'Aprano
On Sun, Feb 04, 2018 at 10:54:53AM +1100, Chris Angelico wrote: > Picking up this one as an example, but this applies to all of them: > the transformation you're giving here is dangerously flawed. If there > are any regex special characters in the strings, this will either bomb > with an exception

Re: [Python-ideas] Complicate str methods

2018-02-03 Thread Chris Angelico
On Sun, Feb 4, 2018 at 10:43 AM, Terry Reedy wrote: > On 2/3/2018 5:04 PM, Franklin? Lee wrote: >> s.startswith, s.endswith: >> Allow argument to be a collection of strings. > > > bool(re.match('|'.join(strings)) does exactly the proposed s.startswith, > with the advantage that the actual mat

Re: [Python-ideas] Complicate str methods

2018-02-03 Thread Terry Reedy
On 2/3/2018 5:04 PM, Franklin? Lee wrote: Let s be a str. I propose to allow these existing str methods to take params in new forms. Thanks for the honest title. As you sort of indicate, these can all be done with re module. However, you imply loops are needed besides, which is mostly not t

[Python-ideas] Complicate str methods

2018-02-03 Thread Franklin? Lee
Let s be a str. I propose to allow these existing str methods to take params in new forms. s.replace(old, new): Allow passing in a collection of olds. Allow passing in a single argument, a mapping of olds to news. Allow the olds in the mapping to be tuples of strings. s.split(sep), s.