2000-02-02 11:10:15 -04:00
|
|
|
"""A more or less complete user-defined wrapper around dictionary objects."""
|
1993-11-30 09:43:54 -04:00
|
|
|
|
2010-06-22 15:09:02 -03:00
|
|
|
class UserDict:
|
2015-09-29 17:33:03 -03:00
|
|
|
def __init__(*args, **kwargs):
|
|
|
|
if not args:
|
|
|
|
raise TypeError("descriptor '__init__' of 'UserDict' object "
|
|
|
|
"needs an argument")
|
|
|
|
self = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
if len(args) > 1:
|
|
|
|
raise TypeError('expected at most 1 arguments, got %d' % len(args))
|
|
|
|
if args:
|
|
|
|
dict = args[0]
|
|
|
|
elif 'dict' in kwargs:
|
|
|
|
dict = kwargs.pop('dict')
|
|
|
|
import warnings
|
|
|
|
warnings.warn("Passing 'dict' as keyword argument is "
|
|
|
|
"deprecated", PendingDeprecationWarning,
|
|
|
|
stacklevel=2)
|
|
|
|
else:
|
|
|
|
dict = None
|
2002-11-27 04:29:11 -04:00
|
|
|
self.data = {}
|
2002-11-21 20:07:40 -04:00
|
|
|
if dict is not None:
|
2002-11-27 04:29:11 -04:00
|
|
|
self.update(dict)
|
|
|
|
if len(kwargs):
|
|
|
|
self.update(kwargs)
|
1997-06-03 11:10:01 -03:00
|
|
|
def __repr__(self): return repr(self.data)
|
|
|
|
def __cmp__(self, dict):
|
1999-03-26 11:31:12 -04:00
|
|
|
if isinstance(dict, UserDict):
|
1998-03-26 17:13:24 -04:00
|
|
|
return cmp(self.data, dict.data)
|
1999-03-26 11:31:12 -04:00
|
|
|
else:
|
|
|
|
return cmp(self.data, dict)
|
2010-01-27 22:18:25 -04:00
|
|
|
__hash__ = None # Avoid Py3k warning
|
1997-06-03 11:10:01 -03:00
|
|
|
def __len__(self): return len(self.data)
|
2006-02-25 18:38:04 -04:00
|
|
|
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)
|
1997-06-03 11:10:01 -03:00
|
|
|
def __setitem__(self, key, item): self.data[key] = item
|
|
|
|
def __delitem__(self, key): del self.data[key]
|
1999-03-26 11:31:12 -04:00
|
|
|
def clear(self): self.data.clear()
|
1997-06-03 11:10:01 -03:00
|
|
|
def copy(self):
|
1999-03-26 11:31:12 -04:00
|
|
|
if self.__class__ is UserDict:
|
2003-12-21 18:19:08 -04:00
|
|
|
return UserDict(self.data.copy())
|
1998-03-26 17:13:24 -04:00
|
|
|
import copy
|
2001-11-05 13:40:48 -04:00
|
|
|
data = self.data
|
|
|
|
try:
|
|
|
|
self.data = {}
|
|
|
|
c = copy.copy(self)
|
|
|
|
finally:
|
|
|
|
self.data = data
|
|
|
|
c.update(self)
|
|
|
|
return c
|
1997-06-03 11:10:01 -03:00
|
|
|
def keys(self): return self.data.keys()
|
|
|
|
def items(self): return self.data.items()
|
2001-05-03 01:54:41 -03:00
|
|
|
def iteritems(self): return self.data.iteritems()
|
|
|
|
def iterkeys(self): return self.data.iterkeys()
|
|
|
|
def itervalues(self): return self.data.itervalues()
|
1997-06-03 11:10:01 -03:00
|
|
|
def values(self): return self.data.values()
|
2008-02-21 15:24:53 -04:00
|
|
|
def has_key(self, key): return key in self.data
|
2015-09-29 17:33:03 -03:00
|
|
|
def update(*args, **kwargs):
|
|
|
|
if not args:
|
|
|
|
raise TypeError("descriptor 'update' of 'UserDict' object "
|
|
|
|
"needs an argument")
|
|
|
|
self = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
if len(args) > 1:
|
|
|
|
raise TypeError('expected at most 1 arguments, got %d' % len(args))
|
|
|
|
if args:
|
|
|
|
dict = args[0]
|
|
|
|
elif 'dict' in kwargs:
|
|
|
|
dict = kwargs.pop('dict')
|
|
|
|
import warnings
|
|
|
|
warnings.warn("Passing 'dict' as keyword argument is deprecated",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
|
|
else:
|
|
|
|
dict = None
|
2004-03-04 04:25:44 -04:00
|
|
|
if dict is None:
|
|
|
|
pass
|
|
|
|
elif isinstance(dict, UserDict):
|
1999-03-26 11:31:12 -04:00
|
|
|
self.data.update(dict.data)
|
2004-03-04 04:25:44 -04:00
|
|
|
elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
|
1999-03-26 11:31:12 -04:00
|
|
|
self.data.update(dict)
|
1998-03-26 17:13:24 -04:00
|
|
|
else:
|
1999-03-26 11:31:12 -04:00
|
|
|
for k, v in dict.items():
|
2001-06-17 22:09:41 -03:00
|
|
|
self[k] = v
|
2004-03-04 04:25:44 -04:00
|
|
|
if len(kwargs):
|
|
|
|
self.data.update(kwargs)
|
1997-10-06 14:50:04 -03:00
|
|
|
def get(self, key, failobj=None):
|
2008-02-21 14:18:37 -04:00
|
|
|
if key not in self:
|
2001-06-17 22:09:41 -03:00
|
|
|
return failobj
|
|
|
|
return self[key]
|
2000-08-08 13:12:54 -03:00
|
|
|
def setdefault(self, key, failobj=None):
|
2008-02-21 15:24:53 -04:00
|
|
|
if key not in self:
|
2001-06-17 22:09:41 -03:00
|
|
|
self[key] = failobj
|
|
|
|
return self[key]
|
2003-03-06 19:54:28 -04:00
|
|
|
def pop(self, key, *args):
|
|
|
|
return self.data.pop(key, *args)
|
2000-12-12 18:06:00 -04:00
|
|
|
def popitem(self):
|
|
|
|
return self.data.popitem()
|
2001-04-21 06:13:15 -03:00
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self.data
|
2005-01-15 20:16:11 -04:00
|
|
|
@classmethod
|
2002-11-27 04:29:11 -04:00
|
|
|
def fromkeys(cls, iterable, value=None):
|
|
|
|
d = cls()
|
|
|
|
for key in iterable:
|
|
|
|
d[key] = value
|
|
|
|
return d
|
2001-08-07 14:40:42 -03:00
|
|
|
|
|
|
|
class IterableUserDict(UserDict):
|
2001-04-21 06:13:15 -03:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.data)
|
2002-11-15 02:46:14 -04:00
|
|
|
|
2008-06-07 13:16:12 -03:00
|
|
|
import _abcoll
|
|
|
|
_abcoll.MutableMapping.register(IterableUserDict)
|
|
|
|
|
|
|
|
|
2002-11-15 02:46:14 -04:00
|
|
|
class DictMixin:
|
2002-11-18 00:34:10 -04:00
|
|
|
# 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
|
2003-01-30 23:30:09 -04:00
|
|
|
# methods, progressively more efficiency comes with defining
|
2002-11-18 00:34:10 -04:00
|
|
|
# __contains__(), __iter__(), and iteritems().
|
2002-11-15 02:46:14 -04:00
|
|
|
|
2002-11-18 00:34:10 -04:00
|
|
|
# second level definitions support higher levels
|
|
|
|
def __iter__(self):
|
|
|
|
for k in self.keys():
|
|
|
|
yield k
|
2002-11-15 02:46:14 -04:00
|
|
|
def has_key(self, key):
|
|
|
|
try:
|
2010-02-06 18:59:15 -04:00
|
|
|
self[key]
|
2002-11-15 02:46:14 -04:00
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
return True
|
2003-01-21 21:39:06 -04:00
|
|
|
def __contains__(self, key):
|
|
|
|
return self.has_key(key)
|
2002-11-15 02:46:14 -04:00
|
|
|
|
2002-11-18 00:34:10 -04:00
|
|
|
# third level takes advantage of second level definitions
|
2002-11-15 02:46:14 -04:00
|
|
|
def iteritems(self):
|
|
|
|
for k in self:
|
|
|
|
yield (k, self[k])
|
2003-01-21 21:39:06 -04:00
|
|
|
def iterkeys(self):
|
|
|
|
return self.__iter__()
|
2002-11-15 02:46:14 -04:00
|
|
|
|
2002-11-18 00:34:10 -04:00
|
|
|
# fourth level uses definitions from lower levels
|
2002-11-15 02:46:14 -04:00
|
|
|
def itervalues(self):
|
|
|
|
for _, v in self.iteritems():
|
|
|
|
yield v
|
|
|
|
def values(self):
|
2002-11-18 00:34:10 -04:00
|
|
|
return [v for _, v in self.iteritems()]
|
2002-11-15 02:46:14 -04:00
|
|
|
def items(self):
|
|
|
|
return list(self.iteritems())
|
|
|
|
def clear(self):
|
|
|
|
for key in self.keys():
|
|
|
|
del self[key]
|
2004-05-27 06:41:04 -03:00
|
|
|
def setdefault(self, key, default=None):
|
2002-11-18 00:34:10 -04:00
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
2002-11-15 02:46:14 -04:00
|
|
|
self[key] = default
|
2002-11-18 00:34:10 -04:00
|
|
|
return default
|
2003-03-06 19:54:28 -04:00
|
|
|
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
|
2002-11-15 02:46:14 -04:00
|
|
|
del self[key]
|
|
|
|
return value
|
|
|
|
def popitem(self):
|
|
|
|
try:
|
|
|
|
k, v = self.iteritems().next()
|
|
|
|
except StopIteration:
|
2002-11-18 00:34:10 -04:00
|
|
|
raise KeyError, 'container is empty'
|
2002-11-15 02:46:14 -04:00
|
|
|
del self[k]
|
|
|
|
return (k, v)
|
2004-03-04 04:25:44 -04:00
|
|
|
def update(self, other=None, **kwargs):
|
2002-11-18 00:34:10 -04:00
|
|
|
# Make progressively weaker assumptions about "other"
|
2004-03-04 04:25:44 -04:00
|
|
|
if other is None:
|
|
|
|
pass
|
|
|
|
elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
|
2002-11-18 00:34:10 -04:00
|
|
|
for k, v in other.iteritems():
|
|
|
|
self[k] = v
|
2004-03-04 04:25:44 -04:00
|
|
|
elif hasattr(other, 'keys'):
|
2002-11-18 00:34:10 -04:00
|
|
|
for k in other.keys():
|
|
|
|
self[k] = other[k]
|
2004-03-04 04:25:44 -04:00
|
|
|
else:
|
|
|
|
for k, v in other:
|
|
|
|
self[k] = v
|
|
|
|
if kwargs:
|
|
|
|
self.update(kwargs)
|
2002-11-15 02:46:14 -04:00
|
|
|
def get(self, key, default=None):
|
2002-11-18 00:34:10 -04:00
|
|
|
try:
|
2002-11-15 02:46:14 -04:00
|
|
|
return self[key]
|
2002-11-18 00:34:10 -04:00
|
|
|
except KeyError:
|
|
|
|
return default
|
2002-11-15 02:46:14 -04:00
|
|
|
def __repr__(self):
|
2002-11-18 00:34:10 -04:00
|
|
|
return repr(dict(self.iteritems()))
|
|
|
|
def __cmp__(self, other):
|
2003-12-04 16:04:09 -04:00
|
|
|
if other is None:
|
|
|
|
return 1
|
2002-11-18 00:34:10 -04:00
|
|
|
if isinstance(other, DictMixin):
|
|
|
|
other = dict(other.iteritems())
|
|
|
|
return cmp(dict(self.iteritems()), other)
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.keys())
|