[Python-checkins] python/dist/src/Lib sets.py,1.44.8.1,1.44.8.2

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sun Aug 17 16:09:00 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv27709/Lib

Modified Files:
      Tag: release23-maint
	sets.py 
Log Message:
Backport improvements to set.py so that the interface will remain
consistent across versions. 

* Relaxed the argument restrictions for non-operator methods.  They now
  allow any iterable instead of requiring a set.  This makes the module
  a little easier to use and paves the way for an efficient C 
  implementation which can take better advantage of iterable arguments
  while screening out immutables.

* Added a PendingDeprecationWarning for Set.update() because it now 
  duplicates Set.union_update()

* Adapted the tests and docs to include the above changes.

* Added more test coverage including testing identities and checking
  to make sure non-restartable generators work as arguments.



Index: sets.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sets.py,v
retrieving revision 1.44.8.1
retrieving revision 1.44.8.2
diff -C2 -d -r1.44.8.1 -r1.44.8.2
*** sets.py	15 Aug 2003 21:14:51 -0000	1.44.8.1
--- sets.py	17 Aug 2003 22:08:58 -0000	1.44.8.2
***************
*** 197,204 ****
          if not isinstance(other, BaseSet):
              return NotImplemented
!         result = self.__class__()
!         result._data = self._data.copy()
!         result._data.update(other._data)
!         return result
  
      def union(self, other):
--- 197,201 ----
          if not isinstance(other, BaseSet):
              return NotImplemented
!         return self.union(other)
  
      def union(self, other):
***************
*** 207,211 ****
          (I.e. all elements that are in either set.)
          """
!         return self | other
  
      def __and__(self, other):
--- 204,210 ----
          (I.e. all elements that are in either set.)
          """
!         result = self.__class__(self)
!         result._update(other)
!         return result
  
      def __and__(self, other):
***************
*** 216,225 ****
          if not isinstance(other, BaseSet):
              return NotImplemented
!         if len(self) <= len(other):
!             little, big = self, other
!         else:
!             little, big = other, self
!         common = ifilter(big._data.has_key, little)
!         return self.__class__(common)
  
      def intersection(self, other):
--- 215,219 ----
          if not isinstance(other, BaseSet):
              return NotImplemented
!         return self.intersection(other)
  
      def intersection(self, other):
***************
*** 228,232 ****
          (I.e. all elements that are in both sets.)
          """
!         return self & other
  
      def __xor__(self, other):
--- 222,233 ----
          (I.e. all elements that are in both sets.)
          """
!         if not isinstance(other, BaseSet):
!             other = Set(other)
!         if len(self) <= len(other):
!             little, big = self, other
!         else:
!             little, big = other, self
!         common = ifilter(big._data.has_key, little)
!         return self.__class__(common)
  
      def __xor__(self, other):
***************
*** 237,245 ****
          if not isinstance(other, BaseSet):
              return NotImplemented
          result = self.__class__()
          data = result._data
          value = True
          selfdata = self._data
!         otherdata = other._data
          for elt in ifilterfalse(otherdata.has_key, selfdata):
              data[elt] = value
--- 238,256 ----
          if not isinstance(other, BaseSet):
              return NotImplemented
+         return self.symmetric_difference(other)
+ 
+     def symmetric_difference(self, other):
+         """Return the symmetric difference of two sets as a new set.
+ 
+         (I.e. all elements that are in exactly one of the sets.)
+         """
          result = self.__class__()
          data = result._data
          value = True
          selfdata = self._data
!         try:
!             otherdata = other._data
!         except AttributeError:
!             otherdata = Set(other)._data
          for elt in ifilterfalse(otherdata.has_key, selfdata):
              data[elt] = value
***************
*** 248,258 ****
          return result
  
-     def symmetric_difference(self, other):
-         """Return the symmetric difference of two sets as a new set.
- 
-         (I.e. all elements that are in exactly one of the sets.)
-         """
-         return self ^ other
- 
      def  __sub__(self, other):
          """Return the difference of two sets as a new Set.
--- 259,262 ----
***************
*** 262,271 ****
          if not isinstance(other, BaseSet):
              return NotImplemented
!         result = self.__class__()
!         data = result._data
!         value = True
!         for elt in ifilterfalse(other._data.has_key, self):
!             data[elt] = value
!         return result
  
      def difference(self, other):
--- 266,270 ----
          if not isinstance(other, BaseSet):
              return NotImplemented
!         return self.difference(other)
  
      def difference(self, other):
***************
*** 274,278 ****
          (I.e. all elements that are in this set and not in the other.)
          """
!         return self - other
  
      # Membership test
--- 273,286 ----
          (I.e. all elements that are in this set and not in the other.)
          """
!         result = self.__class__()
!         data = result._data
!         try:
!             otherdata = other._data
!         except AttributeError:
!             otherdata = Set(other)._data
!         value = True
!         for elt in ifilterfalse(otherdata.has_key, self):
!             data[elt] = value
!         return result
  
      # Membership test
***************
*** 442,446 ****
      def union_update(self, other):
          """Update a set with the union of itself and another."""
!         self |= other
  
      def __iand__(self, other):
--- 450,454 ----
      def union_update(self, other):
          """Update a set with the union of itself and another."""
!         self._update(other)
  
      def __iand__(self, other):
***************
*** 452,462 ****
      def intersection_update(self, other):
          """Update a set with the intersection of itself and another."""
!         self &= other
  
      def __ixor__(self, other):
          """Update a set with the symmetric difference of itself and another."""
          self._binary_sanity_check(other)
          data = self._data
          value = True
          for elt in other:
              if elt in data:
--- 460,480 ----
      def intersection_update(self, other):
          """Update a set with the intersection of itself and another."""
!         if isinstance(other, BaseSet):
!             self &= other
!         else:
!             self._data = (self.intersection(other))._data
  
      def __ixor__(self, other):
          """Update a set with the symmetric difference of itself and another."""
          self._binary_sanity_check(other)
+         self.symmetric_difference_update(other)
+         return self
+ 
+     def symmetric_difference_update(self, other):
+         """Update a set with the symmetric difference of itself and another."""
          data = self._data
          value = True
+         if not isinstance(other, BaseSet):
+             other = Set(other)
          for elt in other:
              if elt in data:
***************
*** 464,484 ****
              else:
                  data[elt] = value
-         return self
- 
-     def symmetric_difference_update(self, other):
-         """Update a set with the symmetric difference of itself and another."""
-         self ^= other
  
      def __isub__(self, other):
          """Remove all elements of another set from this set."""
          self._binary_sanity_check(other)
!         data = self._data
!         for elt in ifilter(data.has_key, other):
!             del data[elt]
          return self
  
      def difference_update(self, other):
          """Remove all elements of another set from this set."""
!         self -= other
  
      # Python dict-like mass mutations: update, clear
--- 482,499 ----
              else:
                  data[elt] = value
  
      def __isub__(self, other):
          """Remove all elements of another set from this set."""
          self._binary_sanity_check(other)
!         self.difference_update(other)
          return self
  
      def difference_update(self, other):
          """Remove all elements of another set from this set."""
!         data = self._data
!         if not isinstance(other, BaseSet):
!             other = Set(other)
!         for elt in ifilter(data.has_key, other):
!             del data[elt]
  
      # Python dict-like mass mutations: update, clear
***************
*** 486,489 ****
--- 501,508 ----
      def update(self, iterable):
          """Add all values from an iterable (such as a list or file)."""
+         import warnings
+         warnings.warn("The update() method is going to be deprecated; "
+                       "Use union_update() instead",
+                       PendingDeprecationWarning)
          self._update(iterable)
  





More information about the Python-checkins mailing list