From 28e7e5b74a4b952e500a6bacec234f235a048c89 Mon Sep 17 00:00:00 2001 From: Bharath Vignesh J K <52282402+RazCrimson@users.noreply.github.com> Date: Fri, 17 May 2024 03:28:11 +0530 Subject: [PATCH] chg: ruff - flake8-comprehensions --- glances/exports/glances_mqtt/__init__.py | 4 ++-- glances/plugins/containers/__init__.py | 2 +- glances/plugins/containers/engines/docker.py | 2 +- glances/plugins/containers/engines/podman.py | 2 +- glances/plugins/diskio/__init__.py | 2 +- glances/plugins/fs/__init__.py | 2 +- glances/plugins/gpu/cards/amd.py | 2 +- glances/plugins/gpu/cards/nvidia.py | 2 +- glances/plugins/network/__init__.py | 2 +- glances/plugins/plugin/model.py | 20 +++++++++----------- glances/plugins/quicklook/__init__.py | 2 +- glances/stats.py | 6 +++--- pyproject.toml | 2 +- unittest-core.py | 6 ++---- 14 files changed, 26 insertions(+), 30 deletions(-) diff --git a/glances/exports/glances_mqtt/__init__.py b/glances/exports/glances_mqtt/__init__.py index 7aeccb03..6c3eaec2 100755 --- a/glances/exports/glances_mqtt/__init__.py +++ b/glances/exports/glances_mqtt/__init__.py @@ -115,7 +115,7 @@ class Export(GlancesExport): sensor_values = dict(zip(columns, points)) # Build the value to output - output_value = dict() + output_value = {} for key in sensor_values: split_key = key.split('.') @@ -123,7 +123,7 @@ class Export(GlancesExport): current_level = output_value for depth in range(len(split_key) - 1): if split_key[depth] not in current_level: - current_level[split_key[depth]] = dict() + current_level[split_key[depth]] = {} current_level = current_level[split_key[depth]] # Add the value diff --git a/glances/plugins/containers/__init__.py b/glances/plugins/containers/__init__.py index bea37ad4..08004e52 100644 --- a/glances/plugins/containers/__init__.py +++ b/glances/plugins/containers/__init__.py @@ -299,7 +299,7 @@ class PluginModel(GlancesPluginModel): show_pod_name = True self.views['show_pod_name'] = show_pod_name show_engine_name = False - if len(set(ct["engine"] for ct in self.stats)) > 1: + if len({ct["engine"] for ct in self.stats}) > 1: show_engine_name = True self.views['show_engine_name'] = show_engine_name diff --git a/glances/plugins/containers/engines/docker.py b/glances/plugins/containers/engines/docker.py index a72c6bcb..066e83a8 100644 --- a/glances/plugins/containers/engines/docker.py +++ b/glances/plugins/containers/engines/docker.py @@ -267,7 +267,7 @@ class DockerContainersExtension: self.stats_fetchers[container.id] = DockerStatsFetcher(container) # Stop threads for non-existing containers - absent_containers = set(iterkeys(self.stats_fetchers)) - set(c.id for c in containers) + absent_containers = set(iterkeys(self.stats_fetchers)) - {c.id for c in containers} for container_id in absent_containers: # Stop the StatsFetcher logger.debug(f"{self.ext_name} plugin - Stop thread for old container {container_id[:12]}") diff --git a/glances/plugins/containers/engines/podman.py b/glances/plugins/containers/engines/podman.py index a73f400b..7324ccd6 100644 --- a/glances/plugins/containers/engines/podman.py +++ b/glances/plugins/containers/engines/podman.py @@ -278,7 +278,7 @@ class PodmanContainersExtension: self.container_stats_fetchers[container.id] = PodmanContainerStatsFetcher(container) # Stop threads for non-existing containers - absent_containers = set(iterkeys(self.container_stats_fetchers)) - set(c.id for c in containers) + absent_containers = set(iterkeys(self.container_stats_fetchers)) - {c.id for c in containers} for container_id in absent_containers: # Stop the StatsFetcher logger.debug(f"{self.ext_name} plugin - Stop thread for old container {container_id[:12]}") diff --git a/glances/plugins/diskio/__init__.py b/glances/plugins/diskio/__init__.py index 4c98c6af..05ec9be7 100644 --- a/glances/plugins/diskio/__init__.py +++ b/glances/plugins/diskio/__init__.py @@ -188,7 +188,7 @@ class PluginModel(GlancesPluginModel): # Disk list (sorted by name) for i in self.sorted_stats(): # Hide stats if never be different from 0 (issue #1787) - if all([self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields]): + if all(self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields): continue # Is there an alias for the disk name ? disk_name = i['alias'] if 'alias' in i else i['disk_name'] diff --git a/glances/plugins/fs/__init__.py b/glances/plugins/fs/__init__.py index 74df193c..6b084e6e 100644 --- a/glances/plugins/fs/__init__.py +++ b/glances/plugins/fs/__init__.py @@ -139,7 +139,7 @@ class PluginModel(GlancesPluginModel): logger.debug("Plugin - fs: PsUtil extended fetch failed") else: # Discard duplicates (#2299) and add entries matching allowed fs types - tracked_mnt_points = set(f.mountpoint for f in fs_stat) + tracked_mnt_points = {f.mountpoint for f in fs_stat} for f in all_mounted_fs: if ( any(f.fstype.find(fs_type) >= 0 for fs_type in allowed_fs_types) diff --git a/glances/plugins/gpu/cards/amd.py b/glances/plugins/gpu/cards/amd.py index eb4ead3a..b3a84e04 100644 --- a/glances/plugins/gpu/cards/amd.py +++ b/glances/plugins/gpu/cards/amd.py @@ -64,7 +64,7 @@ class AmdGPU: stats = [] for index, device in enumerate(self.device_folders): - device_stats = dict() + device_stats = {} # Dictionary key is the GPU_ID device_stats['key'] = 'gpu_id' # GPU id (for multiple GPU, start at 0) diff --git a/glances/plugins/gpu/cards/nvidia.py b/glances/plugins/gpu/cards/nvidia.py index 2c9fe54a..dd6216de 100644 --- a/glances/plugins/gpu/cards/nvidia.py +++ b/glances/plugins/gpu/cards/nvidia.py @@ -49,7 +49,7 @@ class NvidiaGPU: stats = [] for index, device_handle in enumerate(self.device_handles): - device_stats = dict() + device_stats = {} # Dictionary key is the GPU_ID device_stats['key'] = 'gpu_id' # GPU id (for multiple GPU, start at 0) diff --git a/glances/plugins/network/__init__.py b/glances/plugins/network/__init__.py index da582db6..396a1d4f 100644 --- a/glances/plugins/network/__init__.py +++ b/glances/plugins/network/__init__.py @@ -259,7 +259,7 @@ class PluginModel(GlancesPluginModel): if ('is_up' in i) and (i['is_up'] is False): continue # Hide stats if never be different from 0 (issue #1787) - if all([self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields]): + if all(self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields): continue # Format stats # Is there an alias for the interface name ? diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index 502122ca..e7a2dc96 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -93,7 +93,7 @@ class GlancesPluginModel: self.stats_history = self.init_stats_history() # Init the limits (configuration keys) dictionary - self._limits = dict() + self._limits = {} if config is not None: logger.debug(f'Load section {self.plugin_name} in {config.config_file_paths()}') self.load_limits(config=config) @@ -105,7 +105,7 @@ class GlancesPluginModel: self.actions = GlancesActions(args=args) # Init the views - self.views = dict() + self.views = {} # Hide stats if all the hide_zero_fields has never been != 0 # Default is False, always display stats @@ -284,10 +284,8 @@ class GlancesPluginModel: return sorted( self.stats, key=lambda stat: tuple( - map( - lambda part: int(part) if part.isdigit() else part.lower(), - re.split(r"(\d+|\D+)", self.has_alias(stat[key]) or stat[key]), - ) + int(part) if part.isdigit() else part.lower() + for part in re.split(r"(\d+|\D+)", self.has_alias(stat[key]) or stat[key]) ), ) except TypeError: @@ -295,7 +293,7 @@ class GlancesPluginModel: return sorted( self.stats, key=lambda stat: tuple( - map(lambda part: part.lower(), re.split(r"(\d+|\D+)", self.has_alias(stat[key]) or stat[key])) + part.lower() for part in re.split(r"(\d+|\D+)", self.has_alias(stat[key]) or stat[key]) ), ) @@ -453,7 +451,7 @@ class GlancesPluginModel: if isinstance(self.get_raw(), list) and self.get_raw() is not None and self.get_key() is not None: # Stats are stored in a list of dict (ex: NETWORK, FS...) for i in self.get_raw(): - if any([i[f] for f in self.hide_zero_fields]): + if any(i[f] for f in self.hide_zero_fields): for f in self.hide_zero_fields: self.views[i[self.get_key()]][f]['_zero'] = self.views[i[self.get_key()]][f]['hidden'] for f in self.hide_zero_fields: @@ -465,7 +463,7 @@ class GlancesPluginModel: # # Stats are stored in a dict (ex: CPU, LOAD...) for key in listkeys(self.get_raw()): - if any([self.get_raw()[f] for f in self.hide_zero_fields]): + if any(self.get_raw()[f] for f in self.hide_zero_fields): for f in self.hide_zero_fields: self.views[f]['_zero'] = self.views[f]['hidden'] for f in self.hide_zero_fields: @@ -530,7 +528,7 @@ class GlancesPluginModel: def reset_views(self): """Reset the views to input_views.""" - self.views = dict() + self.views = {} def get_views(self, item=None, key=None, option=None): """Return the views object. @@ -888,7 +886,7 @@ class GlancesPluginModel: def read_alias(self): if self.plugin_name + '_' + 'alias' in self._limits: return {i.split(':')[0].lower(): i.split(':')[1] for i in self._limits[self.plugin_name + '_' + 'alias']} - return dict() + return {} def has_alias(self, header): """Return the alias name for the relative header it it exists otherwise None.""" diff --git a/glances/plugins/quicklook/__init__.py b/glances/plugins/quicklook/__init__.py index 0c45d9a3..161e6880 100644 --- a/glances/plugins/quicklook/__init__.py +++ b/glances/plugins/quicklook/__init__.py @@ -179,7 +179,7 @@ class PluginModel(GlancesPluginModel): return ret # Define the data: Bar (default behavior) or Sparkline - data = dict() + data = {} for key in self.stats_list: if self.args.sparkline and self.history_enable() and not self.args.client: data[key] = Sparkline(max_width) diff --git a/glances/stats.py b/glances/stats.py index 99eae439..97b9729e 100644 --- a/glances/stats.py +++ b/glances/stats.py @@ -233,7 +233,7 @@ class GlancesStats: """ if enable: return [p for p in self._plugins if self._plugins[p].is_enabled()] - return [p for p in self._plugins] + return list(self._plugins) def getExportsList(self, enable=True): """Return the exports list. @@ -244,8 +244,8 @@ class GlancesStats: :return: list of export module names """ if enable: - return [e for e in self._exports] - return [e for e in self._exports_all] + return list(self._exports) + return list(self._exports_all) def load_limits(self, config=None): """Load the stats limits (except the one in the exclude list).""" diff --git a/pyproject.toml b/pyproject.toml index 7a1f3608..685b228f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ select = [ # "N", # pep8-naming "W", # pycodestyle "UP", # pyupgrde -# "C4", # flake8-comprehensions + "C4", # flake8-comprehensions "RET", # flake8-return # "FBT", # flake8-boolean-trap # "RUF", # Ruff-specific rules diff --git a/unittest-core.py b/unittest-core.py index 6626d6a4..bad2dcf9 100755 --- a/unittest-core.py +++ b/unittest-core.py @@ -74,10 +74,8 @@ class TestGlances(unittest.TestCase): self.assertEqual(plugin_instance.get_key(), None) self.assertTrue( all( - [ - f in [h['name'] for h in plugin_instance.items_history_list] - for f in plugin_instance.get_raw_history() - ] + f in [h['name'] for h in plugin_instance.items_history_list] + for f in plugin_instance.get_raw_history() ) ) elif plugin_instance.history_enable() and isinstance(plugin_instance.get_raw(), list):