Skip to content

Commit

Permalink
Fix CiscoRange().remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenning committed Oct 20, 2023
1 parent 0b65c0e commit c1b6d2b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ciscoconfparse/ccp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3991,6 +3991,7 @@ def parse_text_list(self, text, debug=False):
# This method is on CiscoRange()
@logger.catch(reraise=True)
def __repr__(self):
"""Return a formal string representation of this CiscoRange() object."""
return f"""<CiscoRange {self.__str__()}>"""

# This method is on CiscoRange()
Expand All @@ -4000,7 +4001,7 @@ def __len__(self):
return len(self._list)

def __iter__(self):
"""Return an iterator for this CiscoRange()"""
"""Return an iterator for this CiscoRange(). If CiscoRange().__iter__() is not implemented, many CiscoRnage() index errors are generated by other methods accessing an index that is off by one."""
return iter(self._list)

# This method is on CiscoRange()
Expand Down Expand Up @@ -4120,19 +4121,25 @@ def insert(self, idx, val, sort=True):
# This method is on CiscoRange()
@logger.catch(reraise=True)
def __str__(self):
"""Return a formal string representation of this CiscoRange()"""
return "[" + str(", ".join([str(ii) for ii in self._list])) + "]"

# This method is on CiscoRange()
@logger.catch(reraise=True)
def remove(self, arg):
def remove(self, arg, debug=False):
length_before = len(self._list)
list_before = copy.deepcopy(self._list)
# Try removing some common result types...
for result_type in [None, str, int, float]:
if result_type is None:
new_list = [ii for ii in list_before if ii != arg]
else:
new_list = [ii for ii in list_before if result_type(ii) != arg]
try:
new_list = [ii for ii in list_before if result_type(ii) != arg]
except TypeError as eee:
if debug is True:
error = "Found type mismatch: {arg}, {ii}"
logger.debug(error)
if len(new_list) < length_before:
self._list = new_list
break
Expand Down

0 comments on commit c1b6d2b

Please sign in to comment.