"""A more or less complete user-defined wrapper around dictionary objects."""
def __init__(*args, **kwargs):
raise TypeError("descriptor '__init__' of 'UserDict' object "
raise TypeError('expected at most 1 arguments, got %d' % len(args))
dict = kwargs.pop('dict')
warnings.warn("Passing 'dict' as keyword argument is "
"deprecated", PendingDeprecationWarning,
def __repr__(self): return repr(self.data)
if isinstance(dict, UserDict):
return cmp(self.data, dict.data)
return cmp(self.data, dict)
__hash__ = None # Avoid Py3k warning
def __len__(self): return len(self.data)
def __getitem__(self, key):
if hasattr(self.__class__, "__missing__"):
return self.__class__.__missing__(self, key)
def __setitem__(self, key, item): self.data[key] = item
def __delitem__(self, key): del self.data[key]
def clear(self): self.data.clear()
if self.__class__ is UserDict:
return UserDict(self.data.copy())
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 has_key(self, key): return key in self.data
def update(*args, **kwargs):
raise TypeError("descriptor 'update' of 'UserDict' object "
raise TypeError('expected at most 1 arguments, got %d' % len(args))
dict = kwargs.pop('dict')
warnings.warn("Passing 'dict' as keyword argument is deprecated",
PendingDeprecationWarning, stacklevel=2)
elif isinstance(dict, UserDict):
self.data.update(dict.data)
elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
for k, v in dict.items():
def get(self, key, failobj=None):
def setdefault(self, key, failobj=None):
def pop(self, key, *args):
return self.data.pop(key, *args)
return self.data.popitem()
def __contains__(self, key):
def fromkeys(cls, iterable, value=None):
class IterableUserDict(UserDict):
_abcoll.MutableMapping.register(IterableUserDict)
# 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 __contains__(self, key):
# third level takes advantage of second level definitions
# fourth level uses definitions from lower levels
for _, v in self.iteritems():
return [v for _, v in self.iteritems()]
return list(self.iteritems())
def setdefault(self, key, default=None):
def pop(self, key, *args):
raise TypeError, "pop expected at most 2 arguments, got "\
k, v = self.iteritems().next()
raise KeyError, 'container is empty'
def update(self, other=None, **kwargs):
# Make progressively weaker assumptions about "other"
elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
for k, v in other.iteritems():
elif hasattr(other, 'keys'):
def get(self, key, default=None):
return repr(dict(self.iteritems()))
def __cmp__(self, other):
if isinstance(other, DictMixin):
other = dict(other.iteritems())
return cmp(dict(self.iteritems()), other)