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] |
Guido van Rossum | f495625 | 2002-04-13 14:03:38 +0000 | [diff] [blame] | 58 | def pop(self, key): |
| 59 | return self.data.pop(key) |
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 |
| 80 | # methods, progessively more efficiency comes with defining |
| 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 |
| 93 | __contains__ = has_key |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 94 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 95 | # third level takes advantage of second level definitions |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 96 | def iteritems(self): |
| 97 | for k in self: |
| 98 | yield (k, self[k]) |
| 99 | iterkeys = __iter__ |
| 100 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 101 | # fourth level uses definitions from lower levels |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 102 | def itervalues(self): |
| 103 | for _, v in self.iteritems(): |
| 104 | yield v |
| 105 | def values(self): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 106 | return [v for _, v in self.iteritems()] |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 107 | def items(self): |
| 108 | return list(self.iteritems()) |
| 109 | def clear(self): |
| 110 | for key in self.keys(): |
| 111 | del self[key] |
| 112 | def setdefault(self, key, default): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 113 | try: |
| 114 | return self[key] |
| 115 | except KeyError: |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 116 | self[key] = default |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 117 | return default |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 118 | def pop(self, key): |
| 119 | value = self[key] |
| 120 | del self[key] |
| 121 | return value |
| 122 | def popitem(self): |
| 123 | try: |
| 124 | k, v = self.iteritems().next() |
| 125 | except StopIteration: |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 126 | raise KeyError, 'container is empty' |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 127 | del self[k] |
| 128 | return (k, v) |
| 129 | def update(self, other): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 130 | # Make progressively weaker assumptions about "other" |
| 131 | if hasattr(other, 'iteritems'): # iteritems saves memory and lookups |
| 132 | for k, v in other.iteritems(): |
| 133 | self[k] = v |
| 134 | elif hasattr(other, '__iter__'): # iter saves memory |
| 135 | for k in other: |
| 136 | self[k] = other[k] |
| 137 | else: |
| 138 | for k in other.keys(): |
| 139 | self[k] = other[k] |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 140 | def get(self, key, default=None): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 141 | try: |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 142 | return self[key] |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 143 | except KeyError: |
| 144 | return default |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 145 | def __repr__(self): |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 146 | return repr(dict(self.iteritems())) |
| 147 | def __cmp__(self, other): |
| 148 | if isinstance(other, DictMixin): |
| 149 | other = dict(other.iteritems()) |
| 150 | return cmp(dict(self.iteritems()), other) |
| 151 | def __len__(self): |
| 152 | return len(self.keys()) |