Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to return a boolean value and a message [duplicate]

I have a simple script that checks various Linux processes and, finding one of them, logs a particular message ("particular" in terms of referencing the service's name).

My question: what is the proper, Pythonic way to have a multi-conditional function return a boolean and a string (for use in a printed message)?

Here's a stripped-down version of my current solution (using tuples):

import subprocess
import time

def _isProcessRunning(cmd):
    return int(
            subprocess.check_output(
                '{} | grep -v grep | wc -l'.format(cmd),
                shell=True
                )
            ) > 0

def processGroupFound():
    if _isProcessRunning('ps auwxx | grep duplicity'):
        return (True, 'Duplicity')
    elif _isProcessRunning('who | grep pts'):
        return (True, 'SSH')
    elif _isProcessRunning('netstat -na | grep ESTA | grep 5901'):
        return (True, 'VNC')
    else:
        return (False, '')

def worker():
    while True:
        process_found, service_string = processGroupFound()
        if process_found:
            print('{} found; skipping'.format(service_string))
        else:
            print('Continuing on')
        time.sleep(10)


if __name__ == "__main__":
    worker()

This works, but I care about doing it correctly (stylistically, in particular, but if you glean incorrect logic in this brief sample, please feel free to comment there, too. Appreciate your assistance!

like image 903
ABach Avatar asked Dec 09 '16 21:12

ABach


People also ask

How do you return a Boolean output in Python?

Functions can Return a Boolean if myFunction(): print("YES!") else: print("NO!")

How do I return true and false in Python?

Python bool() function is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.

How do you return multiple things from a function in Python?

In Python, you can return multiple values by simply return them separated by commas. In Python, comma-separated values are considered tuples without parentheses, except where required by syntax. For this reason, the function in the above example returns a tuple with each value as an element.

Why we use return true in Python?

The Python return statement is a key component of functions and methods. You can use the return statement to make your functions send Python objects back to the caller code. These objects are known as the function's return value. You can use them to perform further computation in your programs.


2 Answers

An empty string in Python is "falsey", so it's somewhat redundant to return (False, ''). I might do this instead:

def processGroupFound():
    if _isProcessRunning('ps auwxx | grep duplicity'):
        return 'Duplicity'
    elif _isProcessRunning('who | grep pts'):
        return 'SSH'
    elif _isProcessRunning('netstat -na | grep ESTA | grep 5901'):
        return 'VNC'
    else:
        return ''

def worker():
    while True:
        service_string = processGroupFound()
        if service_string:
            print('{} found; skipping'.format(service_string))
        else:
            print('Continuing on')
        time.sleep(10)

(See 4.1 Truth Value Testing)

like image 92
Joe Carey Avatar answered Oct 13 '22 01:10

Joe Carey


I think this would be pythonic too (but may be it's just me)

class NoRunningService(RuntimeError): pass

def findService():
    if _isProcessRunning('ps auwxx | grep duplicity'):
        return 'Duplicity'
    elif _isProcessRunning('who | grep pts'):
        return 'SSH'
    elif _isProcessRunning('netstat -na | grep ESTA | grep 5901'):
        return 'VNC'
    else:
        raise NoRunningService

def worker():
    while True:
        try:
            service_string = findService()
        except NoRunningService:
            print('Continuing on')
        else:
            print('{} found; skipping'.format(service_string))
        time.sleep(10)
like image 36
Gribouillis Avatar answered Oct 12 '22 23:10

Gribouillis