Add __ne__ built-in function

In Python 3 __ne__ by default delegates to __eq__ and inverts the
result, but in Python 2 they urge you to define __ne__ when you
define __eq__ for it to work properly [1].There are no implied
relationships among the comparison operators. The truth of x==y
does not imply that x!=y is false. Accordingly, when defining
__eq__(), one should also define __ne__() so that the operators
will behave as expected.
[1]https://docs.python.org/2/reference/datamodel.html#object.__ne__

Change-Id: I7d41128d88c79ebb5de1c38f8aaf98cc9561367c
This commit is contained in:
yuyafei 2016-07-04 17:00:46 +08:00
parent 5e99a99c6a
commit e0c03c63cf
5 changed files with 19 additions and 3 deletions

View File

@ -295,6 +295,9 @@ class ExtraProperties(collections.MutableMapping, dict):
else: else:
return False return False
def __ne__(self, other):
return not self.__eq__(other)
def __len__(self): def __len__(self):
return dict(self).__len__() return dict(self).__len__()

View File

@ -303,6 +303,9 @@ class StoreLocations(collections.MutableSequence):
def __eq__(self, other): def __eq__(self, other):
return self.value == self.__cast(other) return self.value == self.__cast(other)
def __ne__(self, other):
return not self.__eq__(other)
def __iter__(self): def __iter__(self):
return iter(self.value) return iter(self.value)

View File

@ -150,6 +150,9 @@ class QuotaImageTagsProxy(object):
def __eq__(self, other): def __eq__(self, other):
return self.tags == other return self.tags == other
def __ne__(self, other):
return not self.__eq__(other)
def __iter__(self, *args, **kwargs): def __iter__(self, *args, **kwargs):
return self.tags.__iter__(*args, **kwargs) return self.tags.__iter__(*args, **kwargs)
@ -214,6 +217,9 @@ class QuotaImageLocationsProxy(object):
def __eq__(self, other): def __eq__(self, other):
return self.locations == other return self.locations == other
def __ne__(self, other):
return not self.__eq__(other)
def __getitem__(self, *args, **kwargs): def __getitem__(self, *args, **kwargs):
return self.locations.__getitem__(*args, **kwargs) return self.locations.__getitem__(*args, **kwargs)

View File

@ -298,7 +298,7 @@ class TestExtraProperties(test_utils.BaseTestCase):
a_dict = {'foo': 'bar', 'snitch': 'golden'} a_dict = {'foo': 'bar', 'snitch': 'golden'}
extra_properties = domain.ExtraProperties(a_dict) extra_properties = domain.ExtraProperties(a_dict)
ref_extra_properties = {'boo': 'far', 'gnitch': 'solden'} ref_extra_properties = {'boo': 'far', 'gnitch': 'solden'}
self.assertFalse(extra_properties.__eq__(ref_extra_properties)) self.assertNotEqual(ref_extra_properties, extra_properties)
def test_eq_with_unequal_ExtraProperties_object(self): def test_eq_with_unequal_ExtraProperties_object(self):
a_dict = {'foo': 'bar', 'snitch': 'golden'} a_dict = {'foo': 'bar', 'snitch': 'golden'}
@ -306,13 +306,13 @@ class TestExtraProperties(test_utils.BaseTestCase):
ref_extra_properties = domain.ExtraProperties() ref_extra_properties = domain.ExtraProperties()
ref_extra_properties['gnitch'] = 'solden' ref_extra_properties['gnitch'] = 'solden'
ref_extra_properties['boo'] = 'far' ref_extra_properties['boo'] = 'far'
self.assertFalse(extra_properties.__eq__(ref_extra_properties)) self.assertNotEqual(ref_extra_properties, extra_properties)
def test_eq_with_incompatible_object(self): def test_eq_with_incompatible_object(self):
a_dict = {'foo': 'bar', 'snitch': 'golden'} a_dict = {'foo': 'bar', 'snitch': 'golden'}
extra_properties = domain.ExtraProperties(a_dict) extra_properties = domain.ExtraProperties(a_dict)
random_list = ['foo', 'bar'] random_list = ['foo', 'bar']
self.assertFalse(extra_properties.__eq__(random_list)) self.assertNotEqual(random_list, extra_properties)
class TestTaskFactory(test_utils.BaseTestCase): class TestTaskFactory(test_utils.BaseTestCase):

View File

@ -572,6 +572,10 @@ class TestQuotaImageTagsProxy(test_utils.BaseTestCase):
proxy = glance.quota.QuotaImageTagsProxy(set([])) proxy = glance.quota.QuotaImageTagsProxy(set([]))
self.assertEqual(set([]), proxy) self.assertEqual(set([]), proxy)
def test_not_equals(self):
proxy = glance.quota.QuotaImageTagsProxy(set([]))
self.assertNotEqual('foo', proxy)
def test_contains(self): def test_contains(self):
proxy = glance.quota.QuotaImageTagsProxy(set(['foo'])) proxy = glance.quota.QuotaImageTagsProxy(set(['foo']))
self.assertIn('foo', proxy) self.assertIn('foo', proxy)