From: Philipp Gesang Date: Mon, 5 Nov 2018 09:12:35 +0000 (+0100) Subject: support comparison of SimpleCnf() with non-SimpleCnf() objects X-Git-Tag: v1.4~18^2~2 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=ee39385cdf7e0290c9e6034a75142426b27880c7;p=pyi2ncommon support comparison of SimpleCnf() with non-SimpleCnf() objects Since __eq__ operations are polymorphic, crashing on different ``types'' is not a good idea. --- diff --git a/src/simple_cnf.py b/src/simple_cnf.py index 23b5ca4..ae7c264 100644 --- a/src/simple_cnf.py +++ b/src/simple_cnf.py @@ -647,5 +647,8 @@ class SimpleCnf(object): """ key_func = lambda cnf_var_entry: cnf_var_entry['number'] + if isinstance (other_cnf, SimpleCnf) is False: + return False + return sorted(self.__cnfvars, key=key_func) \ == sorted(other_cnf.__cnfvars, key=key_func) # pylint: disable=protected-access diff --git a/test/test_simple_cnf.py b/test/test_simple_cnf.py index bdc9883..c0583d9 100755 --- a/test/test_simple_cnf.py +++ b/test/test_simple_cnf.py @@ -193,6 +193,10 @@ class SimpleCnfTest(unittest.TestCase): self.assertEqual(self.cnf['testvar'], self.cnf[61]) self.assertEqual(self.cnf, self.cnf) self.assertNotEqual(self.cnf[56], self.cnf[57]) + self.assertNotEqual(SimpleCnf ({"cnf": []}), None) + self.assertNotEqual(SimpleCnf ({"cnf": []}), 42) + self.assertNotEqual(SimpleCnf ({"cnf": []}), "got wood?") + self.assertEqual(SimpleCnf (), SimpleCnf ({"cnf": []})) def test_len(self): """ test method :py:meth:`SimpleCnf.__len__` """