Merge pull request #3253 from Boris-Okassa/Dictkeys

remove `.keys()` from loops over dicts
This commit is contained in:
Nicolas Hennion 2025-07-26 16:40:41 +02:00 committed by GitHub
commit 6ffcaf4a94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 9 additions and 9 deletions

View File

@ -266,7 +266,7 @@ class GlancesExport:
if isinstance(stats, dict): if isinstance(stats, dict):
# Stats is a dict # Stats is a dict
# Is there a key ? # Is there a key ?
if "key" in stats.keys() and stats["key"] in stats.keys(): if "key" in stats and stats["key"] in stats:
pre_key = "{}.".format(stats[stats["key"]]) pre_key = "{}.".format(stats[stats["key"]])
else: else:
pre_key = "" pre_key = ""

View File

@ -87,7 +87,7 @@ class GlancesCursesBrowser(_GlancesCurses):
counts[color] = counts.get(color, 0) + 1 counts[color] = counts.get(color, 0) + 1
result = '' result = ''
for key in counts.keys(): for key in counts:
result += key + ': ' + str(counts[key]) + ' ' result += key + ': ' + str(counts[key]) + ' '
return result return result

View File

@ -55,12 +55,12 @@ class GlancesStdoutCsv:
line += f'{plugin}.{attribute}{self.separator}' line += f'{plugin}.{attribute}{self.separator}'
else: else:
if isinstance(stat, dict): if isinstance(stat, dict):
for k in stat.keys(): for k in stat:
line += f'{plugin}.{str(k)}{self.separator}' line += f'{plugin}.{str(k)}{self.separator}'
elif isinstance(stat, list): elif isinstance(stat, list):
for i in stat: for i in stat:
if isinstance(i, dict) and 'key' in i: if isinstance(i, dict) and 'key' in i:
for k in i.keys(): for k in i:
line += '{}.{}.{}{}'.format(plugin, str(i[i['key']]), str(k), self.separator) line += '{}.{}.{}{}'.format(plugin, str(i[i['key']]), str(k), self.separator)
else: else:
line += f'{plugin}{self.separator}' line += f'{plugin}{self.separator}'

View File

@ -98,7 +98,7 @@ class GlancesStdoutIssue:
stat = stats.get_plugin(plugin).get_export() stat = stats.get_plugin(plugin).get_export()
# Hide private information # Hide private information
if plugin == 'ip': if plugin == 'ip':
for key in stat.keys(): for key in stat:
stat[key] = '***' stat[key] = '***'
except Exception as e: except Exception as e:
stat_error = e stat_error = e

View File

@ -156,7 +156,7 @@ class SensorsPlugin(GlancesPluginModel):
if self.input_method == 'local': if self.input_method == 'local':
with ThreadPoolExecutor(max_workers=len(self.sensors_grab_map)) as executor: with ThreadPoolExecutor(max_workers=len(self.sensors_grab_map)) as executor:
logger.debug(f"Sensors enabled sub plugins: {list(self.sensors_grab_map.keys())}") logger.debug(f"Sensors enabled sub plugins: {list(self.sensors_grab_map.keys())}")
futures = {t: executor.submit(self.__get_sensor_data, t) for t in self.sensors_grab_map.keys()} futures = {t: executor.submit(self.__get_sensor_data, t) for t in self.sensors_grab_map}
# Merge the results # Merge the results
for sensor_type, future in futures.items(): for sensor_type, future in futures.items():

View File

@ -185,10 +185,10 @@ class SmartPlugin(GlancesPluginModel):
msg = '{:{width}}'.format(device_stat['DeviceName'][:max_width], width=max_width) msg = '{:{width}}'.format(device_stat['DeviceName'][:max_width], width=max_width)
ret.append(self.curse_add_line(msg)) ret.append(self.curse_add_line(msg))
try: try:
device_stat_sorted = sorted([i for i in device_stat.keys() if i != 'DeviceName'], key=int) device_stat_sorted = sorted([i for i in device_stat if i != 'DeviceName'], key=int)
except ValueError: except ValueError:
# Catch ValueError, see #2904 # Catch ValueError, see #2904
device_stat_sorted = [i for i in device_stat.keys() if i != 'DeviceName'] device_stat_sorted = [i for i in device_stat if i != 'DeviceName']
for smart_stat in device_stat_sorted: for smart_stat in device_stat_sorted:
ret.append(self.curse_new_line()) ret.append(self.curse_new_line())
msg = ' {:{width}}'.format( msg = ' {:{width}}'.format(

View File

@ -404,7 +404,7 @@ class TestGlances(unittest.TestCase):
assert len(stats_grab) == 0 assert len(stats_grab) == 0
else: else:
print(stats_grab) print(stats_grab)
self.assertTrue(stat in stats_grab[0].keys(), msg=f'Cannot find key: {stat}') self.assertTrue(stat in stats_grab[0], msg=f'Cannot find key: {stat}')
print(f'INFO: SMART stats: {stats_grab}') print(f'INFO: SMART stats: {stats_grab}')