'How to ignore random values from server using Python

I am working on client code where it gets connected to server. Server is performing some task and keeps on returning the data every 1 sec. This data is tuple (7.6, 37), where 7.6 is the value of an area (which can be anywhere between 0 to 55) and 37 is the id of the data. This id is like counter which starts from 0 when server starts and keeps on incrementing until server is rebooted.

Client code has to see when the area value is less than 10.0 and perform further calculations. Sometimes in between server sends some noise values where area is less than 10.0 so we have to ignore that but if the value of area is less than 10.0 for more than 10sec, that means its not noise and we can proceed with further calculations. So noisy values will always be present for 3-4secs but if its more than 10secs, its not noise.

To handle this I have made up some of the below logic.

runOnce = True

data = server.recv()  # Getting the values from server
area = data[0]
id = data[1]


if area < 10.0:
    if runOnce:
        checkId = id + 10
        runOnce = False
    else:
        if checkId > id:
            # This data is not noisy

So in the above code, I am taking a variable runOnce as True. Once I receive the area value, I am setting a checkId variable with id+10 so if current id is 50, then I will check at 60. So at 60th id if area is still less than 10, that means, it has been 10 secs and thus the current data is not noisy. This seems to work fine but it its actually the noise then this code doesn't report it.

Can anyone please give some good suggestions to handle this logic. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source