From 7babd30cf7804c1a95c567d43056e707f831f0ab Mon Sep 17 00:00:00 2001 From: Nicolas Grilly Date: Thu, 23 May 2013 18:33:29 +0200 Subject: [PATCH 1/2] Idiomatic Python: use isinstance instead of type --- lib/ansible/inventory/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 2cdf6b974a..821145871a 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -62,12 +62,12 @@ class Inventory(object): self._also_restriction = None self._subset = None - if type(host_list) in [ str, unicode ]: + if isinstance(host_list, basestring): if host_list.find(",") != -1: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] - if type(host_list) == list: + if isinstance(host_list, list): self.parser = None all = Group('all') self.groups = [ all ] @@ -316,7 +316,7 @@ class Inventory(object): to exclude failed hosts in main playbook code, don't use this for other reasons. """ - if type(restriction) != list: + if not isinstance(restriction, list): restriction = [ restriction ] self._restriction = restriction @@ -325,7 +325,7 @@ class Inventory(object): Works like restict_to but offers an additional restriction. Playbooks use this to implement serial behavior. """ - if type(restriction) != list: + if not isinstance(restriction, list): restriction = [ restriction ] self._also_restriction = restriction From 7aee588918b4fb17de681bc31eca177304c3e978 Mon Sep 17 00:00:00 2001 From: Nicolas Grilly Date: Thu, 23 May 2013 18:37:30 +0200 Subject: [PATCH 2/2] Idiomatic Python: use in operator instead of method find --- lib/ansible/inventory/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 821145871a..3d407a92c0 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -63,7 +63,7 @@ class Inventory(object): self._subset = None if isinstance(host_list, basestring): - if host_list.find(",") != -1: + if "," in host_list: host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] @@ -72,8 +72,8 @@ class Inventory(object): all = Group('all') self.groups = [ all ] for x in host_list: - if x.find(":") != -1: - tokens = x.split(":",1) + if ":" in x: + tokens = x.split(":", 1) all.add_host(Host(tokens[0], tokens[1])) else: all.add_host(Host(x))