Last active
May 10, 2019 13:39
-
-
Save candlerb/a8a7c8c6f3e3215baa83c54ba8c4512a to your computer and use it in GitHub Desktop.
Natural sorting with option of priority to line card
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import unittest | |
def parse_natural(name, line_card_first=False): | |
res = re.split("(\d+)", name) | |
if line_card_first and len(res) > 3: | |
res = ["", res[1]] + res | |
# first and last element is always a string; alternate values are numbers | |
for i in range(1, len(res), 2): | |
res[i] = int(res[i]) | |
return res | |
class NaturalOrderingTestCase(unittest.TestCase): | |
def test_parser(self): | |
self.assertEqual(parse_natural("Gi1/0/15"), ["Gi",1,"/",0,"/",15,""]) | |
self.assertEqual(parse_natural("Gi1/0/15", True), ["",1,"Gi",1,"/",0,"/",15,""]) | |
self.assertEqual(parse_natural("Fa20"), ["Fa",20,""]) | |
self.assertEqual(parse_natural("Fa20", True), ["Fa",20,""]) | |
self.assertEqual(parse_natural("1/A20"), ["",1,"/A",20,""]) | |
self.assertEqual(parse_natural("100A"), ["",100,"A"]) | |
def _compare_names(self, names, line_card_first=False): | |
sorted_names = sorted(names, key=lambda x: parse_natural(x, line_card_first)) | |
self.assertEqual(tuple(sorted_names), names) | |
def test_interface_ordering_numeric(self): | |
INTERFACES = ( | |
'0', | |
'0.1', | |
'0.2', | |
'0.10', | |
'0.100', | |
'0:1', | |
'0:1.1', | |
'0:1.2', | |
'0:1.10', | |
'0:2', | |
'0:2.1', | |
'0:2.2', | |
'0:2.10', | |
'1', | |
'1.1', | |
'1.2', | |
'1.10', | |
'1.100', | |
'1:1', | |
'1:1.1', | |
'1:1.2', | |
'1:1.10', | |
'1:2', | |
'1:2.1', | |
'1:2.2', | |
'1:2.10', | |
) | |
self._compare_names(INTERFACES) | |
def test_interface_ordering_linux(self): | |
INTERFACES = ( | |
'eth0', | |
'eth0.1', | |
'eth0.2', | |
'eth0.10', | |
'eth0.100', | |
'eth1', | |
'eth1.1', | |
'eth1.2', | |
'eth1.100', | |
'lo0', | |
) | |
self._compare_names(INTERFACES) | |
def test_interface_ordering_junos(self): | |
# NOTE: This test case has been changed so that xe interfaces go at the end | |
INTERFACES = ( | |
'ae1', | |
'ae2', | |
'ae10.1', | |
'ae10.10', | |
'irb.1', | |
'irb.2', | |
'irb.10', | |
'irb.100', | |
'lo0', | |
'xe-0/0/0', | |
'xe-0/0/1', | |
'xe-0/0/2', | |
'xe-0/0/3', | |
'xe-0/1/0', | |
'xe-0/1/1', | |
'xe-0/1/2', | |
'xe-0/1/3', | |
'xe-1/0/0', | |
'xe-1/0/1', | |
'xe-1/0/2', | |
'xe-1/0/3', | |
'xe-1/1/0', | |
'xe-1/1/1', | |
'xe-1/1/2', | |
'xe-1/1/3', | |
'xe-2/0/0.1', | |
'xe-2/0/0.2', | |
'xe-2/0/0.10', | |
'xe-2/0/0.11', | |
'xe-2/0/0.100', | |
'xe-3/0/0:1', | |
'xe-3/0/0:2', | |
'xe-3/0/0:10', | |
'xe-3/0/0:11', | |
'xe-3/0/0:100', | |
'xe-10/1/0', | |
'xe-10/1/1', | |
'xe-10/1/2', | |
'xe-10/1/3', | |
) | |
self._compare_names(INTERFACES) | |
def test_interface_ordering_ios(self): | |
INTERFACES = ( | |
'GigabitEthernet0/1', | |
'GigabitEthernet0/2', | |
'GigabitEthernet0/10', | |
'TenGigabitEthernet0/20', | |
'TenGigabitEthernet0/21', | |
'GigabitEthernet1/1', | |
'GigabitEthernet1/2', | |
'GigabitEthernet1/10', | |
'TenGigabitEthernet1/20', | |
'TenGigabitEthernet1/21', | |
'FastEthernet1', | |
'FastEthernet2', | |
'FastEthernet10', | |
) | |
self._compare_names(INTERFACES, True) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment