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 <otilibil@eurecom.fr>
This commit is contained in:
Ariel Otilibili 2025-06-02 14:00:11 +02:00 committed by nicolargo
parent 75b7998185
commit 0ffb08dd2a
1 changed files with 6 additions and 10 deletions

View File

@ -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})")