| 1 |
from __pypy__ import reversed_dict, move_to_end, objects_in_repr |
| 2 |
from _operator import eq as _eq |
| 3 |
|
| 4 |
|
| 5 |
class OrderedDict(dict): |
| 6 |
'''Dictionary that remembers insertion order. |
| 7 |
|
| 8 |
In PyPy all dicts are ordered anyway. This is mostly useful as a |
| 9 |
placeholder to mean "this dict must be ordered even on CPython". |
| 10 |
|
| 11 |
Known difference: iterating over an OrderedDict which is being |
| 12 |
concurrently modified raises RuntimeError in PyPy. In CPython |
| 13 |
instead we get some behavior that appears reasonable in some |
| 14 |
cases but is nonsensical in other cases. This is officially |
| 15 |
forbidden by the CPython docs, so we forbid it explicitly for now. |
| 16 |
''' |
| 17 |
__module__ = 'collections' |
| 18 |
|
| 19 |
def __init__(*args, **kwds): |
| 20 |
'''Initialize an ordered dictionary. The signature is the same as |
| 21 |
regular dictionaries, but keyword arguments are not recommended because |
| 22 |
their insertion order is arbitrary. |
| 23 |
|
| 24 |
''' |
| 25 |
if not args: |
| 26 |
raise TypeError("descriptor '__init__' of 'OrderedDict' object " |
| 27 |
"needs an argument") |
| 28 |
self, *args = args |
| 29 |
if len(args) > 1: |
| 30 |
raise TypeError('expected at most 1 arguments, got %d' % len(args)) |
| 31 |
self.__update(*args, **kwds) |
| 32 |
|
| 33 |
def update(*args, **kwds): |
| 34 |
''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F. |
| 35 |
If E present and has a .keys() method, does: for k in E: D[k] = E[k] |
| 36 |
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v |
| 37 |
In either case, this is followed by: for k, v in F.items(): D[k] = v |
| 38 |
''' |
| 39 |
if not args: |
| 40 |
raise TypeError("descriptor 'update' of 'OrderedDict' object " |
| 41 |
"needs an argument") |
| 42 |
self, *args = args |
| 43 |
if len(args) > 1: |
| 44 |
raise TypeError('update expected at most 1 arguments, got %d' % |
| 45 |
len(args)) |
| 46 |
if args: |
| 47 |
other = args[0] |
| 48 |
if hasattr(other, "keys"): |
| 49 |
for key in other.keys(): |
| 50 |
self[key] = other[key] |
| 51 |
elif hasattr(other, 'items'): |
| 52 |
for key, value in other.items(): |
| 53 |
self[key] = value |
| 54 |
else: |
| 55 |
for key, value in other: |
| 56 |
self[key] = value |
| 57 |
for key, value in kwds.items(): |
| 58 |
self[key] = value |
| 59 |
__update = update |
| 60 |
|
| 61 |
def __reversed__(self): |
| 62 |
return reversed_dict(self) |
| 63 |
|
| 64 |
def popitem(self, last=True): |
| 65 |
'''od.popitem() -> (k, v), return and remove a (key, value) pair. |
| 66 |
Pairs are returned in LIFO order if last is true or FIFO order if false. |
| 67 |
|
| 68 |
''' |
| 69 |
if last: |
| 70 |
return dict.popitem(self) |
| 71 |
else: |
| 72 |
it = dict.__iter__(self) |
| 73 |
try: |
| 74 |
k = next(it) |
| 75 |
except StopIteration: |
| 76 |
raise KeyError('dictionary is empty') |
| 77 |
return (k, self.pop(k)) |
| 78 |
|
| 79 |
def move_to_end(self, key, last=True): |
| 80 |
'''Move an existing element to the end (or beginning if last==False). |
| 81 |
|
| 82 |
Raises KeyError if the element does not exist. |
| 83 |
When last=True, acts like a fast version of self[key]=self.pop(key). |
| 84 |
|
| 85 |
''' |
| 86 |
return move_to_end(self, key, last) |
| 87 |
|
| 88 |
def __repr__(self): |
| 89 |
'od.__repr__() <==> repr(od)' |
| 90 |
if not self: |
| 91 |
return '%s()' % (self.__class__.__name__,) |
| 92 |
currently_in_repr = objects_in_repr() |
| 93 |
if self in currently_in_repr: |
| 94 |
return '...' |
| 95 |
currently_in_repr[self] = 1 |
| 96 |
try: |
| 97 |
return '%s(%r)' % (self.__class__.__name__, list(self.items())) |
| 98 |
finally: |
| 99 |
try: |
| 100 |
del currently_in_repr[self] |
| 101 |
except: |
| 102 |
pass |
| 103 |
|
| 104 |
def __reduce__(self): |
| 105 |
'Return state information for pickling' |
| 106 |
state = object.__getstate__(self) |
| 107 |
return self.__class__, (), state or None, None, iter(self.items()) |
| 108 |
|
| 109 |
def copy(self): |
| 110 |
'od.copy() -> a shallow copy of od' |
| 111 |
return self.__class__(self) |
| 112 |
|
| 113 |
def __eq__(self, other): |
| 114 |
'''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive |
| 115 |
while comparison to a regular mapping is order-insensitive. |
| 116 |
|
| 117 |
''' |
| 118 |
if isinstance(other, OrderedDict): |
| 119 |
return dict.__eq__(self, other) and all(map(_eq, self, other)) |
| 120 |
return dict.__eq__(self, other) |
| 121 |
|
| 122 |
def __or__(self, other): |
| 123 |
if not isinstance(other, dict): |
| 124 |
return NotImplemented |
| 125 |
copyself = self.copy() |
| 126 |
copyself.update(other) |
| 127 |
return copyself |
| 128 |
|
| 129 |
def __ror__(self, other): |
| 130 |
if not isinstance(other, dict): |
| 131 |
return NotImplemented |
| 132 |
copy = type(self)(other) |
| 133 |
copy.update(self) |
| 134 |
return copy |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
__ne__ = object.__ne__ |
| 139 |
|
| 140 |
def keys(self): |
| 141 |
"D.keys() -> a set-like object providing a view on D's keys" |
| 142 |
return _OrderedDictKeysView(self) |
| 143 |
|
| 144 |
def items(self): |
| 145 |
"D.items() -> a set-like object providing a view on D's items" |
| 146 |
return _OrderedDictItemsView(self) |
| 147 |
|
| 148 |
def values(self): |
| 149 |
"D.values() -> an object providing a view on D's values" |
| 150 |
return _OrderedDictValuesView(self) |
| 151 |
|
| 152 |
dict_keys = type({}.keys()) |
| 153 |
dict_values = type({}.values()) |
| 154 |
dict_items = type({}.items()) |
| 155 |
|
| 156 |
class _OrderedDictKeysView(dict_keys): |
| 157 |
def __reversed__(self): |
| 158 |
yield from reversed_dict(self._dict) |
| 159 |
|
| 160 |
class _OrderedDictItemsView(dict_items): |
| 161 |
def __reversed__(self): |
| 162 |
for key in reversed_dict(self._dict): |
| 163 |
yield (key, self._dict[key]) |
| 164 |
|
| 165 |
class _OrderedDictValuesView(dict_values): |
| 166 |
def __reversed__(self): |
| 167 |
for key in reversed_dict(self._dict): |
| 168 |
yield self._dict[key] |