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