From 0ffb08dd2a655381f9566d5e73b438fba94550a0 Mon Sep 17 00:00:00 2001 From: Ariel Otilibili Date: Mon, 2 Jun 2025 14:00:11 +0200 Subject: [PATCH] glances: Refactor _build_amps_list Used a list comprehension. Moreover None and empty list are considered as false values in Python. Part-of: #2801 Link: https://docs.python.org/3/library/stdtypes.html#truth-value-testing Signed-off-by: Ariel Otilibili --- glances/amps_list.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/glances/amps_list.py b/glances/amps_list.py index b0900dd0..08ed2271 100644 --- a/glances/amps_list.py +++ b/glances/amps_list.py @@ -125,18 +125,14 @@ class AmpsList: Search application monitored processes by a regular expression """ - ret = [] try: # Search in both cmdline and name (for kernel thread, see #1261) - for p in processlist: - if (re.search(amp_value.regex(), p['name']) is not None) or ( - p['cmdline'] is not None - and p['cmdline'] != [] - and re.search(amp_value.regex(), ' '.join(p['cmdline'])) is not None - ): - ret.append( - {'pid': p['pid'], 'cpu_percent': p['cpu_percent'], 'memory_percent': p['memory_percent']} - ) + ret = [ + {'pid': p['pid'], 'cpu_percent': p['cpu_percent'], 'memory_percent': p['memory_percent']} + for p in processlist + if re.search(amp_value.regex(), p['name']) + or ((cmdline := p.get('cmdline')) and re.search(amp_value.regex(), ' '.join(cmdline))) + ] except (TypeError, KeyError) as e: logger.debug(f"Can not build AMPS list ({e})")