Python: How can I get a loop to break if the value == 'null' shows up multiple times in a row while reading a log?

Question:

Reading an android display mode monitor which gives the value of the plugged in resolution. I want the loop to break if it reads "null" multiple times in a row:

Display Mode:  720p60hz                                                                                       
Display Mode:  720p60hz                                                                                       
Display Mode:  null                                                                                       
Display Mode:  null                                                                                       
Display Mode:  null                                                                                       
Display Mode:  null

BREAK! 

CODE

import time
import subprocess

while True:

z = subprocess.getoutput("adb shell cat /sys/class/display/mode")
time.sleep(.1)
print(f'Display Mode:  {z}')

t1 = time.time()
t2 = time.time()
if z == 'null':


print(f't1 is :{t1}')

else:
continue

if z == 'null'
print(f't2 is :{t2}')
print('i am null')

if t2-t1 > .1:
  
  break
Asked By: Genius

||

Answers:

Your example above does not show valid Python code.

For your desired result, you can initialize a counter to 0, increment it every time you get null and reset it back to 0 if any other value appears:

counter = 0

while counter < 5: 
    if value == 'null':
        counter += 1
    else:
        counter = 0

    # Code runs as usual here

print("Too many null values in a row, halting")
Answered By: Vladimir Vilimaitis

If I understood your code well You’re trying to break the loop when z receives null value. In python we use None to represent null values.

Try to replace this line:

if z == 'null'

For this line:

if z == None
Answered By: Tiago Cabral

Your code is a bit hard to read because the indentation was left out, and indentation is a part of Python syntax.
But here’s my best guess for what you’re looking for:

import time
import subprocess

THRESHOLD_BAD_DISPLAY_READS = 10  # Set this to the number of bad reads in a row that you can tolerate
num_bad_display_reads = 0

while num_bad_display_reads < THRESHOLD_BAD_DISPLAY_READS:
    display_mode = subprocess.getoutput("adb shell cat /sys/class/display/mode")
    time.sleep(.1)
    if display_mode == 'null' or display_mode is None:
        num_bad_display_reads += 1  # Start counting up
    else:
        num_bad_display_reads = 0  # Reset the count
        #print(f'Display Mode:  {display_mode}')  # Uncomment this during debugging

# You have now left the display loop. Feel free to do something else.

My guess is that you would like to re-enter the loop if the display recovers. If that’s true, then I recommend that you post some specifics about that in another SO question.

Note: I renamed z to display_mode.

Answered By: bobsbeenjamin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.