1 """A more or less complete user-defined wrapper around dictionary objects."""
4 def __init__(self
, dict=None, **kwargs
):
10 def __repr__(self
): return repr(self
.data
)
11 def __cmp__(self
, dict):
12 if isinstance(dict, UserDict
):
13 return cmp(self
.data
, dict.data
)
15 return cmp(self
.data
, dict)
16 def __len__(self
): return len(self
.data
)
17 def __getitem__(self
, key
):
20 if hasattr(self
.__class
__, "__missing__"):
21 return self
.__class
__.__missing
__(self
, key
)
23 def __setitem__(self
, key
, item
): self
.data
[key
] = item
24 def __delitem__(self
, key
): del self
.data
[key
]
25 def clear(self
): self
.data
.clear()
27 if self
.__class
__ is UserDict
:
28 return UserDict(self
.data
.copy())
38 def keys(self
): return self
.data
.keys()
39 def items(self
): return self
.data
.items()
40 def iteritems(self
): return self
.data
.iteritems()
41 def iterkeys(self
): return self
.data
.iterkeys()
42 def itervalues(self
): return self
.data
.itervalues()
43 def values(self
): return self
.data
.values()
44 def has_key(self
, key
): return key
in self
.data
45 def update(self
, dict=None, **kwargs
):
48 elif isinstance(dict, UserDict
):
49 self
.data
.update(dict.data
)
50 elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
51 self
.data
.update(dict)
53 for k
, v
in dict.items():
56 self
.data
.update(kwargs
)
57 def get(self
, key
, failobj
=None):
61 def setdefault(self
, key
, failobj
=None):
65 def pop(self
, key
, *args
):
66 return self
.data
.pop(key
, *args
)
68 return self
.data
.popitem()
69 def __contains__(self
, key
):
70 return key
in self
.data
72 def fromkeys(cls
, iterable
, value
=None):
78 class IterableUserDict(UserDict
):
80 return iter(self
.data
)
83 _abcoll
.MutableMapping
.register(IterableUserDict
)
87 # Mixin defining all dictionary methods for classes that already have
88 # a minimum dictionary interface including getitem, setitem, delitem,
89 # and keys. Without knowledge of the subclass constructor, the mixin
90 # does not define __init__() or copy(). In addition to the four base
91 # methods, progressively more efficiency comes with defining
92 # __contains__(), __iter__(), and iteritems().
94 # second level definitions support higher levels
98 def has_key(self
, key
):
104 def __contains__(self
, key
):
105 return self
.has_key(key
)
107 # third level takes advantage of second level definitions
112 return self
.__iter
__()
114 # fourth level uses definitions from lower levels
115 def itervalues(self
):
116 for _
, v
in self
.iteritems():
119 return [v
for _
, v
in self
.iteritems()]
121 return list(self
.iteritems())
123 for key
in self
.keys():
125 def setdefault(self
, key
, default
=None):
131 def pop(self
, key
, *args
):
133 raise TypeError, "pop expected at most 2 arguments, got "\
134 + repr(1 + len(args
))
145 k
, v
= self
.iteritems().next()
146 except StopIteration:
147 raise KeyError, 'container is empty'
150 def update(self
, other
=None, **kwargs
):
151 # Make progressively weaker assumptions about "other"
154 elif hasattr(other
, 'iteritems'): # iteritems saves memory and lookups
155 for k
, v
in other
.iteritems():
157 elif hasattr(other
, 'keys'):
158 for k
in other
.keys():
165 def get(self
, key
, default
=None):
171 return repr(dict(self
.iteritems()))
172 def __cmp__(self
, other
):
175 if isinstance(other
, DictMixin
):
176 other
= dict(other
.iteritems())
177 return cmp(dict(self
.iteritems()), other
)
179 return len(self
.keys())