From 7f681c33a2a4b6f7d074468a039476c95974de04 Mon Sep 17 00:00:00 2001 From: Cristian Ciupitu Date: Thu, 23 Jan 2014 17:02:17 +0200 Subject: [PATCH] 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. --- cloud/virt | 2 +- commands/command | 2 +- database/mysql_user | 2 +- files/file | 2 +- system/open_iscsi | 2 +- system/service | 18 +++++++++--------- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cloud/virt b/cloud/virt index 3400c3ff727..78d2aa1ab91 100644 --- a/cloud/virt +++ b/cloud/virt @@ -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) diff --git a/commands/command b/commands/command index ba9ae30a7f2..b35501f1bf8 100644 --- a/commands/command +++ b/commands/command @@ -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 diff --git a/database/mysql_user b/database/mysql_user index e7fad3d77c6..b7c84fd1c3e 100644 --- a/database/mysql_user +++ b/database/mysql_user @@ -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] != "*": diff --git a/files/file b/files/file index 7a038c9f362..65a3f417c03 100644 --- a/files/file +++ b/files/file @@ -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 diff --git a/system/open_iscsi b/system/open_iscsi index 2e57727cf59..3fd2b1a5a21 100644 --- a/system/open_iscsi +++ b/system/open_iscsi @@ -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) diff --git a/system/service b/system/service index 5180a14d82b..ed30b72aa5b 100644 --- a/system/service +++ b/system/service @@ -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