| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2009 Raymond Hettinger. | |
| 2 # | |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 4 # of this software and associated documentation files (the "Software"), to deal | |
| 5 # in the Software without restriction, including without limitation the rights | |
| 6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 7 # copies of the Software, and to permit persons to whom the Software is | |
| 8 # furnished to do so, subject to the following conditions: | |
| 9 # | |
| 10 # The above copyright notice and this permission notice shall be included in | |
| 11 # all copies or substantial portions of the Software. | |
| 12 # | |
| 13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 19 # SOFTWARE. | |
| 20 | |
| 21 # This code is obtained from http://code.activestate.com/recipes/576669/ | |
| 22 | |
| 23 from collections import MutableMapping | |
| 24 | |
| 25 class OrderedDict(dict, MutableMapping): | |
| 26 | |
| 27 # Methods with direct access to underlying attributes | |
| 28 | |
| 29 def __init__(self, *args, **kwds): | |
| 30 if len(args) > 1: | |
| 31 raise TypeError('expected at 1 argument, got %d', len(args)) | |
| 32 if not hasattr(self, '_keys'): | |
| 33 self._keys = [] | |
| 34 self.update(*args, **kwds) | |
| 35 | |
| 36 def clear(self): | |
| 37 del self._keys[:] | |
| 38 dict.clear(self) | |
| 39 | |
| 40 def __setitem__(self, key, value): | |
| 41 if key not in self: | |
| 42 self._keys.append(key) | |
| 43 dict.__setitem__(self, key, value) | |
| 44 | |
| 45 def __delitem__(self, key): | |
| 46 dict.__delitem__(self, key) | |
| 47 self._keys.remove(key) | |
| 48 | |
| 49 def __iter__(self): | |
| 50 return iter(self._keys) | |
| 51 | |
| 52 def __reversed__(self): | |
| 53 return reversed(self._keys) | |
| 54 | |
| 55 def popitem(self): | |
| 56 if not self: | |
| 57 raise KeyError | |
| 58 key = self._keys.pop() | |
| 59 value = dict.pop(self, key) | |
| 60 return key, value | |
| 61 | |
| 62 def __reduce__(self): | |
| 63 items = [[k, self[k]] for k in self] | |
| 64 inst_dict = vars(self).copy() | |
| 65 inst_dict.pop('_keys', None) | |
| 66 return (self.__class__, (items,), inst_dict) | |
| 67 | |
| 68 # Methods with indirect access via the above methods | |
| 69 | |
| 70 setdefault = MutableMapping.setdefault | |
| 71 update = MutableMapping.update | |
| 72 pop = MutableMapping.pop | |
| 73 keys = MutableMapping.keys | |
| 74 values = MutableMapping.values | |
| 75 items = MutableMapping.items | |
| 76 | |
| 77 def __repr__(self): | |
| 78 pairs = ', '.join(map('%r: %r'.__mod__, self.items())) | |
| 79 return '%s({%s})' % (self.__class__.__name__, pairs) | |
| 80 | |
| 81 def copy(self): | |
| 82 return self.__class__(self) | |
| 83 | |
| 84 @classmethod | |
| 85 def fromkeys(cls, iterable, value=None): | |
| 86 d = cls() | |
| 87 for key in iterable: | |
| 88 d[key] = value | |
| 89 return d | |
| OLD | NEW |