|
1 |
| -__all__ = ['deque', 'defaultdict', 'namedtuple'] |
| 1 | +__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict'] |
2 | 2 | # For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
|
3 | 3 | # They should however be considered an integral part of collections.py.
|
4 | 4 | from _abcoll import *
|
|
10 | 10 | from keyword import iskeyword as _iskeyword
|
11 | 11 | import sys as _sys
|
12 | 12 |
|
| 13 | +################################################################################ |
| 14 | +### namedtuple |
| 15 | +################################################################################ |
| 16 | + |
13 | 17 | def namedtuple(typename, field_names, verbose=False):
|
14 | 18 | """Returns a new subclass of tuple with named fields.
|
15 | 19 |
|
@@ -106,7 +110,60 @@ def _replace(self, **kwds):
|
106 | 110 |
|
107 | 111 |
|
108 | 112 |
|
109 |
| - |
| 113 | +################################################################################ |
| 114 | +### UserDict |
| 115 | +################################################################################ |
| 116 | + |
| 117 | +class UserDict(MutableMapping): |
| 118 | + |
| 119 | + # Start by filling-out the abstract methods |
| 120 | + def __init__(self, dict=None, **kwargs): |
| 121 | + self.data = {} |
| 122 | + if dict is not None: |
| 123 | + self.update(dict) |
| 124 | + if len(kwargs): |
| 125 | + self.update(kwargs) |
| 126 | + def __len__(self): return len(self.data) |
| 127 | + def __getitem__(self, key): |
| 128 | + if key in self.data: |
| 129 | + return self.data[key] |
| 130 | + if hasattr(self.__class__, "__missing__"): |
| 131 | + return self.__class__.__missing__(self, key) |
| 132 | + raise KeyError(key) |
| 133 | + def __setitem__(self, key, item): self.data[key] = item |
| 134 | + def __delitem__(self, key): del self.data[key] |
| 135 | + def __iter__(self): |
| 136 | + return iter(self.data) |
| 137 | + |
| 138 | + |
| 139 | + # Now, add the methods in dicts but not in MutableMapping |
| 140 | + def __repr__(self): return repr(self.data) |
| 141 | + def copy(self): |
| 142 | + if self.__class__ is UserDict: |
| 143 | + return
10000
UserDict(self.data.copy()) |
| 144 | + import copy |
| 145 | + data = self.data |
| 146 | + try: |
| 147 | + self.data = {} |
| 148 | + c = copy.copy(self) |
| 149 | + finally: |
| 150 | + self.data = data |
| 151 | + c.update(self) |
| 152 | + return c |
| 153 | + @classmethod |
| 154 | + def fromkeys(cls, iterable, value=None): |
| 155 | + d = cls() |
| 156 | + for key in iterable: |
| 157 | + d[key] = value |
| 158 | + return d |
| 159 | + |
| 160 | +MutableMapping.register(UserDict) |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +################################################################################ |
| 165 | +### Simple tests |
| 166 | +################################################################################ |
110 | 167 |
|
111 | 168 | if __name__ == '__main__':
|
112 | 169 | # verify that instances can be pickled
|
|
0 commit comments