[Python-3000] Droping find/rfind?

Nick Coghlan ncoghlan at gmail.com
Thu Aug 24 13:48:22 CEST 2006


Walter Dörwald wrote:
> Guido van Rossum wrote:
> 
>> I don't find the current attempts to come up with a better substring
>> search API useful.
>>
>> [...]
>>
>> I appreciate the criticism on the patch -- clearly it's not ready to
>> go in, and more work needs to be put in to actually *improve* the
>> code, using [r]partition()  where necessary, etc. But I'm strenghtened
>> in the conclusion that find() is way overused and we don't need yet
>> another search primitive. TOOWTDI.
> 
> I don't see what's wrong with find() per se. IMHO in the following use
> case find() is the best option: Find the occurrences of "{foo bar}"
> patterns in the string and return both parts as a tuple. Return (None,
> "text") for the parts between the patterns, i.e. for
>    'foo{spam eggs}bar{foo bar}'
> return
>    [(None, 'foo'), ('spam', 'eggs'), (None, 'bar'), ('foo', 'bar')]

With a variety of "view types", that work like the corresponding builtin type, 
but reference the original data structure instead of creating copies, then you 
could use partition without having to worry about poor performance on large 
strings:

def splitview(s):
     rest = strview(s)
     while 1:
         prefix, found, rest = rest.partition("{")
         if prefix:
             yield (None, str(prefix))
         if not found:
             break
         first, found, rest = rest.partition("{")
         if not found:
             break
         second, found, rest = rest.partition("{")
         if not found:
             break
         yield (str(first), str(second))

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list