If none of the above python

I have a series of checks that I need to work with sensor data, all checks are independent, so they can not be formatted as elifs or elses, if any checks are not performed, I need to print this for the user. If none of the checks are complete, I want to tell the user that the sensor is OK

(i is just an iterator going through all the sensors in the array)

if worst_stdev[i] > 5:
    print("Sensor bad, STDEV VALUE: {}".format(worst_stdev[i]))
if worst_invalid[i] > 2:
    print("Sensor bad, INVALID VALUE: {}".format(worst_invalid[i]))
if worst_err[i] > 1:
    print("Sensor bad, ERROR VALUE: {}".format(worst_bit_err[i]))
if not (worst_stdev[i] > 5 or worst_invalid[i] > 2 or worst_err[i] > 1):
    print("Sensor OK")

Last, if the operator bothers me the most, he feels superfluous (and perhaps slower?) To check again all the things that I have already checked. Is there a good way to make this more elegant?

+4
source share
4 answers

, . :

was_error = False
if worst_stdev[i] > 5:
    print("Sensor bad, STDEV VALUE: {}".format(worst_stdev[i]))
    was_error = True
if worst_invalid[i] > 2:
    print("Sensor bad, INVALID VALUE: {}".format(worst_invalid[i]))
    was_error = True
if worst_err[i] > 1:
    print("Sensor bad, ERROR VALUE: {}".format(worst_bit_err[i]))
    was_error = True
if not was_error:
    print("Sensor OK")

, , . , , , , . , if not was_error:

+8

if s.. , . , . . any , statemeny. . ( @SethMMorton)

bad_stdev = worst_stdev[i] > 5
bad_invalid = worst_invalid[i] > 2
bad_err = worst_err[i] > 1
if bad_stdev:
    print("Sensor bad, STDEV VALUE: {}".format(worst_stdev[i]))
if bad_invalid:
    print("Sensor bad, INVALID VALUE: {}".format(worst_invalid[i]))
if bad_err:
    print("Sensor bad, ERROR VALUE: {}".format(worst_bit_err[i]))
if not any([bad_stdev, bad_invalid, bad_err]):
    print("Sensor OK")
+6

.

, , :

    def check_cases(i, cases, text='sensor bad'):
        for case, (param, condition) in cases.items():
            value = param[i]
            if condition(value):
                yield ValueError(f'{text}: {value}')

.

cases = {'stdev value': (worst_stdev, lambda x: x > 5), 'invalid value': (worst_invalid, lambda x: x > 2), 'error value': (worst_bit_err, lambda x: x > 1)}

errors = list(check_cases(1, cases))

:

for err in errors:
    print(err)

if not errors:
    print('OK')

, , , , , . .

+1

It depends on how your code is executed. You can do something similar and catch the exception so that it does not fall into the final print statement (obviously, you would need to define an exception):

if worst_stdev[i] > 5:
    raise BadSensorException("Sensor bad, STDEV VALUE: {}".format(worst_stdev[i]))
if worst_invalid[i] > 2:
    raise BadSensorException("Sensor bad, INVALID VALUE: {}".format(worst_invalid[i]))
if worst_err[i] > 1:
    raise BadSensorException("Sensor bad, ERROR VALUE: {}".format(worst_bit_err[i]))

print("Sensor OK")
0
source

Source: https://habr.com/ru/post/1684135/


All Articles