mirror of
https://github.com/python/cpython.git
synced 2024-11-26 11:24:40 +08:00
47b9ff6ba1
*ordering* between objects; there is only a default equality test (defined by an object being equal to itself only). Read the comment in object.c. The current implementation never uses a three-way comparison to compute a rich comparison, but it does use a rich comparison to compute a three-way comparison. I'm not quite done ripping out all the calls to PyObject_Compare/Cmp, or replacing tp_compare implementations with tp_richcompare implementations; but much of that has happened (to make most unit tests pass). The following tests still fail, because I need help deciding or understanding: test_codeop -- depends on comparing code objects test_datetime -- need Tim Peters' opinion test_marshal -- depends on comparing code objects test_mutants -- need help understanding it The problem with test_codeop and test_marshal is this: these tests compare two different code objects and expect them to be equal. Is that still a feature we'd like to support? I've temporarily removed the comparison and hash code from code objects, so they use the default (equality by pointer only) comparison. For the other two tests, run them to see for yourself. (There may be more failing test with "-u all".) A general problem with getting lots of these tests to pass is the reality that for object types that have a natural total ordering, implementing __cmp__ is much more convenient than implementing __eq__, __ne__, __lt__, and so on. Should we go back to allowing __cmp__ to provide a total ordering? Should we provide some other way to implement rich comparison with a single method override? Alex proposed a __key__() method; I've considered a __richcmp__() method. Or perhaps __cmp__() just shouldn't be killed off...
180 lines
5.7 KiB
Python
180 lines
5.7 KiB
Python
"""A more or less complete user-defined wrapper around dictionary objects."""
|
|
|
|
class UserDict:
|
|
def __init__(self, dict=None, **kwargs):
|
|
self.data = {}
|
|
if dict is not None:
|
|
self.update(dict)
|
|
if len(kwargs):
|
|
self.update(kwargs)
|
|
def __repr__(self): return repr(self.data)
|
|
def __eq__(self, dict):
|
|
if isinstance(dict, UserDict):
|
|
return self.data == dict.data
|
|
else:
|
|
return self.data == dict
|
|
def __ne__(self, dict):
|
|
if isinstance(dict, UserDict):
|
|
return self.data != dict.data
|
|
else:
|
|
return self.data != dict
|
|
def __len__(self): return len(self.data)
|
|
def __getitem__(self, key):
|
|
if key in self.data:
|
|
return self.data[key]
|
|
if hasattr(self.__class__, "__missing__"):
|
|
return self.__class__.__missing__(self, key)
|
|
raise KeyError(key)
|
|
def __setitem__(self, key, item): self.data[key] = item
|
|
def __delitem__(self, key): del self.data[key]
|
|
def clear(self): self.data.clear()
|
|
def copy(self):
|
|
if self.__class__ is UserDict:
|
|
return UserDict(self.data.copy())
|
|
import copy
|
|
data = self.data
|
|
try:
|
|
self.data = {}
|
|
c = copy.copy(self)
|
|
finally:
|
|
self.data = data
|
|
c.update(self)
|
|
return c
|
|
def keys(self): return self.data.keys()
|
|
def items(self): return self.data.items()
|
|
def iteritems(self): return self.data.iteritems()
|
|
def iterkeys(self): return self.data.iterkeys()
|
|
def itervalues(self): return self.data.itervalues()
|
|
def values(self): return self.data.values()
|
|
def update(self, dict=None, **kwargs):
|
|
if dict is None:
|
|
pass
|
|
elif isinstance(dict, UserDict):
|
|
self.data.update(dict.data)
|
|
elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
|
|
self.data.update(dict)
|
|
else:
|
|
for k, v in dict.items():
|
|
self[k] = v
|
|
if len(kwargs):
|
|
self.data.update(kwargs)
|
|
def get(self, key, failobj=None):
|
|
if key not in self:
|
|
return failobj
|
|
return self[key]
|
|
def setdefault(self, key, failobj=None):
|
|
if key not in self:
|
|
self[key] = failobj
|
|
return self[key]
|
|
def pop(self, key, *args):
|
|
return self.data.pop(key, *args)
|
|
def popitem(self):
|
|
return self.data.popitem()
|
|
def __contains__(self, key):
|
|
return key in self.data
|
|
@classmethod
|
|
def fromkeys(cls, iterable, value=None):
|
|
d = cls()
|
|
for key in iterable:
|
|
d[key] = value
|
|
return d
|
|
|
|
class IterableUserDict(UserDict):
|
|
def __iter__(self):
|
|
return iter(self.data)
|
|
|
|
class DictMixin:
|
|
# Mixin defining all dictionary methods for classes that already have
|
|
# a minimum dictionary interface including getitem, setitem, delitem,
|
|
# and keys. Without knowledge of the subclass constructor, the mixin
|
|
# does not define __init__() or copy(). In addition to the four base
|
|
# methods, progressively more efficiency comes with defining
|
|
# __contains__(), __iter__(), and iteritems().
|
|
|
|
# second level definitions support higher levels
|
|
def __iter__(self):
|
|
for k in self.keys():
|
|
yield k
|
|
def __contains__(self, key):
|
|
try:
|
|
value = self[key]
|
|
except KeyError:
|
|
return False
|
|
return True
|
|
|
|
# third level takes advantage of second level definitions
|
|
def iteritems(self):
|
|
for k in self:
|
|
yield (k, self[k])
|
|
def iterkeys(self):
|
|
return self.__iter__()
|
|
|
|
# fourth level uses definitions from lower levels
|
|
def itervalues(self):
|
|
for _, v in self.iteritems():
|
|
yield v
|
|
def values(self):
|
|
return [v for _, v in self.iteritems()]
|
|
def items(self):
|
|
return list(self.iteritems())
|
|
def clear(self):
|
|
for key in self.keys():
|
|
del self[key]
|
|
def setdefault(self, key, default=None):
|
|
try:
|
|
return self[key]
|
|
except KeyError:
|
|
self[key] = default
|
|
return default
|
|
def pop(self, key, *args):
|
|
if len(args) > 1:
|
|
raise TypeError, "pop expected at most 2 arguments, got "\
|
|
+ repr(1 + len(args))
|
|
try:
|
|
value = self[key]
|
|
except KeyError:
|
|
if args:
|
|
return args[0]
|
|
raise
|
|
del self[key]
|
|
return value
|
|
def popitem(self):
|
|
try:
|
|
k, v = self.iteritems().next()
|
|
except StopIteration:
|
|
raise KeyError, 'container is empty'
|
|
del self[k]
|
|
return (k, v)
|
|
def update(self, other=None, **kwargs):
|
|
# Make progressively weaker assumptions about "other"
|
|
if other is None:
|
|
pass
|
|
elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
|
|
for k, v in other.iteritems():
|
|
self[k] = v
|
|
elif hasattr(other, 'keys'):
|
|
for k in other.keys():
|
|
self[k] = other[k]
|
|
else:
|
|
for k, v in other:
|
|
self[k] = v
|
|
if kwargs:
|
|
self.update(kwargs)
|
|
def get(self, key, default=None):
|
|
try:
|
|
return self[key]
|
|
except KeyError:
|
|
return default
|
|
def __repr__(self):
|
|
return repr(dict(self.iteritems()))
|
|
def __eq__(self, other):
|
|
if isinstance(other, DictMixin):
|
|
other = dict(other.iteritems())
|
|
return dict(self.iteritems()) == other
|
|
def __ne__(self, other):
|
|
if isinstance(other, DictMixin):
|
|
other = dict(other.iteritems())
|
|
return dict(self.iteritems()) != other
|
|
def __len__(self):
|
|
return len(self.keys())
|