On 2017-05-26 13:29, Cecil Westerhof wrote:
> To check if Firefox is running I use:
> if not 'firefox' in [i.name() for i in list(process_iter())]:
>
> It probably could be made more efficient, because it can stop when
> it finds the first instance.
>
> But know I switched to Debian and there firefox is called
> firefox-esr. So I should use:
> re.search('^firefox', 'firefox-esr')
>
> Is there a way to rewrite
> [i.name() for i in list(process_iter())]
>
> so that it returns True when there is a i.name() that matches and
> False otherwise?
> And is it possible to stop processing the list when it found a
> match?
this sounds like an ideal use-case for any():
if any("firefox" in p.name for p in process_iter()):
do_stuff()
or
if any(p.name.startswith("firefox") for p in process_iter()):
do_stuff()
The any() call stops after the True-ish match (and the all() call
stops after the first False-ish match).
-tkc
--
https://mail.python.org/mailman/listinfo/python-list