diff options
author | pmoody@google.com <pmoody@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035> | 2010-05-10 05:54:16 +0000 |
---|---|---|
committer | pmoody@google.com <pmoody@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035> | 2010-05-10 05:54:16 +0000 |
commit | 1368c60abd0184343730c125de9d279db193a455 (patch) | |
tree | 733d39ddd84f6e813011c25dec44a5109ec3dc93 /trunk | |
parent | 8391c8366db73beaf0d05b872c57f64920b8d475 (diff) | |
download | ipaddr-py-1368c60abd0184343730c125de9d279db193a455.tar.gz |
+ add masked() to _BaseNet to automatically mask out
the host bits of a network object. issue58.
Reported by Robert Thomson.
git-svn-id: https://ipaddr-py.googlecode.com/svn@166 09200d28-7f98-11dd-ad27-0f66e57d2035
Diffstat (limited to 'trunk')
-rw-r--r-- | trunk/ipaddr.py | 5 | ||||
-rwxr-xr-x | trunk/ipaddr_test.py | 9 |
2 files changed, 14 insertions, 0 deletions
diff --git a/trunk/ipaddr.py b/trunk/ipaddr.py index f7c7d2c..5f3a15b 100644 --- a/trunk/ipaddr.py +++ b/trunk/ipaddr.py @@ -903,6 +903,11 @@ class _BaseNet(_IPAddrBase): yield current + def masked(self): + """Return the network object with the host bits masked out.""" + return IPNetwork('%s/%d' % (self.network, self._prefixlen), + version=self._version) + def subnet(self, prefixlen_diff=1, new_prefix=None): """Return a list of subnets, rather than an interator.""" return list(self.iter_subnets(prefixlen_diff, new_prefix)) diff --git a/trunk/ipaddr_test.py b/trunk/ipaddr_test.py index d1313f3..90a12e0 100755 --- a/trunk/ipaddr_test.py +++ b/trunk/ipaddr_test.py @@ -47,6 +47,15 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEqual("IPv6Network('::1/128')", repr(ipaddr.IPv6Network('::1'))) + def testAutoMasking(self): + addr1 = ipaddr.IPv4Network('1.1.1.255/24') + addr1_masked = ipaddr.IPv4Network('1.1.1.0/24') + self.assertEqual(addr1_masked, addr1.masked()) + + addr2 = ipaddr.IPv6Network('2000:cafe::efac:100/96') + addr2_masked = ipaddr.IPv6Network('2000:cafe::/96') + self.assertEqual(addr2_masked, addr2.masked()) + # issue57 def testAddressIntMath(self): self.assertEqual(ipaddr.IPv4Address('1.1.1.1') + 255, |