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 | |
| 3 | class UserDict: |
Raymond Hettinger | 5440545 | 2002-11-22 00:07:40 +0000 | [diff] [blame] | 4 | def __init__(self, dict=None, **kwargs): |
Raymond Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 5 | self.data = {} |
Raymond Hettinger | 5440545 | 2002-11-22 00:07:40 +0000 | [diff] [blame] | 6 | if dict is not None: |
Raymond Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 7 | self.update(dict) |
| 8 | if len(kwargs): |
| 9 | self.update(kwargs) |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 10 | def __repr__(self): return repr(self.data) |
| 11 | def __cmp__(self, dict): |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 12 | if isinstance(dict, UserDict): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 13 | return cmp(self.data, dict.data) |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 14 | else: |
| 15 | return cmp(self.data, dict) |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 16 | def __len__(self): return len(self.data) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 17 | def __getitem__(self, key): |
| 18 | if key in self.data: |
| 19 | return self.data[key] |
| 20 | if hasattr(self.__class__, "__missing__"): |
| 21 | return self.__class__.__missing__(self, key) |
| 22 | raise KeyError(key) |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 23 | def __setitem__(self, key, item): self.data[key] = item |
| 24 | def __delitem__(self, key): del self.data[key] |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 25 | def clear(self): self.data.clear() |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 26 | def copy(self): |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 27 | if self.__class__ is UserDict: |
Raymond Hettinger | 9014560 | 2003-12-21 22:19:08 +0000 | [diff] [blame] | 28 | return UserDict(self.data.copy()) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 29 | import copy |
Fred Drake | 3ce5af7 | 2001-11-05 17:40:48 +0000 | [diff] [blame] | 30 | data = self.data |
| 31 | try: |
| 32 | self.data = {} |
| 33 | c = copy.copy(self) |
| 34 | finally: |
| 35 | self.data = data |
| 36 | c.update(self) |
| 37 | return c |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 38 | def keys(self): return self.data.keys() |
| 39 | def items(self): return self.data.items() |
Fred Drake | bedebbd | 2001-05-03 04:54:41 +0000 | [diff] [blame] | 40 | def iteritems(self): return self.data.iteritems() |
| 41 | def iterkeys(self): return self.data.iterkeys() |
| 42 | def itervalues(self): return self.data.itervalues() |
Guido van Rossum | b94cd96 | 1997-06-03 14:10:01 +0000 | [diff] [blame] | 43 | def values(self): return self.data.values() |
| 44 | def has_key(self, key): return self.data.has_key(key) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 45 | def update(self, dict=None, **kwargs): |
| 46 | if dict is None: |
| 47 | pass |
| 48 | elif isinstance(dict, UserDict): |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 49 | self.data.update(dict.data) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 50 | elif isinstance(dict, type({})) or not hasattr(dict, 'items'): |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 51 | self.data.update(dict) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 52 | else: |
Guido van Rossum | 1697b9c | 1999-03-26 15:31:12 +0000 | [diff] [blame] | 53 | for k, v in dict.items(): |
Martin v. Löwis | f3b3074 | 2001-06-18 01:09:41 +0000 | [diff] [blame] | 54 | self[k] = v |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 55 | if len(kwargs): |
| 56 | self.data.update(kwargs) |
Barry Warsaw | fc3e61c | 1997-10-06 17:50:04 +0000 | [diff] [blame] | 57 | def get(self, key, failobj=None): |
Martin v. Löwis | f3b3074 | 2001-06-18 01:09:41 +0000 | [diff] [blame] | 58 | if not self.has_key(key): |
| 59 | return failobj |
| 60 | return self[key] |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 61 | def setdefault(self, key, failobj=None): |
Martin v. Löwis | f3b3074 | 2001-06-18 01:09:41 +0000 | [diff] [blame] | 62 | if not self.has_key(key): |
| 63 | self[key] = failobj |
| 64 | return self[key] |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 65 | def pop(self, key, *args): |
| 66 | return self.data.pop(key, *args) |
Guido van Rossum | 1072c38 | 2000-12-12 22:06:00 +0000 | [diff] [blame] | 67 | def popitem(self): |
| 68 | return self.data.popitem() |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 69 | def __contains__(self, key): |
| 70 | return key in self.data |
Guido van Rossum | 75b64e6 | 2005-01-16 00:16:11 +0000 | [diff] [blame] | 71 | @classmethod |
Raymond Hettinger | e4827eb | 2002-11-27 08:29:11 +0000 | [diff] [blame] | 72 | def fromkeys(cls, iterable, value=None): |
| 73 | d = cls() |
| 74 | for key in iterable: |
| 75 | d[key] = value |
| 76 | return d |
Guido van Rossum | 2050b65 | 2001-08-07 17:40:42 +0000 | [diff] [blame] | 77 | |
| 78 | class IterableUserDict(UserDict): |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 79 | def __iter__(self): |
| 80 | return iter(self.data) |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 81 | |
| 82 | class DictMixin: |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 83 | # Mixin defining all dictionary methods for classes that already have |
| 84 | # a minimum dictionary interface including getitem, setitem, delitem, |
| 85 | # and keys. Without knowledge of the subclass constructor, the mixin |
| 86 | # does not define __init__() or copy(). In addition to the four base |
Barry Warsaw | 1a17704 | 2003-01-31 03:30:09 +0000 | [diff] [blame] | 87 | # methods, progressively more efficiency comes with defining |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 88 | # __contains__(), __iter__(), and iteritems(). |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 89 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 90 | # second level definitions support higher levels |
| 91 | def __iter__(self): |
| 92 | for k in self.keys(): |
| 93 | yield k |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 94 | def has_key(self, key): |
| 95 | try: |
| 96 | value = self[key] |
| 97 | except KeyError: |
| 98 | return False |
| 99 | return True |
Raymond Hettinger | 51f3f1b | 2003-01-22 01:39:06 +0000 | [diff] [blame] | 100 | def __contains__(self, key): |
| 101 | return self.has_key(key) |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 102 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 103 | # third level takes advantage of second level definitions |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 104 | def iteritems(self): |
| 105 | for k in self: |
| 106 | yield (k, self[k]) |
Raymond Hettinger | 51f3f1b | 2003-01-22 01:39:06 +0000 | [diff] [blame] | 107 | def iterkeys(self): |
| 108 | return self.__iter__() |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 109 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 110 | # fourth level uses definitions from lower levels |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 111 | def itervalues(self): |
| 112 | for _, v in self.iteritems(): |
| 113 | yield v |
| 114 | def values(self): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 115 | return [v for _, v in self.iteritems()] |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 116 | def items(self): |
| 117 | return list(self.iteritems()) |
| 118 | def clear(self): |
| 119 | for key in self.keys(): |
| 120 | del self[key] |
Walter Dörwald | c139909 | 2004-05-27 09:41:04 +0000 | [diff] [blame] | 121 | def setdefault(self, key, default=None): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 122 | try: |
| 123 | return self[key] |
| 124 | except KeyError: |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 125 | self[key] = default |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 126 | return default |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 127 | def pop(self, key, *args): |
| 128 | if len(args) > 1: |
| 129 | raise TypeError, "pop expected at most 2 arguments, got "\ |
| 130 | + repr(1 + len(args)) |
| 131 | try: |
| 132 | value = self[key] |
| 133 | except KeyError: |
| 134 | if args: |
| 135 | return args[0] |
| 136 | raise |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 137 | del self[key] |
| 138 | return value |
| 139 | def popitem(self): |
| 140 | try: |
| 141 | k, v = self.iteritems().next() |
| 142 | except StopIteration: |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 143 | raise KeyError, 'container is empty' |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 144 | del self[k] |
| 145 | return (k, v) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 146 | def update(self, other=None, **kwargs): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 147 | # Make progressively weaker assumptions about "other" |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 148 | if other is None: |
| 149 | pass |
| 150 | elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 151 | for k, v in other.iteritems(): |
| 152 | self[k] = v |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 153 | elif hasattr(other, 'keys'): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 154 | for k in other.keys(): |
| 155 | self[k] = other[k] |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 156 | else: |
| 157 | for k, v in other: |
| 158 | self[k] = v |
| 159 | if kwargs: |
| 160 | self.update(kwargs) |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 161 | def get(self, key, default=None): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 162 | try: |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 163 | return self[key] |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 164 | except KeyError: |
| 165 | return default |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 166 | def __repr__(self): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 167 | return repr(dict(self.iteritems())) |
| 168 | def __cmp__(self, other): |
Raymond Hettinger | f477c88 | 2003-12-04 20:04:09 +0000 | [diff] [blame] | 169 | if other is None: |
| 170 | return 1 |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 171 | if isinstance(other, DictMixin): |
| 172 | other = dict(other.iteritems()) |
| 173 | return cmp(dict(self.iteritems()), other) |
| 174 | def __len__(self): |
| 175 | return len(self.keys()) |