MarkS
MarkS

Reputation: 1539

Checking for non-zero values in a dictionary

I am in the process of writing a program that connects to a Cisco switch or router and then examines the output of a 'show int '. I then process/parse the data to the point where I have a dictionary of twenty-one key/value pairs. All values are integers. It is working exactly as I want up to this point.

I am having some trouble visualizing what I want to do next and I was hoping I could get some ideas and/or guidance.

What I want to do is this:

Check each value. If ALL values are zero, then skip that dictionary. If ANY single value is non-zero (it will be a positive integer if it is not zero), then I want to save to a file the entire dictionary.

Each iteration of my program creates a dictionary representing data from a switch or router port.

Since I want the entire dictionary (all twenty-one key/value pairs) if even a single value is non-zero, I wasn't sure if adding all of the values and then checking if the sum is > 0 was the best option.

I could potentially be checking thousands of switch ports.

It seems to me that 'best' would be to start checking values and as soon as I hit a non-zero value then I want to save the entire dictionary and proceed to the next one (looping through the ports on a switch, for example), but I am just not sure of how to accomplish that.

I would appreciate some ideas or examples on how to best accomplish this task.

Oh, and I hesitate to use the word 'best'. Since I will be processing thousands of ports what I don't want is an inefficient approach, which is why I am hesitating to simply add up all of the values.

I am just not sure how to put into code: "as soon as I see a single non-zero value, save off the entire dictionary and proceed to the next one".

Upvotes: 1

Views: 6564

Answers (2)

Raymond Hettinger
Raymond Hettinger

Reputation: 226366

Here is a direct translation of the request, working from the parts you've already done and incorporating the any() function applied to the values of the dictionary.

# I am in the process of writing a program that connects to a Cisco switch or
# router and then examines the output of a 'show int '. I then process\parse the
# data to the point where I have a dictionary of twenty-one key\value pairs.
# All values are integers.
for device in devices:
    s = run_show_interfaces(device)
    d = preprocess_parse(s)

    # Check each value. If ALL values are zero, then skip that dictionary. If ANY
    # single value is non-zero (it will be a positive integer if it is not zero),
    # then I want to save to a file the entire dictionary.
    if any(d.values()):
        filename = os.path.join(device, '.txt')
        with open(filename, 'w') as f:
            json.dump(d, f)

FYI, the any() function has an early-out and will stop looking as soon as it finds a non-zero value. In Python 3, values() returns a view of the data so it doesn't copy all of information. In Python 2, use viewvalues() to achieve the same effect. Taken together, this will give you great preformance.

Upvotes: 2

Andrew O'Rourke
Andrew O'Rourke

Reputation: 137

if all non-zero keys have the same value, you can just do dict.get(x), if x is not in the dictionary, it will return none.

otherwise:

    for value in dict.values():
      if value != 0:
        return true
    return false

you may also want to do a dict.get(0) first in the case all values are non-zero.

Upvotes: 0

Related Questions