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 <[EMAIL PROTECTED]>
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe:
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com