Skip to content

Commit f047b4e

Browse files
jellonekbd808
authored andcommitted
Provide methods for comparison of IpRangeList objects
1 parent f86734a commit f047b4e

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

iptools/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,38 @@ def __len__(self):
517517
"""
518518
return sum(r.__len__() for r in self.ips)
519519
# end __len__
520+
521+
def __hash__(self):
522+
"""
523+
Return correct hash for IpRangeList object
524+
525+
>>> a = IpRange('127.0.0.0/8')
526+
>>> b = IpRange('127.0.0.0', '127.255.255.255')
527+
>>> IpRangeList(a, b).__hash__() == IpRangeList(a, b).__hash__()
528+
True
529+
>>> IpRangeList(a, b).__hash__() == IpRangeList(b, a).__hash__()
530+
True
531+
>>> c = IpRange('10.0.0.0/8')
532+
>>> IpRangeList(a, c).__hash__() == IpRangeList(c, a).__hash__()
533+
False
534+
"""
535+
return hash(self.ips)
536+
# end __hash__
537+
538+
def __eq__(self, other):
539+
"""
540+
>>> a = IpRange('127.0.0.0/8')
541+
>>> b = IpRange('127.0.0.0', '127.255.255.255')
542+
>>> IpRangeList(a, b) == IpRangeList(a, b)
543+
True
544+
>>> IpRangeList(a, b) == IpRangeList(b, a)
545+
True
546+
>>> c = IpRange('10.0.0.0/8')
547+
>>> IpRangeList(a, c) == IpRangeList(c, a)
548+
False
549+
"""
550+
return hash(self) == hash(other)
551+
# end __eq__
520552
# end class IpRangeList
521553

522554
# vim: set sw=4 ts=4 sts=4 et :

0 commit comments

Comments
 (0)