diff --git a/netutils/interface.py b/netutils/interface.py index eb2afc07..fb170fc3 100644 --- a/netutils/interface.py +++ b/netutils/interface.py @@ -53,13 +53,20 @@ def _pairwise(interface_constant: t.List[int]) -> t.List[t.Tuple[int, int]]: interface_constant.append(match.end()) cartesian_list.append(_range_expand(match.group()[1:-1])) + # accommodate trailing constants + if interface_constant[-1] < len(interface_pattern): + interface_constant.append(len(interface_pattern)) + interface_constant_out = _pairwise(interface_constant) expanded_interfaces = [] for element in itertools.product(*cartesian_list): current_interface = "" for count, item in enumerate(interface_constant_out): current_interface += interface_pattern[item[0] : item[1]] # noqa: E203 - current_interface += str(element[count]) + # only append the next item if the current constant + # doesn't fall at the end of the line + if count < len(element): + current_interface += str(element[count]) expanded_interfaces.append(current_interface) return expanded_interfaces diff --git a/tests/unit/test_interface.py b/tests/unit/test_interface.py index d2e5e180..c6b94b3c 100644 --- a/tests/unit/test_interface.py +++ b/tests/unit/test_interface.py @@ -177,6 +177,8 @@ {"sent": "Gi[1,3-5]", "received": ["Gi1", "Gi3", "Gi4", "Gi5"]}, {"sent": "Gi[1,3-5,8]", "received": ["Gi1", "Gi3", "Gi4", "Gi5", "Gi8"]}, {"sent": "[1,2]/0/[1-2]", "received": ["1/0/1", "1/0/2", "2/0/1", "2/0/2"]}, + {"sent": "Gi[1-3]/1", "received": ["Gi1/1", "Gi2/1", "Gi3/1"]}, + {"sent": "Gi[1-3]/[1]", "received": ["Gi1/1", "Gi2/1", "Gi3/1"]}, ] INTERFACE_SORT = [