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.
This commit is contained in:
Cristian Ciupitu
2014-01-23 17:02:17 +02:00
committed by Michael DeHaan
parent a7da5d8702
commit 0749112286
19 changed files with 50 additions and 48 deletions

View File

@@ -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)

View File

@@ -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