Skip to content

Commit

Permalink
Add an argument to and
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenning committed Oct 16, 2023
1 parent 55d0248 commit c7c398e
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions ciscoconfparse/ccp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3808,7 +3808,7 @@ def parse_text_list(self, text, debug=False):
# Sort _expanded_interfaces...
######################################################################
if not isinstance(self.text, str):
error = f'CiscoRange(text="{text}", result_type={result_type}) [text: {type({text})}] must be a string representation of a CiscoRange(), such as "Ethernet1,4-7,12"'
error = f'CiscoRange(text="{self.text}") must be a string representation of a CiscoRange(), such as "Ethernet1,4-7,12"; the received text argument was {type({self.text})} instead of a string'
logger.error(error)
raise ValueError(error)
# De-deplicate _expanded_interfaces...
Expand Down Expand Up @@ -3881,7 +3881,7 @@ def remove(self, arg):
# This method is on CiscoRange()
@property
@logger.catch(reraise=True)
def as_list(self):
def as_list(self, result_type=str):
"""Return a list of sorted components; an empty string is automatically rejected. This method is tricky to test due to the requirement for the `.sort_list` attribute on all elements; avoid using the ordered nature of `as_list` and use `as_set`."""
yy_list = copy.deepcopy(self._list)
for ii in self._list:
Expand All @@ -3895,7 +3895,17 @@ def as_list(self):
self._list = yy_list
try:
# Disable linter qa checks on this embedded list syntax...
return sorted(set(self._list), reverse=self.reverse) # noqa
retval = sorted(set(self._list), reverse=self.reverse) # noqa
if result_type is None:
return retval
elif result_type is str:
return [str(ii) for ii in retval]
elif result_type is int:
return [int(ii) for ii in retval]
else:
error f"CiscoRange().as_list(result_type={result_type}) is not valid. Choose from {[None, int, str]}. result_type: None will return CiscoInterface() objects."
logger.critical(error)
raise ValueError(error)
except AttributeError as eee:
error = f"`sorted(self._list, reverse={self.reverse})` tried to access an attribute that does not exist on the objects being sorted: {eee}"
logger.error(error)
Expand All @@ -3907,7 +3917,7 @@ def as_list(self):
# This method is on CiscoRange()
@property
@logger.catch(reraise=True)
def as_set(self):
def as_set(self, result_type=str):
"""Return an unsorted set({}) components. Use this method instead of `.as_list` whenever possible to avoid the requirement for elements needing a `.sort_list` attribute."""
return set(self._list)

Expand Down Expand Up @@ -3971,7 +3981,16 @@ def compressed_str(self):
else:
retval += "," + str(range_list[ii])

return retval
if result_type is None:
return retval
elif result_type is str:
return [str(ii) for ii in retval]
elif result_type is int:
return [int(ii) for ii in retval]
else:
error f"CiscoRange().as_set(result_type={result_type}) is not valid. Choose from {[None, int, str]}. result_type: None will return CiscoInterface() objects."
logger.critical(error)
raise ValueError(error)

##############################################################################
# Restore SonarCloud warnings in this file
Expand Down

0 comments on commit c7c398e

Please sign in to comment.