Skip to content
Snippets Groups Projects
Commit 98107824 authored by Pavel Kácha's avatar Pavel Kácha
Browse files

Merge branch 'devel' into 'master'

Add equality with arbitrary objects

See merge request !9
parents 25a32652 293e6559
No related branches found
No related tags found
1 merge request!9Add equality with arbitrary objects
Pipeline #12447 passed
# Official language image. Look for the different tagged releases at: # Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/ # https://hub.docker.com/r/library/python/tags/
image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/python:latest image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/python:3.11
# Change pip's cache directory to be inside the project directory since we can # Change pip's cache directory to be inside the project directory since we can
# only cache local items. # only cache local items.
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# Copyright (c) 2016, CESNET, z. s. p. o. # Copyright (c) 2016, CESNET, z. s. p. o.
# Use of this source is governed by an ISC license, see LICENSE file. # Use of this source is governed by an ISC license, see LICENSE file.
__version__ = '0.1.12' __version__ = '0.1.13'
__author__ = 'Pavel Kácha <pavel.kacha@cesnet.cz>' __author__ = 'Pavel Kácha <pavel.kacha@cesnet.cz>'
import socket import socket
...@@ -26,7 +26,16 @@ class Range(object): ...@@ -26,7 +26,16 @@ class Range(object):
return self.high() - self.low() + 1 return self.high() - self.low() + 1
def __eq__(self, other): def __eq__(self, other):
return (self.low() == other.low() and self.high() == other.high()) if (
hasattr(self, "single")
and hasattr(other, "single")
and (
issubclass(self.single, other.single)
or issubclass(other.single, self.single)
)
):
return self.low() == other.low() and self.high() == other.high()
return False
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
......
...@@ -108,6 +108,14 @@ class TestIPRange(unittest.TestCase): ...@@ -108,6 +108,14 @@ class TestIPRange(unittest.TestCase):
self.assertFalse(ip2 != ip3) self.assertFalse(ip2 != ip3)
self.assertFalse(ip1 != ip3) self.assertFalse(ip1 != ip3)
def test4OtherObject(self):
ip1 = IP4Net("192.0.2.65/32")
ip2 = IP4Range("192.0.2.65-192.0.2.65")
ip3 = IP4("192.0.2.65")
self.assertFalse(ip1 == [ip1])
self.assertFalse(ip2 == 1)
self.assertFalse(ip3 == "192.0.2.65")
def test6SameNetRange(self): def test6SameNetRange(self):
net1 = IP6Net("2001:db8:220:1::/64") net1 = IP6Net("2001:db8:220:1::/64")
net2 = IP6Range("2001:db8:220:1::-2001:db8:220:1:ffff:ffff:ffff:ffff") net2 = IP6Range("2001:db8:220:1::-2001:db8:220:1:ffff:ffff:ffff:ffff")
...@@ -125,6 +133,14 @@ class TestIPRange(unittest.TestCase): ...@@ -125,6 +133,14 @@ class TestIPRange(unittest.TestCase):
self.assertFalse(ip2 != ip3) self.assertFalse(ip2 != ip3)
self.assertFalse(ip1 != ip3) self.assertFalse(ip1 != ip3)
def test6OtherObject(self):
ip1 = IP6Net("2001:db8:220:1:248:1893:25c8:1946/128")
ip2 = IP6Range("2001:db8:220:1:248:1893:25c8:1946-2001:db8:220:1:248:1893:25c8:1946")
ip3 = IP6("2001:db8:220:1:248:1893:25c8:1946")
self.assertFalse(ip1 == [ip1])
self.assertFalse(ip2 == 1)
self.assertFalse(ip3 == "2001:db8:220:1:248:1893:25c8:1946")
def test4Contains(self): def test4Contains(self):
self.assertTrue(IP4Net("192.0.2.64/28") in IP4Net("192.0.2.64/26")) self.assertTrue(IP4Net("192.0.2.64/28") in IP4Net("192.0.2.64/26"))
self.assertTrue(IP4Net("192.0.2.64/28") in IP4Range("192.0.2.64-192.0.2.127")) self.assertTrue(IP4Net("192.0.2.64/28") in IP4Range("192.0.2.64-192.0.2.127"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment