Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """A more or less complete user-defined wrapper around dictionary objects.""" |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 3 | __all__ = ["UserDict"] |
| 4 | |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 5 | class UserDict: |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 6 | def __init__(self, dict=None): |
| 7 | self.data = {} |
| 8 | if dict is not None: self.update(dict) |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 9 | def __repr__(self): return repr(self.data) |
| 10 | def __cmp__(self, dict): |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 11 | if isinstance(dict, UserDict): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 12 | return cmp(self.data, dict.data) |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 13 | else: |
| 14 | return cmp(self.data, dict) |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 15 | def __len__(self): return len(self.data) |
| 16 | def __getitem__(self, key): return self.data[key] |
| 17 | def __setitem__(self, key, item): self.data[key] = item |
| 18 | def __delitem__(self, key): del self.data[key] |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 19 | def clear(self): self.data.clear() |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 20 | def copy(self): |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 21 | if self.__class__ is UserDict: |
| 22 | return UserDict(self.data) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 23 | import copy |
| 24 | return copy.copy(self) |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 25 | def keys(self): return self.data.keys() |
| 26 | def items(self): return self.data.items() |
| 27 | def values(self): return self.data.values() |
| 28 | def has_key(self, key): return self.data.has_key(key) |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 29 | def update(self, dict): |
| 30 | if isinstance(dict, UserDict): |
| 31 | self.data.update(dict.data) |
| 32 | elif isinstance(dict, type(self.data)): |
| 33 | self.data.update(dict) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 34 | else: |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 35 | for k, v in dict.items(): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 36 | self.data[k] = v |
Barry Warsaw | fc3e61c | 1997-10-06 17:50:04 +0000 | [diff] [blame] | 37 | def get(self, key, failobj=None): |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 38 | return self.data.get(key, failobj) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 39 | def setdefault(self, key, failobj=None): |
| 40 | if not self.data.has_key(key): |
| 41 | self.data[key] = failobj |
| 42 | return self.data[key] |
Guido van Rossum | 1072c38 | 2000-12-12 22:06:00 +0000 | [diff] [blame] | 43 | def popitem(self): |
| 44 | return self.data.popitem() |