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
This commit is contained in:
Dag Wieers
2016-11-17 15:08:12 +01:00
committed by Brian Coca
parent b79bf14607
commit 1ca4add91c
7 changed files with 15 additions and 15 deletions

View File

@@ -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'] = []

View File

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

View File

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