From d218e6f1079359cd4fc296636e3a2f23b9721fba Mon Sep 17 00:00:00 2001 From: Viktor Varga Date: Thu, 3 Aug 2017 17:39:20 +0200 Subject: [PATCH] Replace map/filter lambda with comprehensions List comprehensions and generator expressions are considered to be more Pythonic (and usually more readable) than map and filter with lambda. This patch replaces four usages of [map|filter](lambda ...) with the appropriate list comprehension or generator expression. TrivialFix Change-Id: Ifda9030bb8aa196cb7a5977a57ef46dfefd70fa6 --- watcher/api/middleware/auth_token.py | 4 ++-- watcher/common/cinder_helper.py | 17 +++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/watcher/api/middleware/auth_token.py b/watcher/api/middleware/auth_token.py index 585d4958e..b426c04a8 100644 --- a/watcher/api/middleware/auth_token.py +++ b/watcher/api/middleware/auth_token.py @@ -52,8 +52,8 @@ class AuthTokenMiddleware(auth_token.AuthProtocol): # The information whether the API call is being performed against the # public API is required for some other components. Saving it to the # WSGI environment is reasonable thereby. - env['is_public_api'] = any(map(lambda pattern: re.match(pattern, path), - self.public_api_routes)) + env['is_public_api'] = any(re.match(pattern, path) + for pattern in self.public_api_routes) if env['is_public_api']: return self._app(env, start_response) diff --git a/watcher/common/cinder_helper.py b/watcher/common/cinder_helper.py index 280e84c40..0ee1ad3c3 100644 --- a/watcher/common/cinder_helper.py +++ b/watcher/common/cinder_helper.py @@ -40,9 +40,8 @@ class CinderHelper(object): def get_storage_node_by_name(self, name): """Get storage node by name(host@backendname)""" try: - storages = list(filter(lambda storage: - storage.host == name, - self.get_storage_node_list())) + storages = [storage for storage in self.get_storage_node_list() + if storage.host == name] if len(storages) != 1: raise exception.StorageNodeNotFound(name=name) return storages[0] @@ -56,9 +55,8 @@ class CinderHelper(object): def get_storage_pool_by_name(self, name): """Get pool by name(host@backend#poolname)""" try: - pools = list(filter(lambda pool: - pool.name == name, - self.get_storage_pool_list())) + pools = [pool for pool in self.get_storage_pool_list() + if pool.name == name] if len(pools) != 1: raise exception.PoolNotFound(name=name) return pools[0] @@ -75,10 +73,9 @@ class CinderHelper(object): def get_volume_type_by_backendname(self, backendname): volume_type_list = self.get_volume_type_list() - volume_type = list(filter( - lambda volume_type: - volume_type.extra_specs.get( - 'volume_backend_name') == backendname, volume_type_list)) + volume_type = [volume_type for volume_type in volume_type_list + if volume_type.extra_specs.get( + 'volume_backend_name') == backendname] if volume_type: return volume_type[0].name else: