1.5 IPSet Objects

An IPSet object represents any set of IP addresses, as produced by rwsetbuild and related programs. The IPSet object handles iteration over IP addresses with for x in set, and iteration over CIDR blocks using for x in set.cidr_iter().

class IPSet( [iterable])
The constructor creates an empty IPset. If an iterable is supplied as an argument, each item of the iterable will be added to the IPset. Each item of the iterable should either be an IPv4 IPAddr or a string representing a valid IPv4 address.

Other constructors, all class methods:

load( path)
Creates an IPSet from an IPset saved in a file. path must be a valid location of an IPset.

Supported operations:

Operation Equivalent Result Notes
len(s) cardinality of IPset s (1)
s.cardinality() cardinality of IPset s
addr in s test addr for membership in s (2)
addr not in s test addr for non-membership in s (2)
s.issubset(t) s <= t test whether every element in s is in t (3)
s.issuperset(t) s >= t test whether every element in t is in s (3)
s.union(t) s | t new IPset with elements from both s and t (3)
s.intersection(t) s & t new IPset with elements common to s and t (3)
s.difference(t) s - t new IPset with elements in s but not in t (3)
s.symmetric_difference(t) s ^ t new IPset with elements in either s or t but not both (3)
s.copy() new set with a copy of s
s.update(t) s |= t update s, adding elements from t (3)
s.intersection_update(t) s &= t update s, keeping only elements found in both s and t (3)
s.difference_update(t) s -= t update s, removing elements found in t (3)
s.symmetric_difference_update(t) s ^= t update s, keeping elements found in s or t but not in both (3)
s.add(addr) add element addr to IPset s (2)
s.remove(addr) remove addr from IPset s; raises KeyError if not present
s.discard(addr) removes addr from IPset s if present
s.clear() remove all elements from IPset s
Notes:
(1)
May throw OverflowError if there are too many IP addresses in the IPset. Use s.cardinality() instead.

(2)
addr can be an IPAddr, an IPWildcard, or the string representation of either. The address or addresses must be an IPv4 addresses.

(3)
With the non-operator version of this method, t can be any iterable object of IP addresses or IP address strings. The operator version requires that t be an IPSet.

Instance methods:

cidr_iter( )
Returns an iterator over CIDR blocks. Each iteration returns a tuple, the first element of which is the first IP address in the block, the second of which is the prefix length of the block. Can be used as for (addr, prefix) in s.cidr_iter().

save( filename)
Saves the IPSet in the file filename.