Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 1 | # A more or less complete user-defined wrapper around dictionary objects |
| 2 | |
| 3 | class UserDict: |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 4 | def __init__(self): self.data = {} |
| 5 | def __repr__(self): return repr(self.data) |
| 6 | def __cmp__(self, dict): |
| 7 | if type(dict) == type(self.data): |
| 8 | return cmp(self.data, dict) |
| 9 | else: |
| 10 | return cmp(self.data, dict.data) |
| 11 | def __len__(self): return len(self.data) |
| 12 | def __getitem__(self, key): return self.data[key] |
| 13 | def __setitem__(self, key, item): self.data[key] = item |
| 14 | def __delitem__(self, key): del self.data[key] |
| 15 | def clear(self): return self.data.clear() |
| 16 | def copy(self): |
| 17 | import copy |
| 18 | return copy.copy(self) |
| 19 | def keys(self): return self.data.keys() |
| 20 | def items(self): return self.data.items() |
| 21 | def values(self): return self.data.values() |
| 22 | def has_key(self, key): return self.data.has_key(key) |
| 23 | def update(self, other): |
| 24 | if type(other) is type(self.data): |
| 25 | self.data.update(other) |
| 26 | else: |
| 27 | for k, v in other.items(): |
| 28 | self.data[k] = v |