[Pkg-bazaar-commits] ./bzr/unstable r544: - Define __eq__ and __ne__ for Inventory and InventoryEntry objects,

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:19:07 UTC 2009


------------------------------------------------------------
revno: 544
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Thu 2005-05-19 21:07:36 +1000
message:
  - Define __eq__ and __ne__ for Inventory and InventoryEntry objects,
    not __cmp__.  (Apparently this is the preferred style for modern
    Python code.)
  
  - Make those classes explicitly not hashable.
modified:
  bzrlib/inventory.py
-------------- next part --------------
=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py	2005-05-19 09:59:49 +0000
+++ b/bzrlib/inventory.py	2005-05-19 11:07:36 +0000
@@ -210,19 +210,24 @@
 
     from_element = classmethod(from_element)
 
-    def __cmp__(self, other):
-        if self is other:
-            return 0
+    def __eq__(self, other):
         if not isinstance(other, InventoryEntry):
             return NotImplemented
 
-        return cmp(self.file_id, other.file_id) \
-               or cmp(self.name, other.name) \
-               or cmp(self.text_sha1, other.text_sha1) \
-               or cmp(self.text_size, other.text_size) \
-               or cmp(self.text_id, other.text_id) \
-               or cmp(self.parent_id, other.parent_id) \
-               or cmp(self.kind, other.kind)
+        return (self.file_id == other.file_id) \
+               and (self.name == other.name) \
+               and (self.text_sha1 == other.text_sha1) \
+               and (self.text_size == other.text_size) \
+               and (self.text_id == other.text_id) \
+               and (self.parent_id == other.parent_id) \
+               and (self.kind == other.kind)
+
+
+    def __ne__(self, other):
+        return not (self == other)
+
+    def __hash__(self):
+        raise ValueError('not hashable')
 
 
 
@@ -234,13 +239,12 @@
         self.parent_id = None
         self.name = ''
 
-    def __cmp__(self, other):
-        if self is other:
-            return 0
+    def __eq__(self, other):
         if not isinstance(other, RootEntry):
             return NotImplemented
-        return cmp(self.file_id, other.file_id) \
-               or cmp(self.children, other.children)
+        
+        return (self.file_id == other.file_id) \
+               and (self.children == other.children)
 
 
 
@@ -481,7 +485,7 @@
     from_element = classmethod(from_element)
 
 
-    def __cmp__(self, other):
+    def __eq__(self, other):
         """Compare two sets by comparing their contents.
 
         >>> i1 = Inventory()
@@ -495,31 +499,23 @@
         >>> i1 == i2
         True
         """
-        if self is other:
-            return 0
-        
         if not isinstance(other, Inventory):
             return NotImplemented
 
-        byid = self._byid
-        otherids = other._byid
-
-        if len(byid) != len(otherids):
+        if len(self._byid) != len(other._byid):
             # shortcut: obviously not the same
-            return 1                    
-        
-        for file_id in byid:
-            if file_id not in otherids:
-                return 1
-            
-            c = cmp(byid[file_id], otherids[file_id])
-            if c: return c
-
-        for file_id in otherids:
-            if file_id not in byid:
-                return 1
-
-        return 0
+            return False
+
+        return self._byid == other._byid
+
+
+    def __ne__(self, other):
+        return not (self == other)
+
+
+    def __hash__(self):
+        raise ValueError('not hashable')
+
 
 
     def get_idpath(self, file_id):



More information about the Pkg-bazaar-commits mailing list