TypeError: '>=' not supported between instances of 'NoneType' and 'int' with Meraki API Request

950 views Asked by At

I am trying to fix this from happening on my output of my VPN Failover script, in my function that scans the API for network health stats it seems like sometimes I get a None response from the API so it makes this function fail. How could I edit this code to make it so if it gets this kind of input it doesn't break and will just assign it a 0 or something of the sort.

Here is the function:

def networkHealthCheck(network, loss):
    # "Iterates through timeseries list to find cases where losspercent is >=30% or latency is >=100ms"

    for i in network["timeSeries"]:
        if i["lossPercent"] >= 30 or i["latencyMs"] >= 100:
            loss = True
            network_info = getNetwork(api_key, network["networkId"])
            network_name = network_info["name"]
            tags = network_info["tags"]
            print("Network to Failover: ", network_name, " // Network ID: ", network["networkId"], " // ZScaler IP: ", network["ip"], " // Loss:", loss, " // Initial Tags: ", tags)
            VPNFailover(tags, network, network_name, i)
            break
    return loss
1

There are 1 answers

2
Crooser On

You can use or. Basically your condition would be wrote as if (i["lossPercent"] or 0) >= 30 or (i["latencyMs"] or 0) >= 100:. If you want to know when you got None and when you have a regular value you will need to make another condition before the previous one.