Skip to content

Commit

Permalink
Update the CiscoRange().attribute_sort() method and deprecate CiscoRa…
Browse files Browse the repository at this point in the history
…nge().insert()
  • Loading branch information
mpenning committed Oct 20, 2023
1 parent 12ef974 commit 993e17b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions ciscoconfparse/ccp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4032,9 +4032,22 @@ def __setitem__(self, ii, val):

# This method is on CiscoRange()
@logger.catch(reraise=True)
def attribute_sort(self, target_list, attribute="sort_list", reverse=False):
new_list = sorted(self._list, key=lambda x: getattr(x, attribute), reverse=reverse)
return target_list
def attribute_sort(self, target_list=None, attribute="sort_list", reverse=False):
sort_this_obj = False
if target_list is None:
sort_this_obj = True
target_list = copy.deepcopy(self._list)
if sort_this_obj:
self._list = target_list
return self
else:
if isinstance(target_list, list):
new_list = sorted(target_list, key=lambda x: getattr(x, attribute), reverse=reverse)
return new_list
else:
error = f"`target_list` must be a list; however, we got `target_list`: {target_list} {type(target_list)}"
logger.critical(error)
raise ValueError(error)


# This method is on CiscoRange()
Expand Down Expand Up @@ -4073,6 +4086,10 @@ def append(self, val, sort=True):
# This method is on CiscoRange()
@logger.catch(reraise=True)
def insert(self, idx, val, sort=True):
error = "CiscoRange().insert() currently generates a stackoverflow."
logger.critical(error)
raise NotImplementedError("CiscoRange().insert() currently generates a stackoverflow.")

# Insert at the end of the list with new_last_list_idx = len(self._list)
if val in self._list:
raise DuplicateMember(val)
Expand Down

0 comments on commit 993e17b

Please sign in to comment.