in scripts/ValidateParameters/netaddr/ip/sets.py [0:0]
def remove(self, addr, flags=0):
"""
Removes an IP address or subnet or IPRange from this IP set. Does
nothing if it is not already a member.
Note that this method behaves more like discard() found in regular
Python sets because it doesn't raise KeyError exceptions if the
IP address or subnet is question does not exist. It doesn't make sense
to fully emulate that behaviour here as IP sets contain groups of
individual IP addresses as individual set members using IPNetwork
objects.
:param addr: An IP address or subnet, or an IPRange.
:param flags: decides which rules are applied to the interpretation
of the addr value. See the netaddr.core namespace documentation
for supported constant values.
"""
if isinstance(addr, IPRange):
cidrs = iprange_to_cidrs(addr[0], addr[-1])
for cidr in cidrs:
self.remove(cidr)
return
if isinstance(addr, _int_type):
addr = IPAddress(addr, flags=flags)
else:
addr = IPNetwork(addr)
# This add() is required for address blocks provided that are larger
# than blocks found within the set but have overlaps. e.g. :-
#
# >>> IPSet(['192.0.2.0/24']).remove('192.0.2.0/23')
# IPSet([])
#
self.add(addr)
remainder = None
matching_cidr = None
# Search for a matching CIDR and exclude IP from it.
for cidr in self._cidrs:
if addr in cidr:
remainder = cidr_exclude(cidr, addr)
matching_cidr = cidr
break
# Replace matching CIDR with remaining CIDR elements.
if remainder is not None:
del self._cidrs[matching_cidr]
for cidr in remainder:
self._cidrs[cidr] = True