[Python-Dev] Remove str.find in 3.0?
Raymond Hettinger
raymond.hettinger at verizon.net
Sat Aug 27 14:56:28 CEST 2005
FWIW, here are three more comparative code fragments. They are
presented without judgment as an evaluation tool to let everyone form
their own opinion about the merits of each:
--- 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
try:
i = rest.rindex('?')
except ValueError():
query = ''
else:
rest, query = rest[:i], rest[i+1:]
try:
i = rest.index('/')
except ValueError():
script, rest = rest, ''
else:
script, rest = rest[:i], rest[i:]
. . .
--- From ConfigParser.py ---------------
optname, vi, optval = mo.group('option', 'vi', 'value')
if vi in ('=', ':') and ';' in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
pos = optval.find(';')
if pos != -1 and optval[pos-1].isspace():
optval = optval[:pos]
optval = optval.strip()
. . .
optname, vi, optval = mo.group('option', 'vi', 'value')
if vi in ('=', ':') and ';' in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
try:
pos = optval.index(';')
except ValueError():
pass
else:
if optval[pos-1].isspace():
optval = optval[:pos]
optval = optval.strip()
. . .
--- StringIO.py ---------------
i = self.buf.find('\n', self.pos)
if i < 0:
newpos = self.len
else:
newpos = i+1
. . .
try:
i = self.buf.find('\n', self.pos)
except ValueError():
newpos = self.len
else:
newpos = i+1
. . .
My notes so far weren't meant to judge the proposal. I'm just
suggesting that examining fragments like the ones above will help inform
the design process.
Peace,
Raymond
More information about the Python-Dev
mailing list