Network Automation Example - Generators With Filtering | zeroslash.io

Network Automation Example - Generators With Filtering

Submitted by zeroslash on Tue, 05/15/2018 - 12:58

Following on from my last post, I made a variation of it. In this instance I wanted to filter the interfaces based on a certain amount of errors.

One thing I changed is I switched it up with a generator function instead (lines 11-14) and used that to provide the iterable sequence in the generator comprehension following that. The generator comprehension (lines 16-18) is where the filtering happens.

One thing to note is that I didn't need to cast the error variable into int() in the generator comprehension, as opposed to what I did in the sort() part in my previous post. Check out my previous post to find out the difference :)

Code:

Output:

{'name': 'FastEthernet', 'number': '3/3/1', 'error': 781}
{'name': 'GigabitEthernet', 'number': '4/1/0', 'error': 142}
{'name': 'GigabitEthernet', 'number': '3/2/0', 'error': 142}
{'name': 'GigabitEthernet', 'number': '0/2/0', 'error': 142}