[Python-3000] Droping find/rfind?
David Hopwood
david.nospam.hopwood at blueyonder.co.uk
Thu Aug 24 22:41:43 CEST 2006
Walter Dörwald wrote:
[...]
> Using find(), the code looks like this:
>
> def splitfind(s):
> pos = 0
> while True:
> posstart = s.find("{", pos)
> if posstart < 0:
> break
> posarg = s.find(" ", posstart)
> if posarg < 0:
> break
> posend = s.find("}", posarg)
> if posend < 0:
> break
> prefix = s[pos:posstart]
> if prefix:
> yield (None, prefix)
> yield (s[posstart+1:posarg], s[posarg+1:posend])
> pos = posend+1
> rest = s[pos:]
> if rest:
> yield (None, rest)
>
> Using index() looks worse to me. The code is buried under the exception
> handling:
>
> def splitindex(s):
> pos = 0
> while True:
> try:
> posstart = s.index("{", pos)
> except ValueError:
> break
> try:
> posarg = s.index(" ", posstart)
> except ValueError:
> break
> try:
> posend = s.find("}", posarg)
> except ValueError:
> break
try:
posstart = s.index("{", pos)
posarg = s.index(" ", posstart)
posend = s.find("}", posarg)
except ValueError:
break
is shorter and clearer than the version using 'find'.
--
David Hopwood <david.nospam.hopwood at blueyonder.co.uk>
More information about the Python-3000
mailing list