[Python-checkins] python/nondist/sandbox/sets set.py,1.7,1.8
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Mon, 19 Aug 2002 09:13:38 -0700
Update of /cvsroot/python/python/nondist/sandbox/sets
In directory usw-pr-cvs1:/tmp/cvs-serv18972
Modified Files:
set.py
Log Message:
Get rid of sort_repr altogether. Instead, for the test suite there's
an internal routine _repr() that takes an optional 'sorted' argument.
Index: set.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/sets/set.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** set.py 17 Aug 2002 12:20:50 -0000 1.7
--- set.py 19 Aug 2002 16:13:31 -0000 1.8
***************
*** 50,66 ****
"""Common base class for mutable and immutable sets."""
! # Constructor
! def __init__(self, seq=None, sort_repr=True):
! """Construct a set, optionally initializing it from a sequence.
! Unless the optional keyword argument sort_repr is False, the
! set's elements are displayed in sorted order. This slows down
! string conversions, but is generally more user-friendly. The
! option to turn off this behavior is provided so that you can
! create sets whose elements are not ordered; for example,
! complex numbers.
! """
! self._sort_repr = sort_repr
self._data = {}
if seq is not None:
--- 50,59 ----
"""Common base class for mutable and immutable sets."""
! __slots__ = ['_data']
! # Constructor
! def __init__(self, seq=None):
! """Construct a set, optionally initializing it from a sequence."""
self._data = {}
if seq is not None:
***************
*** 87,98 ****
This looks like 'Set([<list of elements>])'.
"""
! elements = self._data.keys()
! if self._sort_repr:
! elements.sort()
! return '%s(%r)' % (self.__class__.__name__, elements)
# __str__ is the same as __repr__
__str__ = __repr__
def __iter__(self):
"""Return an iterator over the elements or a set.
--- 80,94 ----
This looks like 'Set([<list of elements>])'.
"""
! return self._repr()
# __str__ is the same as __repr__
__str__ = __repr__
+ def _repr(self, sorted=False):
+ elements = self._data.keys()
+ if sorted:
+ elements.sort()
+ return '%s(%r)' % (self.__class__.__name__, elements)
+
def __iter__(self):
"""Return an iterator over the elements or a set.
***************
*** 133,137 ****
def copy(self):
"""Return a shallow copy of a set."""
! return self.__class__(self, sort_repr=self._sort_repr)
__copy__ = copy # For the copy module
--- 129,133 ----
def copy(self):
"""Return a shallow copy of a set."""
! return self.__class__(self)
__copy__ = copy # For the copy module
***************
*** 145,149 ****
# itself.
from copy import deepcopy
! result = self.__class__([], self._sort_repr)
memo[id(self)] = result
data = result._data
--- 141,145 ----
# itself.
from copy import deepcopy
! result = self.__class__([])
memo[id(self)] = result
data = result._data
***************
*** 161,165 ****
"""
self._binary_sanity_check(other)
! result = self.__class__(self._data, self._sort_repr)
result._data.update(other._data)
return result
--- 157,161 ----
"""
self._binary_sanity_check(other)
! result = self.__class__(self._data)
result._data.update(other._data)
return result
***************
*** 177,181 ****
else:
little, big = other, self
! result = self.__class__([], self._sort_repr)
data = result._data
value = True
--- 173,177 ----
else:
little, big = other, self
! result = self.__class__([])
data = result._data
value = True
***************
*** 193,197 ****
"""
self._binary_sanity_check(other)
! result = self.__class__([], self._sort_repr)
data = result._data
value = True
--- 189,193 ----
"""
self._binary_sanity_check(other)
! result = self.__class__([])
data = result._data
value = True
***************
*** 212,216 ****
"""
self._binary_sanity_check(other)
! result = self.__class__([], self._sort_repr)
data = result._data
value = True
--- 208,212 ----
"""
self._binary_sanity_check(other)
! result = self.__class__([])
data = result._data
value = True
***************
*** 278,297 ****
"""Immutable set class."""
! # BaseSet + hashing
!
! _hashcode = None
! def __init__(self, seq, sort_repr=True):
! """Construct an immutable set from a sequence.
! Unless the optional keyword argument sort_repr is False, the
! set's elements are displayed in sorted order. This slows down
! string conversions, but is generally more user-friendly. The
! option to turn off this behavior is provided so that you can
! create sets whose elements are not ordered; for example,
! complex numbers.
! """
# Override the constructor to make 'seq' a required argument
! BaseSet.__init__(self, seq, sort_repr)
def __hash__(self):
--- 274,286 ----
"""Immutable set class."""
! __slots__ = ['_hash']
! # BaseSet + hashing
! def __init__(self, seq):
! """Construct an immutable set from a sequence."""
# Override the constructor to make 'seq' a required argument
! BaseSet.__init__(self, seq)
! self._hashcode = None
def __hash__(self):
***************
*** 304,307 ****
--- 293,298 ----
""" Mutable set class."""
+ __slots__ = []
+
# BaseSet + operations requiring mutability; no hashing
***************
*** 456,468 ****
# 3-element set
blue = Set([0, 1, 2])
! assert `blue` == "Set([0, 1, 2])", "3-element set: %s" % `blue`
# 2-element set with other values
black = Set([0, 5])
! assert `black` == "Set([0, 5])", "2-element set: %s" % `black`
# All elements from all sets
white = Set([0, 1, 2, 5])
! assert `white` == "Set([0, 1, 2, 5])", "4-element set: %s" % `white`
# Add element to empty set
--- 447,459 ----
# 3-element set
blue = Set([0, 1, 2])
! assert blue._repr(True) == "Set([0, 1, 2])", "3-element set: %s" % `blue`
# 2-element set with other values
black = Set([0, 5])
! assert black._repr(True) == "Set([0, 5])", "2-element set: %s" % `black`
# All elements from all sets
white = Set([0, 1, 2, 5])
! assert white._repr(True) == "Set([0, 1, 2, 5])", "4-element set: %s" % `white`
# Add element to empty set