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
): return self
.data
[key
]
18 def __setitem__(self
, key
, item
): self
.data
[key
] = item
19 def __delitem__(self
, key
): del self
.data
[key
]
20 def clear(self
): self
.data
.clear()
22 if self
.__class
__ is UserDict
:
23 return UserDict(self
.data
.copy())
33 def keys(self
): return self
.data
.keys()
34 def items(self
): return self
.data
.items()
35 def iteritems(self
): return self
.data
.iteritems()
36 def iterkeys(self
): return self
.data
.iterkeys()
37 def itervalues(self
): return self
.data
.itervalues()
38 def values(self
): return self
.data
.values()
39 def has_key(self
, key
): return self
.data
.has_key(key
)
40 def update(self
, dict=None, **kwargs
):
43 elif isinstance(dict, UserDict
):
44 self
.data
.update(dict.data
)
45 elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
46 self
.data
.update(dict)
48 for k
, v
in dict.items():
51 self
.data
.update(kwargs
)
52 def get(self
, key
, failobj
=None):
53 if not self
.has_key(key
):
56 def setdefault(self
, key
, failobj
=None):
57 if not self
.has_key(key
):
60 def pop(self
, key
, *args
):
61 return self
.data
.pop(key
, *args
)
63 return self
.data
.popitem()
64 def __contains__(self
, key
):
65 return key
in self
.data
67 def fromkeys(cls
, iterable
, value
=None):
73 class IterableUserDict(UserDict
):
75 return iter(self
.data
)
78 # Mixin defining all dictionary methods for classes that already have
79 # a minimum dictionary interface including getitem, setitem, delitem,
80 # and keys. Without knowledge of the subclass constructor, the mixin
81 # does not define __init__() or copy(). In addition to the four base
82 # methods, progressively more efficiency comes with defining
83 # __contains__(), __iter__(), and iteritems().
85 # second level definitions support higher levels
89 def has_key(self
, key
):
95 def __contains__(self
, key
):
96 return self
.has_key(key
)
98 # third level takes advantage of second level definitions
103 return self
.__iter
__()
105 # fourth level uses definitions from lower levels
106 def itervalues(self
):
107 for _
, v
in self
.iteritems():
110 return [v
for _
, v
in self
.iteritems()]
112 return list(self
.iteritems())
114 for key
in self
.keys():
116 def setdefault(self
, key
, default
=None):
122 def pop(self
, key
, *args
):
124 raise TypeError, "pop expected at most 2 arguments, got "\
125 + repr(1 + len(args
))
136 k
, v
= self
.iteritems().next()
137 except StopIteration:
138 raise KeyError, 'container is empty'
141 def update(self
, other
=None, **kwargs
):
142 # Make progressively weaker assumptions about "other"
145 elif hasattr(other
, 'iteritems'): # iteritems saves memory and lookups
146 for k
, v
in other
.iteritems():
148 elif hasattr(other
, 'keys'):
149 for k
in other
.keys():
156 def get(self
, key
, default
=None):
162 return repr(dict(self
.iteritems()))
163 def __cmp__(self
, other
):
166 if isinstance(other
, DictMixin
):
167 other
= dict(other
.iteritems())
168 return cmp(dict(self
.iteritems()), other
)
170 return len(self
.keys())