1996-07-22 12:23:25 -03:00
|
|
|
# A more or less complete user-defined wrapper around dictionary objects
|
|
|
|
|
|
|
|
class UserDict:
|
1997-08-14 21:45:26 -03:00
|
|
|
def __init__(self): self.data = {}
|
|
|
|
def __repr__(self): return repr(self.data)
|
|
|
|
def __cmp__(self, dict):
|
1998-03-26 18:14:20 -04:00
|
|
|
if type(dict) == type(self.data):
|
|
|
|
return cmp(self.data, dict)
|
|
|
|
else:
|
|
|
|
return cmp(self.data, dict.data)
|
1997-08-14 21:45:26 -03: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]
|
|
|
|
def clear(self): return self.data.clear()
|
|
|
|
def copy(self):
|
1998-03-26 18:14:20 -04:00
|
|
|
import copy
|
|
|
|
return copy.copy(self)
|
1997-08-14 21:45:26 -03: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)
|
|
|
|
def update(self, other):
|
1998-03-26 18:14:20 -04:00
|
|
|
if type(other) is type(self.data):
|
|
|
|
self.data.update(other)
|
|
|
|
else:
|
|
|
|
for k, v in other.items():
|
|
|
|
self.data[k] = v
|
1997-11-26 11:44:34 -04:00
|
|
|
def get(self, key, failobj=None):
|
1998-03-26 18:14:20 -04:00
|
|
|
if self.data.has_key(key):
|
|
|
|
return self.data[key]
|
|
|
|
else:
|
|
|
|
return failobj
|