[Python-Dev] Remove str.find in 3.0?

M.-A. Lemburg mal at egenix.com
Sun Aug 28 20:33:58 CEST 2005


Raymond Hettinger wrote:
> [Marc-Andre Lemburg]
> 
>>I may be missing something, but why invent yet another parsing
>>method - we already have the re module. I'd suggest to
>>use it :-)
>>
>>If re is not fast enough or you want more control over the
>>parsing process, you could also have a look at mxTextTools:
>>
>>    http://www.egenix.com/files/python/mxTextTools.html
> 
> 
> Both are excellent tools.  Neither is as lightweight, as trivial to
> learn, or as transparently obvious as the proposed s.partition(sep).
> The idea is to find a viable replacement for s.find().

Your partition idea could be had with an additional argument
to .split() (e.g. keepsep=1); no need to learn a new method.

Also, as I understand Terry's request, .find() should be removed
in favor of just leaving .index() (which is the identical method
without the funny -1 return code logic).

So your proposal really doesn't have all that much to do
with Terry's request, but is a new and separate proposal
(which does have some value in few cases, but not enough
to warrant a new method).

> Looking at sample code transformations shows that the high-power
> mxTextTools and re approaches do not simplify code that currently uses
> s.find().  In contrast, the proposed partition() method is a joy to use
> and has no surprises.  The following code transformation shows
> unbeatable simplicity and clarity.
> 
> 
> --- From CGIHTTPServer.py ---------------
> 
> def run_cgi(self):
>     """Execute a CGI script."""
>     dir, rest = self.cgi_info
>     i = rest.rfind('?')
>     if i >= 0:
>         rest, query = rest[:i], rest[i+1:]
>     else:
>         query = ''
>     i = rest.find('/')
>     if i >= 0:
>         script, rest = rest[:i], rest[i:]
>     else:
>         script, rest = rest, ''
>     . . .
> 
> 
> def run_cgi(self):
>     """Execute a CGI script."""
>     dir, rest = self.cgi_info
>     rest, _, query = rest.rpartition('?')
>     script, _, rest = rest.partition('/')

Wouldn't this do the same ?! ...

rest, query = rest.rsplit('?', maxsplit=1)
script, rest = rest.split('/', maxsplit=1)

>     . . .
> 
> 
> The new proposal does not help every use case though.  In
> ConfigParser.py, the problem description reads, "a semi-colon is a
> comment delimiter only if it follows a spacing character".  This cries
> out for a regular expression.  In StringIO.py, since the task at hand IS
> calculating an index, an indexless higher level construct doesn't help.
> However, many of the other s.find() use cases in the library simplify as
> readily and directly as the above cgi server example.
> 
> 
> 
> Raymond
> 
> 
> -------------------------------------------------------
> 
> P.S.  FWIW, if you want to experiment with it, here a concrete
> implementation of partition() expressed as a function:
> 
> def partition(s, t):
>     """ Returns a three element tuple, (head, sep, tail) where:
> 
>         head + sep + tail == s
>         t not in head
>         sep == '' or sep is t
>         bool(sep) == (t in s)       # sep indicates if the string was
> found
> 
>     >>> s = 'http://www.python.org'
>     >>> partition(s, '://')
>     ('http', '://', 'www.python.org')
>     >>> partition(s, '?')
>     ('http://www.python.org', '', '')
>     >>> partition(s, 'http://')
>     ('', 'http://', 'www.python.org')
>     >>> partition(s, 'org')
>     ('http://www.python.', 'org', '')
> 
>     """
>     if not isinstance(t, basestring) or not t:
>         raise ValueError('partititon argument must be a non-empty
> string')
>     parts = s.split(t, 1)
>     if len(parts) == 1:
>         result = (s, '', '')
>     else:
>         result = (parts[0], t, parts[1])
>     assert len(result) == 3
>     assert ''.join(result) == s
>     assert result[1] == '' or result[1] is t
>     assert t not in result[0]
>     return result
> 
> 
> import doctest
> print doctest.testmod()

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Aug 28 2005)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the Python-Dev mailing list