[Zope-CVS] CVS: Packages/pypes/pypes - extent.py:1.6

Casey Duncan casey at zope.com
Sun Feb 22 01:10:02 EST 2004


Update of /cvs-repository/Packages/pypes/pypes
In directory cvs.zope.org:/tmp/cvs-serv11264

Modified Files:
	extent.py 
Log Message:
Finish implementing extent to identity set operations and add integration tests


=== Packages/pypes/pypes/extent.py 1.5 => 1.6 ===
--- Packages/pypes/pypes/extent.py:1.5	Mon Feb 16 00:54:03 2004
+++ Packages/pypes/pypes/extent.py	Sun Feb 22 01:09:31 2004
@@ -19,6 +19,7 @@
 
 from types import ClassType
 from sets import Set
+from itertools import chain
 from zope.interface import implements, directlyProvides
 from persistent import Persistent
 from BTrees.OOBTree import OOBTree, OOTreeSet, union, difference, intersection
@@ -99,14 +100,9 @@
                 directlyProvides(extent, IDerivedExtent)
                 return extent
             else:
-                return identity.multiunion(self._listSets(self, other))
-        elif isinstance(other, IdentitySet):
-            return identity.multiunion(self._listSets(self) + other)
+                return identity.multiUnion(self._iterSets(self, other))
         else:
-            # Assume other is a sequence of identified objects
-            set = identity.multiunion(self._listSets(self))
-            set.update(other)
-            return set
+            return self.identitySet().union(other)
     
     __or__ = union
     
@@ -119,14 +115,9 @@
                 directlyProvides(extent, IDerivedExtent)
                 return extent
             else:
-                return (identity.multiunion(self._listSets(self))
-                        - identity.multiunion(self._listSets(other)))
-        elif isinstance(other, IdentitySet):
-            return identity.multiunion(self._listSets(self)) - other
+                return self.identitySet() - other.identitySet()
         else:
-            # Assume other is a sequence of identified objects
-            return (identity.multiunion(self._listSets(self))
-                    - IdentitySet(other))
+            return self.identitySet() - other
     
     __sub__ = difference
     
@@ -139,14 +130,9 @@
                 directlyProvides(extent, IDerivedExtent)
                 return extent
             else:
-                return identity.multiintersection(self._listSets(self, other))
-        elif isinstance(other, IdentitySet):
-            return identity.multiintersection(self._listSets(self) + other)
+                return self.identitySet().intersection(other.identitySet())
         else:
-            # Assume other is a sequence of identified objects
-            sets = self._listSets(self)
-            sets.append(IdentitySet(other))
-            return identity.multiintersection(sets)
+            return self.identitySet().intersection(other)
     
     __and__ = intersection
     
@@ -158,28 +144,16 @@
         return extent
     
     def issubset(self, other):
-        if isinstance(other, Extent):
-            if self._instances is other._instances:
-                return not difference(self._qualifiers, other._qualifiers)
-            else:
-                self_set = identity.multiintersection(self._listSets(self))
-                other_set = identity.multiintersection(self._listSets(other))
-                return self_set.issubset(other_set)
+        if isinstance(other, Extent) and self._instances is other._instances:
+            return not difference(self._qualifiers, other._qualifiers)
         else:
-            self_set = identity.multiintersection(self._listSets(self))
-            return self_set.issubset(other)
+            return self.identitySet().issubset(other)
     
     def issuperset(self, other):
-        if isinstance(other, Extent):
-            if self._instances is other._instances:
-                return not difference(other._qualifiers, self._qualifiers)
-            else:
-                self_set = identity.multiintersection(self._listSets(self))
-                other_set = identity.multiintersection(self._listSets(other))
-                return self_set.issuperset(other_set)
+        if isinstance(other, Extent) and self._instances is other._instances:
+            return not difference(other._qualifiers, self._qualifiers)
         else:
-            self_set = identity.multiintersection(self._listSets(self))
-            return self_set.issuperset(other)
+            return self.identitySet().issuperset(other)
     
     def __eq__(self, other):
         if isinstance(other, Extent):
@@ -187,38 +161,27 @@
                 return not (difference(self._qualifiers, other._qualifiers) or
                             difference(other._qualifiers, self._qualifiers))
             else:
-               self_set = identity.multiintersection(self._listSets(self))
-               other_set = identity.multiintersection(self._listSets(other))
-               return self_set == other_set
+               return self.identitySet() == other.identitySet()
         else:
-            self_set = identity.multiintersection(self._listSets(self))
-            return self_set == other
+            return self.identitySet() == other
     
     def __ne__(self, other):
-        if isinstance(other, Extent):
-            if self._instances is other._instances:
-                return (difference(self._qualifiers, other._qualifiers) or
-                        difference(other._qualifiers, self._qualifiers))
-            else:
-               self_set = identity.multiintersection(self._listSets(self))
-               other_set = identity.multiintersection(self._listSets(other))
-               return self_set != other_set
-        else:
-            self_set = identity.multiintersection(self._listSets(self))
-            return self_set != other
+        return not self.__eq__(other)
                          
     ## Helper Methods ##
            
-    def _listSets(self, *extents):
-        """Return a list of identity sets that comprise extents"""
-        sets = []
+    def _iterSets(self, *extents):
+        """Return an iterator of identity sets that comprise extents"""
         for extent in extents:
             for key in extent._qualifiers:
                 try:
-                    sets.append(self._instances[key])
+                    yield self._instances[key]
                 except KeyError:
                     pass # No instances for that type
-        return sets
+    
+    def identitySet(self):
+        """Return an identity set with the same members as the extent"""
+        return identity.multiUnion(self._iterSets(self))
                         
 
 class ClassExtentMap(Persistent):
@@ -236,6 +199,7 @@
         self._subclasses = OOBTree() # Map class key => set of subclass keys
         self._instances = OOBTree() # Map class key => id set of instances
         listenForIdEvents(self, dbconn)
+        self._p_jar = dbconn # Allow service access before commit
         #self.update(dbconn)
     
     def __getitem__(self, key):
@@ -276,6 +240,8 @@
         except KeyError:
             # Instance of class not yet seen
             instances = self._instances[key] = IdentitySet()
+            # Allow service access by instances set before commit
+            instances._p_jar = getattr(self, '_p_jar', None)
             self._addClass(cls)
         instances.add(obj)
     




More information about the Zope-CVS mailing list