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