2000-02-02 15:10:15 +00:00
|
|
|
"""A more or less complete user-defined wrapper around dictionary objects."""
|
1993-11-30 13:43:54 +00:00
|
|
|
|
|
|
|
class UserDict:
|
1999-03-26 15:31:12 +00:00
|
|
|
def __init__(self, dict=None):
|
|
|
|
self.data = {}
|
|
|
|
if dict is not None: self.update(dict)
|
1997-06-03 14:10:01 +00:00
|
|
|
def __repr__(self): return repr(self.data)
|
|
|
|
def __cmp__(self, dict):
|
1999-03-26 15:31:12 +00:00
|
|
|
if isinstance(dict, UserDict):
|
1998-03-26 21:13:24 +00:00
|
|
|
return cmp(self.data, dict.data)
|
1999-03-26 15:31:12 +00:00
|
|
|
else:
|
|
|
|
return cmp(self.data, dict)
|
1997-06-03 14:10:01 +00:00
|
|
|
def __len__(self): return len(self.data)
|
|
|
|
def __getitem__(self, key): return self.data[key]
|
|
|
|
def __setitem__(self, key, item): self.data[key] = item
|
|
|
|
def __delitem__(self, key): del self.data[key]
|
1999-03-26 15:31:12 +00:00
|
|
|
def clear(self): self.data.clear()
|
1997-06-03 14:10:01 +00:00
|
|
|
def copy(self):
|
1999-03-26 15:31:12 +00:00
|
|
|
if self.__class__ is UserDict:
|
|
|
|
return UserDict(self.data)
|
1998-03-26 21:13:24 +00:00
|
|
|
import copy
|
|
|
|
return copy.copy(self)
|
1997-06-03 14:10:01 +00:00
|
|
|
def keys(self): return self.data.keys()
|
|
|
|
def items(self): return self.data.items()
|
|
|
|
def values(self): return self.data.values()
|
|
|
|
def has_key(self, key): return self.data.has_key(key)
|
1999-03-26 15:31:12 +00:00
|
|
|
def update(self, dict):
|
|
|
|
if isinstance(dict, UserDict):
|
|
|
|
self.data.update(dict.data)
|
|
|
|
elif isinstance(dict, type(self.data)):
|
|
|
|
self.data.update(dict)
|
1998-03-26 21:13:24 +00:00
|
|
|
else:
|
1999-03-26 15:31:12 +00:00
|
|
|
for k, v in dict.items():
|
1998-03-26 21:13:24 +00:00
|
|
|
self.data[k] = v
|
1997-10-06 17:50:04 +00:00
|
|
|
def get(self, key, failobj=None):
|
1999-03-26 15:31:12 +00:00
|
|
|
return self.data.get(key, failobj)
|
2000-08-08 16:12:54 +00:00
|
|
|
def setdefault(self, key, failobj=None):
|
|
|
|
if not self.data.has_key(key):
|
|
|
|
self.data[key] = failobj
|
|
|
|
return self.data[key]
|