Rev 3797: Define __contains__ for all Inventories, as otherwise __iter__ gets used for foo in bar, if __iter__ is defined. in http://people.ubuntu.com/~robertc/baz2.0/repository

Robert Collins robertc at robertcollins.net
Thu Dec 4 17:52:50 GMT 2008


At http://people.ubuntu.com/~robertc/baz2.0/repository

------------------------------------------------------------
revno: 3797
revision-id: robertc at robertcollins.net-20081204175242-jfz0abhsf0f684g3
parent: john at arbash-meinel.com-20081204170109-3g9j0j762y594bgg
committer: Robert Collins <robertc at robertcollins.net>
branch nick: repository
timestamp: Fri 2008-12-05 04:52:42 +1100
message:
  Define __contains__ for all Inventories, as otherwise __iter__ gets used for foo in bar, if __iter__ is defined.
modified:
  bzrlib/inventory.py            inventory.py-20050309040759-6648b84ca2005b37
=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py	2008-12-03 22:55:40 +0000
+++ b/bzrlib/inventory.py	2008-12-04 17:52:42 +0000
@@ -714,6 +714,23 @@
 class CommonInventory(object):
     """Basic inventory logic, defined in terms of primitives like has_id."""
 
+    def __contains__(self, file_id):
+        """True if this entry contains a file with given id.
+
+        >>> inv = Inventory()
+        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
+        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
+        >>> '123' in inv
+        True
+        >>> '456' in inv
+        False
+
+        Note that this method along with __iter__ are not encouraged for use as
+        they are less clear than specific query methods - they may be rmeoved
+        in the future.
+        """
+        return self.has_id(file_id)
+
     def id2path(self, file_id):
         """Return as a string the path to file_id.
         
@@ -1060,19 +1077,6 @@
                 descend(child_ie, child_path)
         descend(self.root, u'')
         return accum
-        
-    def __contains__(self, file_id):
-        """True if this entry contains a file with given id.
-
-        >>> inv = Inventory()
-        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
-        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
-        >>> '123' in inv
-        True
-        >>> '456' in inv
-        False
-        """
-        return (file_id in self._byid)
 
     def __getitem__(self, file_id):
         """Return the entry for given file_id.




More information about the bazaar-commits mailing list