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() |
Raymond Hettinger | 31ce5cb | 2008-02-21 19:24:53 +0000 | [diff] [blame] | 44 | def has_key(self, key): return key in self.data |
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): |
Guido van Rossum | 8bc0965 | 2008-02-21 18:18:37 +0000 | [diff] [blame] | 58 | if key not in self: |
Martin v. Löwis | f3b3074 | 2001-06-18 01:09:41 +0000 | [diff] [blame] | 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): |
Raymond Hettinger | 31ce5cb | 2008-02-21 19:24:53 +0000 | [diff] [blame] | 62 | if key not in self: |
Martin v. Löwis | f3b3074 | 2001-06-18 01:09:41 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 3bed4ae | 2008-06-07 16:16:12 +0000 | [diff] [blame] | 82 | import _abcoll |
| 83 | _abcoll.MutableMapping.register(IterableUserDict) |
| 84 | |
| 85 | |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 86 | class DictMixin: |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 87 | # Mixin defining all dictionary methods for classes that already have |
| 88 | # a minimum dictionary interface including getitem, setitem, delitem, |
| 89 | # and keys. Without knowledge of the subclass constructor, the mixin |
| 90 | # does not define __init__() or copy(). In addition to the four base |
Barry Warsaw | 1a17704 | 2003-01-31 03:30:09 +0000 | [diff] [blame] | 91 | # methods, progressively more efficiency comes with defining |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 92 | # __contains__(), __iter__(), and iteritems(). |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 93 | |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 94 | # second level definitions support higher levels |
| 95 | def __iter__(self): |
| 96 | for k in self.keys(): |
| 97 | yield k |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 98 | def has_key(self, key): |
| 99 | try: |
| 100 | value = self[key] |
| 101 | except KeyError: |
| 102 | return False |
| 103 | return True |
Raymond Hettinger | 51f3f1b | 2003-01-22 01:39:06 +0000 | [diff] [blame] | 104 | def __contains__(self, key): |
| 105 | return self.has_key(key) |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 106 | |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 107 | # third level takes advantage of second level definitions |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 108 | def iteritems(self): |
| 109 | for k in self: |
| 110 | yield (k, self[k]) |
Raymond Hettinger | 51f3f1b | 2003-01-22 01:39:06 +0000 | [diff] [blame] | 111 | def iterkeys(self): |
| 112 | return self.__iter__() |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 113 | |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 114 | # fourth level uses definitions from lower levels |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 115 | def itervalues(self): |
| 116 | for _, v in self.iteritems(): |
| 117 | yield v |
| 118 | def values(self): |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 119 | return [v for _, v in self.iteritems()] |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 120 | def items(self): |
| 121 | return list(self.iteritems()) |
| 122 | def clear(self): |
| 123 | for key in self.keys(): |
| 124 | del self[key] |
Walter Dörwald | c139909 | 2004-05-27 09:41:04 +0000 | [diff] [blame] | 125 | def setdefault(self, key, default=None): |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 126 | try: |
| 127 | return self[key] |
| 128 | except KeyError: |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 129 | self[key] = default |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 130 | return default |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 131 | def pop(self, key, *args): |
| 132 | if len(args) > 1: |
| 133 | raise TypeError, "pop expected at most 2 arguments, got "\ |
| 134 | + repr(1 + len(args)) |
| 135 | try: |
| 136 | value = self[key] |
| 137 | except KeyError: |
| 138 | if args: |
| 139 | return args[0] |
| 140 | raise |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 141 | del self[key] |
| 142 | return value |
| 143 | def popitem(self): |
| 144 | try: |
| 145 | k, v = self.iteritems().next() |
| 146 | except StopIteration: |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 147 | raise KeyError, 'container is empty' |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 148 | del self[k] |
| 149 | return (k, v) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 150 | def update(self, other=None, **kwargs): |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 151 | # Make progressively weaker assumptions about "other" |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 152 | if other is None: |
| 153 | pass |
| 154 | elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 155 | for k, v in other.iteritems(): |
| 156 | self[k] = v |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 157 | elif hasattr(other, 'keys'): |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 158 | for k in other.keys(): |
| 159 | self[k] = other[k] |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 160 | else: |
| 161 | for k, v in other: |
| 162 | self[k] = v |
| 163 | if kwargs: |
| 164 | self.update(kwargs) |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 165 | def get(self, key, default=None): |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 166 | try: |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 167 | return self[key] |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 168 | except KeyError: |
| 169 | return default |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 170 | def __repr__(self): |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 171 | return repr(dict(self.iteritems())) |
| 172 | def __cmp__(self, other): |
Raymond Hettinger | f477c88 | 2003-12-04 20:04:09 +0000 | [diff] [blame] | 173 | if other is None: |
| 174 | return 1 |
Raymond Hettinger | 8ddc176 | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 175 | if isinstance(other, DictMixin): |
| 176 | other = dict(other.iteritems()) |
| 177 | return cmp(dict(self.iteritems()), other) |
| 178 | def __len__(self): |
| 179 | return len(self.keys()) |