From 0c0f0143e1d10081357fbad03f98fb2ecdfe8abe Mon Sep 17 00:00:00 2001 From: Masayuki Igawa Date: Mon, 10 Apr 2017 17:22:02 +0900 Subject: [PATCH] Use sequence directly instead of using len() This commit makes to use sequence directly instead of using len(SEQUENCE). The original code works correctly, and it's really straight forward. However, PEP8 recommends like below[1]. And it makes code more simple, too. ``` For sequences, (strings, lists, tuples), use the fact that empty sequences are false. Yes: if not seq: if seq: No: if len(seq): if not len(seq): ``` [1] https://www.python.org/dev/peps/pep-0008/#programming-recommendations Change-Id: I8d41e16d82b1b3860a98e5217cb7a541fc83b907 --- tempest/api/compute/admin/test_hypervisor.py | 2 +- tempest/api/compute/servers/test_novnc.py | 4 ++-- tempest/api/compute/test_extensions.py | 2 +- tempest/api/identity/base.py | 6 +++--- tempest/api/network/base.py | 2 +- tempest/api/volume/test_extensions.py | 2 +- tempest/api/volume/test_volumes_list.py | 2 +- tempest/cmd/cleanup_service.py | 2 +- tempest/common/compute.py | 4 ++-- tempest/common/preprov_creds.py | 2 +- tempest/lib/auth.py | 10 +++++----- tempest/lib/cli/output_parser.py | 2 +- tempest/lib/exceptions.py | 2 +- tempest/lib/services/image/v1/images_client.py | 2 +- tempest/test.py | 8 ++++---- 15 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tempest/api/compute/admin/test_hypervisor.py b/tempest/api/compute/admin/test_hypervisor.py index 4637024ec6..e0e5a29e16 100644 --- a/tempest/api/compute/admin/test_hypervisor.py +++ b/tempest/api/compute/admin/test_hypervisor.py @@ -104,7 +104,7 @@ class HypervisorAdminTestJSON(base.BaseV2ComputeAdminTest): try: uptime = (self.client.show_hypervisor_uptime(hyper['id']) ['hypervisor']) - if len(uptime) > 0: + if uptime: has_valid_uptime = True break except Exception: diff --git a/tempest/api/compute/servers/test_novnc.py b/tempest/api/compute/servers/test_novnc.py index 6354c576b1..90b0665ca5 100644 --- a/tempest/api/compute/servers/test_novnc.py +++ b/tempest/api/compute/servers/test_novnc.py @@ -82,7 +82,7 @@ class NoVNCConsoleTestJSON(base.BaseV2ComputeTest): """Verify we can connect to novnc and do the websocket connection.""" # Turn the Socket into a WebSocket to do the communication data = self._websocket.receive_frame() - self.assertFalse(data is None or len(data) == 0, + self.assertFalse(data is None or not data, 'Token must be invalid because the connection ' 'closed.') # Parse the RFB version from the data to make sure it is valid @@ -181,6 +181,6 @@ class NoVNCConsoleTestJSON(base.BaseV2ComputeTest): self._websocket = compute.create_websocket(url) # Make sure the novncproxy rejected the connection and closed it data = self._websocket.receive_frame() - self.assertTrue(data is None or len(data) == 0, + self.assertTrue(data is None or not data, "The novnc proxy actually sent us some data, but we " "expected it to close the connection.") diff --git a/tempest/api/compute/test_extensions.py b/tempest/api/compute/test_extensions.py index f87bf6d304..3b4177556d 100644 --- a/tempest/api/compute/test_extensions.py +++ b/tempest/api/compute/test_extensions.py @@ -31,7 +31,7 @@ class ExtensionsTestJSON(base.BaseV2ComputeTest): @decorators.idempotent_id('3bb27738-b759-4e0d-a5fa-37d7a6df07d1') def test_list_extensions(self): # List of all extensions - if len(CONF.compute_feature_enabled.api_extensions) == 0: + if not CONF.compute_feature_enabled.api_extensions: raise self.skipException('There are not any extensions configured') extensions = self.extensions_client.list_extensions()['extensions'] ext = CONF.compute_feature_enabled.api_extensions[0] diff --git a/tempest/api/identity/base.py b/tempest/api/identity/base.py index 9d794b563a..06cc1202cc 100644 --- a/tempest/api/identity/base.py +++ b/tempest/api/identity/base.py @@ -47,7 +47,7 @@ class BaseIdentityTest(tempest.test.BaseTestCase): else: users = cls.users_client.list_users()['users'] user = [u for u in users if u['name'] == name] - if len(user) > 0: + if user: return user[0] @classmethod @@ -57,14 +57,14 @@ class BaseIdentityTest(tempest.test.BaseTestCase): except AttributeError: tenants = cls.projects_client.list_projects()['projects'] tenant = [t for t in tenants if t['name'] == name] - if len(tenant) > 0: + if tenant: return tenant[0] @classmethod def get_role_by_name(cls, name): roles = cls.roles_client.list_roles()['roles'] role = [r for r in roles if r['name'] == name] - if len(role) > 0: + if role: return role[0] def create_test_user(self, **kwargs): diff --git a/tempest/api/network/base.py b/tempest/api/network/base.py index a724dc9f9c..7ceeb50f7e 100644 --- a/tempest/api/network/base.py +++ b/tempest/api/network/base.py @@ -106,7 +106,7 @@ class BaseNetworkTest(tempest.test.BaseTestCase): # Clean up metering label rules # Not all classes in the hierarchy have the client class variable - if len(cls.metering_label_rules) > 0: + if cls.metering_label_rules: label_rules_client = cls.admin_metering_label_rules_client for metering_label_rule in cls.metering_label_rules: test_utils.call_and_ignore_notfound_exc( diff --git a/tempest/api/volume/test_extensions.py b/tempest/api/volume/test_extensions.py index 91bfa338c2..39ce00c491 100644 --- a/tempest/api/volume/test_extensions.py +++ b/tempest/api/volume/test_extensions.py @@ -32,7 +32,7 @@ class ExtensionsTestJSON(base.BaseVolumeTest): # List of all extensions extensions = (self.volumes_extension_client.list_extensions() ['extensions']) - if len(CONF.volume_feature_enabled.api_extensions) == 0: + if not CONF.volume_feature_enabled.api_extensions: raise self.skipException('There are not any extensions configured') extension_list = [extension.get('alias') for extension in extensions] LOG.debug("Cinder extensions: %s", ','.join(extension_list)) diff --git a/tempest/api/volume/test_volumes_list.py b/tempest/api/volume/test_volumes_list.py index 6b9f131e61..ef96b6ed7d 100644 --- a/tempest/api/volume/test_volumes_list.py +++ b/tempest/api/volume/test_volumes_list.py @@ -47,7 +47,7 @@ class VolumesListTestJSON(base.BaseVolumeTest): 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: + if not missing_vols: return def str_vol(vol): diff --git a/tempest/cmd/cleanup_service.py b/tempest/cmd/cleanup_service.py index a632726e64..f1c0a3ebad 100644 --- a/tempest/cmd/cleanup_service.py +++ b/tempest/cmd/cleanup_service.py @@ -105,7 +105,7 @@ class BaseService(object): def _filter_by_tenant_id(self, item_list): if (item_list is None - or len(item_list) == 0 + or not item_list or not hasattr(self, 'tenant_id') or self.tenant_id is None or 'tenant_id' not in item_list[0]): diff --git a/tempest/common/compute.py b/tempest/common/compute.py index 38daffec52..9587ffe50b 100644 --- a/tempest/common/compute.py +++ b/tempest/common/compute.py @@ -258,7 +258,7 @@ class _WebSocket(object): while True: header = self._socket.recv(2) # If we didn't receive any data, just return None - if len(header) == 0: + if not header: return None # We will make the assumption that we are only dealing with # frames less than 125 bytes here (for the negotiation) and @@ -313,6 +313,6 @@ class _WebSocket(object): self._socket.sendall(reqdata.encode('utf8')) self.response = data = self._socket.recv(4096) # Loop through & concatenate all of the data in the response body - while len(data) > 0 and self.response.find(b'\r\n\r\n') < 0: + while data and self.response.find(b'\r\n\r\n') < 0: data = self._socket.recv(4096) self.response += data diff --git a/tempest/common/preprov_creds.py b/tempest/common/preprov_creds.py index a92d16a0a6..8053cacf30 100644 --- a/tempest/common/preprov_creds.py +++ b/tempest/common/preprov_creds.py @@ -241,7 +241,7 @@ class PreProvisionedCredentialProvider(cred_provider.CredentialProvider): def _get_creds(self, roles=None): useable_hashes = self._get_match_hash_list(roles) - if len(useable_hashes) == 0: + if not useable_hashes: msg = 'No users configured for type/roles %s' % roles raise lib_exc.InvalidCredentials(msg) free_hash = self._get_free_hash(useable_hashes) diff --git a/tempest/lib/auth.py b/tempest/lib/auth.py index 83aa405f3f..ab4308fedd 100644 --- a/tempest/lib/auth.py +++ b/tempest/lib/auth.py @@ -539,18 +539,18 @@ class KeystoneV3AuthProvider(KeystoneAuthProvider): # Select entries with matching service type service_catalog = [ep for ep in catalog if ep['type'] == service] - if len(service_catalog) > 0: + if service_catalog: if name is not None: service_catalog = ( [ep for ep in service_catalog if ep['name'] == name]) - if len(service_catalog) > 0: + if service_catalog: service_catalog = service_catalog[0]['endpoints'] else: raise exceptions.EndpointNotFound(name) else: service_catalog = service_catalog[0]['endpoints'] else: - if len(catalog) == 0 and service == 'identity': + if not catalog and service == 'identity': # NOTE(andreaf) If there's no catalog at all and the service # is identity, it's a valid use case. Having a non-empty # catalog with no identity in it is not valid instead. @@ -571,13 +571,13 @@ class KeystoneV3AuthProvider(KeystoneAuthProvider): # Filter by endpoint type (interface) filtered_catalog = [ep for ep in service_catalog if ep['interface'] == endpoint_type] - if len(filtered_catalog) == 0: + if not filtered_catalog: # No matching type, keep all and try matching by region at least filtered_catalog = service_catalog # Filter by region filtered_catalog = [ep for ep in filtered_catalog if ep['region'] == region] - if len(filtered_catalog) == 0: + if not filtered_catalog: # No matching region (or name), take the first endpoint filtered_catalog = [service_catalog[0]] # There should be only one match. If not take the first. diff --git a/tempest/lib/cli/output_parser.py b/tempest/lib/cli/output_parser.py index 716f374804..2edd5c16f4 100644 --- a/tempest/lib/cli/output_parser.py +++ b/tempest/lib/cli/output_parser.py @@ -114,7 +114,7 @@ def tables(output_lines): label = line else: LOG.warning('Invalid line between tables: %s', line) - if len(table_) > 0: + if table_: LOG.warning('Missing end of table') return tables_ diff --git a/tempest/lib/exceptions.py b/tempest/lib/exceptions.py index dea3289699..68ce57a6ef 100644 --- a/tempest/lib/exceptions.py +++ b/tempest/lib/exceptions.py @@ -32,7 +32,7 @@ class TempestException(Exception): except Exception: # at least get the core message out if something happened self._error_string = self.message - if len(args) > 0: + if args: # If there is a non-kwarg parameter, assume it's the error # message or reason description and tack it on to the end # of the exception message diff --git a/tempest/lib/services/image/v1/images_client.py b/tempest/lib/services/image/v1/images_client.py index faafb4a5e7..42f8cf212f 100644 --- a/tempest/lib/services/image/v1/images_client.py +++ b/tempest/lib/services/image/v1/images_client.py @@ -118,7 +118,7 @@ class ImagesClient(rest_client.RestClient): if 'changes_since' in kwargs: kwargs['changes-since'] = kwargs.pop('changes_since') - if len(kwargs) > 0: + if kwargs: url += '?%s' % urllib.urlencode(kwargs) resp, body = self.get(url) diff --git a/tempest/test.py b/tempest/test.py index 052033d410..ca41e08ea1 100644 --- a/tempest/test.py +++ b/tempest/test.py @@ -139,7 +139,7 @@ def is_extension_enabled(extension_name, service): 'object': CONF.object_storage_feature_enabled.discoverable_apis, 'identity': CONF.identity_feature_enabled.api_extensions } - if len(config_dict[service]) == 0: + if not config_dict[service]: return False if config_dict[service][0] == 'all': return True @@ -160,7 +160,7 @@ def is_scheduler_filter_enabled(filter_name): """ filters = CONF.compute_feature_enabled.scheduler_available_filters - if len(filters) == 0: + if not filters: return False if 'all' in filters: return True @@ -634,7 +634,7 @@ class BaseTestCase(testtools.testcase.WithAttributes, """ if msg is None: msg = "sequence or collection is not empty: %s" % items - self.assertEqual(0, len(items), msg) + self.assertFalse(items, msg) def assertNotEmpty(self, items, msg=None): """Asserts whether a sequence or collection is not empty @@ -645,4 +645,4 @@ class BaseTestCase(testtools.testcase.WithAttributes, """ if msg is None: msg = "sequence or collection is empty." - self.assertGreater(len(items), 0, msg) + self.assertTrue(items, msg)