py3: Replace map/filter with a list comprehension
The map and filter functions returns a list in python 2 but an iterator in python 3. However, there are instances where the code assumes the returned value from map/value to be a list. Substitute these functions to its list comprehension equivalent expression to enable compatibility with Python 3. Change-Id: Iccaa3d1f4b009f59ea575c955ee4015e964f2318
This commit is contained in:
parent
1c984e4034
commit
935f2cca8e
@ -181,7 +181,7 @@ class AggregatesAdminTestJSON(base.BaseV2ComputeAdminTest):
|
||||
host=self.host)
|
||||
|
||||
aggregates = self.client.list_aggregates()['aggregates']
|
||||
aggs = filter(lambda x: x['id'] == aggregate['id'], aggregates)
|
||||
aggs = [agg for agg in aggregates if agg['id'] == aggregate['id']]
|
||||
self.assertEqual(1, len(aggs))
|
||||
agg = aggs[0]
|
||||
self.assertEqual(aggregate_name, agg['name'])
|
||||
|
@ -36,8 +36,8 @@ class AggregatesAdminNegativeTestJSON(base.BaseV2ComputeAdminTest):
|
||||
cls.az_name_prefix = 'test_az'
|
||||
|
||||
hosts_all = cls.os_adm.hosts_client.list_hosts()['hosts']
|
||||
hosts = map(lambda x: x['host_name'],
|
||||
filter(lambda y: y['service'] == 'compute', hosts_all))
|
||||
hosts = ([host['host_name']
|
||||
for host in hosts_all if host['service'] == 'compute'])
|
||||
cls.host = hosts[0]
|
||||
|
||||
@test.attr(type=['negative'])
|
||||
|
@ -65,7 +65,7 @@ class SecurityGroupsTestAdminJSON(base.BaseV2ComputeAdminTest):
|
||||
# Fetch all security groups based on 'all_tenants' search filter
|
||||
fetched_list = self.adm_client.list_security_groups(
|
||||
all_tenants='true')['security_groups']
|
||||
sec_group_id_list = map(lambda sg: sg['id'], fetched_list)
|
||||
sec_group_id_list = [sg['id'] for sg in fetched_list]
|
||||
# Now check if all created Security Groups are present in fetched list
|
||||
for sec_group in security_group_list:
|
||||
self.assertIn(sec_group['id'], sec_group_id_list)
|
||||
|
@ -77,7 +77,7 @@ class ServersAdminTestJSON(base.BaseV2ComputeAdminTest):
|
||||
params = {'all_tenants': ''}
|
||||
body = self.client.list_servers(detail=True, **params)
|
||||
servers = body['servers']
|
||||
servers_name = map(lambda x: x['name'], servers)
|
||||
servers_name = [server['name'] for server in servers]
|
||||
|
||||
self.assertIn(self.s1_name, servers_name)
|
||||
self.assertIn(self.s2_name, servers_name)
|
||||
|
@ -93,7 +93,7 @@ class ServicesTestJSON(base.BaseIdentityV2AdminTest):
|
||||
name=name, type=s_type,
|
||||
description=description)['OS-KSADM:service']
|
||||
services.append(service)
|
||||
service_ids = map(lambda x: x['id'], services)
|
||||
service_ids = [svc['id'] for svc in services]
|
||||
|
||||
def delete_services():
|
||||
for service_id in service_ids:
|
||||
|
@ -35,7 +35,7 @@ class TenantsTestJSON(base.BaseIdentityV2AdminTest):
|
||||
self.addCleanup(test_utils.call_and_ignore_notfound_exc,
|
||||
self.tenants_client.delete_tenant, tenant['id'])
|
||||
tenants.append(tenant)
|
||||
tenant_ids = map(lambda x: x['id'], tenants)
|
||||
tenant_ids = [tn['id'] for tn in tenants]
|
||||
body = self.tenants_client.list_tenants()['tenants']
|
||||
found = [t for t in body if t['id'] in tenant_ids]
|
||||
self.assertEqual(len(found), len(tenants), 'Tenants not created')
|
||||
|
@ -25,7 +25,7 @@ class ImageMembersTest(base.BaseV1ImageMembersTest):
|
||||
self.image_member_client.create_image_member(image, self.alt_tenant_id)
|
||||
body = self.image_member_client.list_image_members(image)
|
||||
members = body['members']
|
||||
members = map(lambda x: x['member_id'], members)
|
||||
members = [member['member_id'] for member in members]
|
||||
self.assertIn(self.alt_tenant_id, members)
|
||||
# get image as alt user
|
||||
self.alt_img_cli.show_image(image)
|
||||
@ -40,7 +40,7 @@ class ImageMembersTest(base.BaseV1ImageMembersTest):
|
||||
body = self.image_member_client.list_shared_images(
|
||||
self.alt_tenant_id)
|
||||
images = body['shared_images']
|
||||
images = map(lambda x: x['image_id'], images)
|
||||
images = [img['image_id'] for img in images]
|
||||
self.assertIn(share_image, images)
|
||||
self.assertIn(image, images)
|
||||
|
||||
|
@ -212,7 +212,7 @@ class ListImagesTest(base.BaseV1ImageTest):
|
||||
def test_index_no_params(self):
|
||||
# Simple test to see all fixture images returned
|
||||
images_list = self.client.list_images()['images']
|
||||
image_list = map(lambda x: x['id'], images_list)
|
||||
image_list = [image['id'] for image in images_list]
|
||||
for image_id in self.created_images:
|
||||
self.assertIn(image_id, image_list)
|
||||
|
||||
|
@ -185,7 +185,7 @@ class ListImagesTest(base.BaseV2ImageTest):
|
||||
def test_list_no_params(self):
|
||||
# Simple test to see all fixture images returned
|
||||
images_list = self.client.list_images()['images']
|
||||
image_list = map(lambda x: x['id'], images_list)
|
||||
image_list = [image['id'] for image in images_list]
|
||||
|
||||
for image in self.created_images:
|
||||
self.assertIn(image, image_list)
|
||||
|
@ -125,7 +125,7 @@ class StacksTestJSON(base.BaseOrchestrationTest):
|
||||
'resource_status_reason',
|
||||
'resource_status', 'event_time')
|
||||
|
||||
resource_statuses = map(lambda event: event['resource_status'], events)
|
||||
resource_statuses = [event['resource_status'] for event in events]
|
||||
self.assertIn('CREATE_IN_PROGRESS', resource_statuses)
|
||||
self.assertIn('CREATE_COMPLETE', resource_statuses)
|
||||
|
||||
|
@ -53,7 +53,7 @@ class VolumesListAdminV2TestJSON(base.BaseVolumeAdminTest):
|
||||
self.assertEqual(sorted(expected_list_ids), sorted(fetched_list_ids))
|
||||
# Verifying tenant id of volumes fetched list is related to
|
||||
# primary tenant
|
||||
fetched_tenant_id = map(operator.itemgetter(
|
||||
'os-vol-tenant-attr:tenant_id'), fetched_list)
|
||||
fetched_tenant_id = [operator.itemgetter(
|
||||
'os-vol-tenant-attr:tenant_id')(item) for item in fetched_list]
|
||||
expected_tenant_id = [self.volumes_client.tenant_id] * 3
|
||||
self.assertEqual(expected_tenant_id, fetched_tenant_id)
|
||||
|
@ -33,8 +33,9 @@ class VolumesV2ListTestJSON(base.BaseVolumeTest):
|
||||
|
||||
def assertVolumesIn(self, fetched_list, expected_list, fields=None):
|
||||
if fields:
|
||||
expected_list = map(operator.itemgetter(*fields), expected_list)
|
||||
fetched_list = map(operator.itemgetter(*fields), fetched_list)
|
||||
fieldsgetter = operator.itemgetter(*fields)
|
||||
expected_list = map(fieldsgetter, expected_list)
|
||||
fetched_list = [fieldsgetter(item) for item in fetched_list]
|
||||
|
||||
missing_vols = [v for v in expected_list if v not in fetched_list]
|
||||
if len(missing_vols) == 0:
|
||||
|
Loading…
Reference in New Issue
Block a user