[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Container - ContainerTraversable.py:1.1.2.8 ContainerTraverser.py:1.1.2.7 IContainer.py:1.1.2.9 SampleContainer.py:1.1.2.9

Jim Fulton jim@zope.com
Mon, 10 Jun 2002 15:34:47 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Container
In directory cvs.zope.org:/tmp/cvs-serv5490/lib/python/Zope/App/OFS/Container

Modified Files:
      Tag: Zope-3x-branch
	ContainerTraversable.py ContainerTraverser.py IContainer.py 
	SampleContainer.py 
Log Message:
Implemented 

http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/IContainerPythonification

Along the way:

- Converted most uses of has_key to use in.

- Fixed a bug in Interface names and namesAndDescriptions methods 
  that caused base class attributes to be missed.



=== Zope3/lib/python/Zope/App/OFS/Container/ContainerTraversable.py 1.1.2.7 => 1.1.2.8 ===
 
 class ContainerTraversable:
-    """Traverses containers via getattr and getObject.
-    
-       If the name to traverse to includes a ';', the name
-       will be treated as a name and namespace.
-       
-       The allowed namespaces are:
-       
-           method   traverse using getattr only
-           content  traverse using getObject only
-           
-       If no namespace is given, try getattr and then getObject.
-       
+    """Traverses containers via getattr and get.
     """
 
     __implements__ = ITraversable
@@ -53,7 +42,7 @@
 
         container = self._container
         
-        v = container.getObject(name, _marker)
+        v = container.get(name, _marker)
         if v is _marker:
             v = getattr(container, name, _marker)
             if v is _marker:            


=== Zope3/lib/python/Zope/App/OFS/Container/ContainerTraverser.py 1.1.2.6 => 1.1.2.7 ===
         c = self.context
 
-        subob = c.getObject(name, None)
+        subob = c.get(name, None)
         if subob is None:
 
             view = queryView(c, name, request)


=== Zope3/lib/python/Zope/App/OFS/Container/IContainer.py 1.1.2.8 => 1.1.2.9 ===
 ##############################################################################
 from Interface import Interface
+from Interface.Common.Mapping import IEnumerableMapping
 
-
-
-_RAISE_KEYERROR = []
-
-
-class IReadContainer(Interface):
+class IReadContainer(IEnumerableMapping):
     """Readable content containers
 
        For all methods that return a sequence of values, the return
@@ -34,68 +30,26 @@
 
        """
 
-    def objectIds():
-        """Return a sequence of ids used in the container
-
-           Return a sequence-like object containing the names of the
-           objects that appear in the container.
-
-           """
-
-    def objectValues():
-        """Return a sequence of the values in the container
-
-           Return a sequence-like object containing the objects that
-           appear in the container.
-
-           """
-
-    def objectItems():
-        """Return a sequence of id-object pairs
-
-           Return a sequence-like object containing tuples of the form
-           (name, object) for the objects that appear in the container.
-
-           """
-
-    def getObject(name, default=_RAISE_KEYERROR):
-        """Return the named object
-
-        Return the named object, or the value of the default argument
-        if given and the named object is not found. If no default is
-        given and the object is not found a KeyError is raised.
-
-        """
-
-    def hasObject(name):
-        """Return true if the named object appears in the container."""
-
-    def objectCount():
-        """Return the number of objects in the container."""
-
-
 class IWriteContainer(Interface):
     """An interface for the write aspects of a container."""
 
-    def setObject(name, object):
-        """Add the given object to the container under the given name.
-        
-        Raises a ValueError if name is an empty string.
-        
-        Raises a Zope.App.OFS.Container.Exceptions.UnaddableError
-        if the item cannot be added.
+    def setObject(key, object):
+        """Add the given object to the container under the given key.
+
+        Raises a ValueError if key is an empty string.
+
+        Returns the key used, which might be different than the given key
         """
 
-    def delObject(name):
-        """Delete the named object from the container.
+    def __delitem__(key):
+        """Delete the keyd object from the container.
 
-        TheRaises a KeyError if the object is not found.
+        Raises a KeyError if the object is not found.
 
         """
 
 class IContainer(IReadContainer, IWriteContainer):
     """Readable and writable content container."""
-
 
 class IHomogenousContainer(Interface):
 


=== Zope3/lib/python/Zope/App/OFS/Container/SampleContainer.py 1.1.2.8 => 1.1.2.9 ===
     # Zope.App.OFS.Container.IContainer
 
-    def objectIds(self):
+    def keys(self):
         '''See interface IReadContainer'''
         return self.__data.keys()
 
-    def getObject(self, name, default=KeyError):
+    def __iter__(self):
+        return iter(self.__data.keys())
+
+    def __getitem__(self, key):
+        '''See interface IReadContainer'''
+        return self.__data[key]
+
+    def get(self, key, default=None):
         '''See interface IReadContainer'''
-        v = self.__data.get(name, default)
-        if v is KeyError:
-            raise KeyError, name
-        return v
+        return self.__data.get(key, default)
 
-    def objectValues(self):
+    def values(self):
         '''See interface IReadContainer'''
         return self.__data.values()
 
-    def objectCount(self):
+    def __len__(self):
         '''See interface IReadContainer'''
         return len(self.__data)
 
-    def objectItems(self):
+    def items(self):
         '''See interface IReadContainer'''
         return self.__data.items()
 
-    def hasObject(self, name):
+    def __contains__(self, key):
         '''See interface IReadContainer'''
-        return self.__data.has_key(name)
+        return self.__data.has_key(key)
+
+    has_key = __contains__
 
-    def setObject(self, name, object):
+    def setObject(self, key, object):
         '''See interface IWriteContainer'''
-        if type(name) in StringTypes and len(name)==0:
+        if type(key) in StringTypes and len(key)==0:
             raise ValueError("The id cannot be an empty string")
-        self.__data[name] = object
+        self.__data[key] = object
+        return key
 
-    def delObject(self, name):
+    def __delitem__(self, key):
         '''See interface IWriteContainer'''
-        del self.__data[name]
+        del self.__data[key]
 
     #
     ############################################################