Micro-optimization: replace s.find(x)!=-1 with x in s

timeit shows a speedup of ~3x on Python 2.7.5 x86_64.
It also makes the code a bit shorter.
reviewable/pr18780/r1
Cristian Ciupitu 11 years ago committed by Michael DeHaan
parent c3e8a51374
commit 7f681c33a2

@ -120,7 +120,7 @@ class LibvirtConnection(object):
cmd = "uname -r"
rc, stdout, stderr = self.module.run_command(cmd)
if stdout.find("xen") != -1:
if "xen" in stdout:
conn = libvirt.open(None)
else:
conn = libvirt.open(uri)

@ -180,7 +180,7 @@ class CommandModule(AnsibleModule):
params['removes'] = None
params['shell'] = False
params['executable'] = None
if args.find("#USE_SHELL") != -1:
if "#USE_SHELL" in args:
args = args.replace("#USE_SHELL", "")
params['shell'] = True

@ -259,7 +259,7 @@ def privileges_unpack(priv):
output = {}
for item in priv.split('/'):
pieces = item.split(':')
if pieces[0].find('.') != -1:
if '.' in pieces[0]:
pieces[0] = pieces[0].split('.')
for idx, piece in enumerate(pieces):
if pieces[0][idx] != "*":

@ -168,7 +168,7 @@ def main():
f = open(path)
b = f.read(8192)
f.close()
if b.find("\x00") != -1:
if "\x00" in b:
appears_binary = True
except:
pass

@ -138,7 +138,7 @@ def iscsi_get_cached_nodes(module, portal=None):
# older versions of scsiadm don't have nice return codes
# for newer versions see iscsiadm(8); also usr/iscsiadm.c for details
# err can contain [N|n]o records...
elif rc == 21 or (rc == 255 and err.find("o records found") != -1):
elif rc == 21 or (rc == 255 and "o records found" in err):
nodes = []
else:
module.fail_json(cmd=cmd, rc=rc, msg=err)

@ -483,9 +483,9 @@ class LinuxService(Service):
if self.svc_initctl and self.running is None:
# check the job status by upstart response
initctl_rc, initctl_status_stdout, initctl_status_stderr = self.execute_command("%s status %s" % (self.svc_initctl, self.name))
if initctl_status_stdout.find("stop/waiting") != -1:
if "stop/waiting" in initctl_status_stdout:
self.running = False
elif initctl_status_stdout.find("start/running") != -1:
elif "start/running" in initctl_status_stdout:
self.running = True
if self.svc_cmd and self.svc_cmd.endswith("rc-service") and self.running is None:
@ -525,7 +525,7 @@ class LinuxService(Service):
# if the job status is still not known check it by special conditions
if self.running is None:
if self.name == 'iptables' and status_stdout.find("ACCEPT") != -1:
if self.name == 'iptables' and "ACCEPT" in status_stdout:
# iptables status command output is lame
# TODO: lookup if we can use a return code for this instead?
self.running = True
@ -631,16 +631,16 @@ class LinuxService(Service):
if line.startswith('rename'):
self.changed = True
break
elif self.enable and line.find('do not exist') != -1:
elif self.enable and 'do not exist' in line:
self.changed = True
break
elif not self.enable and line.find('already exist') != -1:
elif not self.enable and 'already exist' in line:
self.changed = True
break
# Debian compatibility
for line in err.splitlines():
if self.enable and line.find('no runlevel symlinks to modify') != -1:
if self.enable and 'no runlevel symlinks to modify' in line:
self.changed = True
break
@ -982,9 +982,9 @@ class SunOSService(Service):
# enabled false
for line in stdout.split("\n"):
if line.find("enabled") == 0:
if line.find("true") != -1:
if "true" in line:
enabled = True
if line.find("temporary") != -1:
if "temporary" in line:
temporary = True
startup_enabled = (enabled and not temporary) or (not enabled and temporary)
@ -1176,7 +1176,7 @@ def main():
(rc, out, err) = service.modify_service_state()
if rc != 0:
if err and err.find("is already") != -1:
if err and "is already" in err:
# upstart got confused, one such possibility is MySQL on Ubuntu 12.04
# where status may report it has no start/stop links and we could
# not get accurate status

Loading…
Cancel
Save