From 1ca4add91c4d813c0bc054e0664fa8fbaf20aff0 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Thu, 17 Nov 2016 15:08:12 +0100 Subject: [PATCH] Performance improvement using in-operator on dicts Just a small cleanup for the existing occurrences. Using the in-operator for hash lookups is faster than using .keys() http://stackoverflow.com/questions/29314269/why-do-key-in-dict-and-key-in-dict-keys-have-the-same-output --- contrib/inventory/abiquo.py | 6 +++--- contrib/inventory/brook.py | 6 +++--- contrib/inventory/docker.py | 2 +- lib/ansible/module_utils/facts.py | 4 ++-- lib/ansible/playbook/task.py | 4 ++-- lib/ansible/plugins/callback/mail.py | 6 +++--- lib/ansible/utils/module_docs.py | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/contrib/inventory/abiquo.py b/contrib/inventory/abiquo.py index cd068e482b0..04f7d4e60df 100755 --- a/contrib/inventory/abiquo.py +++ b/contrib/inventory/abiquo.py @@ -149,15 +149,15 @@ def generate_inv_from_api(enterprise_entity,config): vm_state = False if not vm_nic == None and vm_state: - if not vm_vapp in inventory.keys(): + if vm_vapp not in inventory: inventory[vm_vapp] = {} inventory[vm_vapp]['children'] = [] inventory[vm_vapp]['hosts'] = [] - if not vm_vdc in inventory.keys(): + if vm_vdc not in inventory: inventory[vm_vdc] = {} inventory[vm_vdc]['hosts'] = [] inventory[vm_vdc]['children'] = [] - if not vm_template in inventory.keys(): + if vm_template not in inventory: inventory[vm_template] = {} inventory[vm_template]['children'] = [] inventory[vm_template]['hosts'] = [] diff --git a/contrib/inventory/brook.py b/contrib/inventory/brook.py index e0a9348a17b..a7d474148a5 100755 --- a/contrib/inventory/brook.py +++ b/contrib/inventory/brook.py @@ -180,14 +180,14 @@ class BrookInventory: # Group by project project_group = 'project_%s' % project.name - if project_group in groups.keys(): + if project_group in groups: groups[project_group].append(instance.name) else: groups[project_group] = [instance.name] # Group by status status_group = 'status_%s' % meta['hostvars'][instance.name]['brook_status'] - if status_group in groups.keys(): + if status_group in groups: groups[status_group].append(instance.name) else: groups[status_group] = [instance.name] @@ -196,7 +196,7 @@ class BrookInventory: tags = meta['hostvars'][instance.name]['brook_tags'] for tag in tags: tag_group = 'tag_%s' % tag - if tag_group in groups.keys(): + if tag_group in groups: groups[tag_group].append(instance.name) else: groups[tag_group] = [instance.name] diff --git a/contrib/inventory/docker.py b/contrib/inventory/docker.py index 6cbfb5d1cf8..3ea18314435 100755 --- a/contrib/inventory/docker.py +++ b/contrib/inventory/docker.py @@ -609,7 +609,7 @@ class DockerInventory(object): self.groups[id].append(name) self.groups[name].append(name) - if short_id not in self.groups.keys(): + if short_id not in self.groups: self.groups[short_id].append(name) self.groups[hostname].append(name) diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py index b38604d2f0b..9f2ee76d7b4 100644 --- a/lib/ansible/module_utils/facts.py +++ b/lib/ansible/module_utils/facts.py @@ -2724,7 +2724,7 @@ class GenericBsdIfconfigNetwork(Network): return [] def merge_default_interface(self, defaults, interfaces, ip_type): - if not 'interface' in defaults.keys(): + if 'interface' not in defaults: return if not defaults['interface'] in interfaces: return @@ -3045,7 +3045,7 @@ class SunOSNetwork(GenericBsdIfconfigNetwork): def parse_interface_line(self, words, current_if, interfaces): device = words[0][0:-1] - if device not in interfaces.keys(): + if device not in interfaces: current_if = {'device': device, 'ipv4': [], 'ipv6': [], 'type': 'unknown'} else: current_if = interfaces[device] diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 503c97f322f..d568911f8b7 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -268,12 +268,12 @@ class Task(Base, Conditional, Taggable, Become): else: env = [] for env_item in value: - if isinstance(env_item, (string_types, AnsibleUnicode)) and env_item in templar._available_variables.keys(): + if isinstance(env_item, (string_types, AnsibleUnicode)) and env_item in templar._available_variables: env[env_item] = templar.template(env_item, convert_bare=False) elif isinstance(value, dict): env = dict() for env_item in value: - if isinstance(env_item, (string_types, AnsibleUnicode)) and env_item in templar._available_variables.keys(): + if isinstance(env_item, (string_types, AnsibleUnicode)) and env_item in templar._available_variables: env[env_item] = templar.template(value[env_item], convert_bare=False) # at this point it should be a simple string diff --git a/lib/ansible/plugins/callback/mail.py b/lib/ansible/plugins/callback/mail.py index 65bdc5bbbec..750c88c1aaa 100644 --- a/lib/ansible/plugins/callback/mail.py +++ b/lib/ansible/plugins/callback/mail.py @@ -92,13 +92,13 @@ class CallbackModule(CallbackBase): subject = 'Failed: %s' % attach body = 'The following task failed for host ' + host + ':\n\n%s\n\n' % attach - if 'stdout' in res._result.keys() and res._result['stdout']: + if 'stdout' in res._result and res._result['stdout']: subject = res._result['stdout'].strip('\r\n').split('\n')[-1] body += 'with the following output in standard output:\n\n' + res._result['stdout'] + '\n\n' - if 'stderr' in res._result.keys() and res._result['stderr']: + if 'stderr' in res._result and res._result['stderr']: subject = res._result['stderr'].strip('\r\n').split('\n')[-1] body += 'with the following output in standard error:\n\n' + res._result['stderr'] + '\n\n' - if 'msg' in res._result.keys() and res._result['msg']: + if 'msg' in res._result and res._result['msg']: subject = res._result['msg'].strip('\r\n').split('\n')[0] body += 'with the following message:\n\n' + res._result['msg'] + '\n\n' body += 'A complete dump of the error:\n\n' + self._dump_results(res._result) diff --git a/lib/ansible/utils/module_docs.py b/lib/ansible/utils/module_docs.py index 729b81c713e..48f705cd920 100755 --- a/lib/ansible/utils/module_docs.py +++ b/lib/ansible/utils/module_docs.py @@ -104,7 +104,7 @@ def get_docstring(filename, verbose=False): doc['notes'] = [] doc['notes'].extend(notes) - if 'options' not in fragment.keys(): + if 'options' not in fragment: raise Exception("missing options in fragment, possibly misformatted?") for key, value in fragment.items():