diff options
Diffstat (limited to 'src/leap/util/dicts.py')
-rw-r--r-- | src/leap/util/dicts.py | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/src/leap/util/dicts.py b/src/leap/util/dicts.py index d8177973..001ca96b 100644 --- a/src/leap/util/dicts.py +++ b/src/leap/util/dicts.py @@ -1,4 +1,5 @@ -# Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy. +# Backport of OrderedDict() class that runs +# on Python 2.4, 2.5, 2.6, 2.7 and pypy. # Passes Python2.7's test suite and incorporates all the latest updates. try: @@ -17,9 +18,11 @@ class OrderedDict(dict): # An inherited dict maps keys to values. # The inherited dict provides __getitem__, __len__, __contains__, and get. # The remaining methods are order-aware. - # Big-O running times for all methods are the same as for regular dictionaries. + # Big-O running times for all methods are the same as for regular + # dictionaries. - # The internal self.__map dictionary maps keys to links in a doubly linked list. + # The internal self.__map dictionary maps keys to links in a doubly + # linked list. # The circular doubly linked list starts and ends with a sentinel element. # The sentinel element never gets deleted (this simplifies the algorithm). # Each link is stored as a list of length three: [PREV, NEXT, KEY]. @@ -42,8 +45,9 @@ class OrderedDict(dict): def __setitem__(self, key, value, dict_setitem=dict.__setitem__): 'od.__setitem__(i, y) <==> od[i]=y' - # Setting a new item creates a new link which goes at the end of the linked - # list, and the inherited dictionary is updated with the new key/value pair. + # Setting a new item creates a new link which goes at the end + # of the linked list, and the inherited dictionary is updated + # with the new key/value pair. if key not in self: root = self.__root last = root[0] @@ -53,7 +57,8 @@ class OrderedDict(dict): def __delitem__(self, key, dict_delitem=dict.__delitem__): 'od.__delitem__(y) <==> del od[y]' # Deleting an existing item uses self.__map to find the link which is - # then removed by updating the links in the predecessor and successor nodes. + # then removed by updating the links in the predecessor and successor + # nodes. dict_delitem(self, key) link_prev, link_next, key = self.__map.pop(key) link_prev[1] = link_next @@ -89,8 +94,8 @@ class OrderedDict(dict): def popitem(self, last=True): '''od.popitem() -> (k, v), return and remove a (key, value) pair. - Pairs are returned in LIFO order if last is true or FIFO order if false. - + Pairs are returned in LIFO order if last is true or FIFO order if + false. ''' if not self: raise KeyError('dictionary is empty') @@ -142,11 +147,13 @@ class OrderedDict(dict): '''od.update(E, **F) -> None. Update od from dict/iterable E and F. If E is a dict instance, does: for k in E: od[k] = E[k] - If E has a .keys() method, does: for k in E.keys(): od[k] = E[k] + If E has a .keys() method, does: for k in E.keys(): + od[k] = E[k] Or if E is an iterable of items, does: for k, v in E: od[k] = v - In either case, this is followed by: for k, v in F.items(): od[k] = v - + In either case, this is followed by: for k, v in F.items(): + od[k] = v ''' + if len(args) > 2: raise TypeError('update() takes at most 2 positional ' 'arguments (%d given)' % (len(args),)) @@ -169,13 +176,16 @@ class OrderedDict(dict): for key, value in kwds.items(): self[key] = value - __update = update # let subclasses override update without breaking __init__ + __update = update # let subclasses override update + # without breaking __init__ __marker = object() def pop(self, key, default=__marker): - '''od.pop(k[,d]) -> v, remove specified key and return the corresponding value. - If key is not found, d is returned if given, otherwise KeyError is raised. + '''od.pop(k[,d]) -> v + remove specified key and return the corresponding value. + If key is not found, d is returned if given, + otherwise KeyError is raised. ''' if key in self: @@ -232,12 +242,12 @@ class OrderedDict(dict): return d def __eq__(self, other): - '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive + '''od.__eq__(y) <==> od==y. + Comparison to another OD is order-sensitive while comparison to a regular mapping is order-insensitive. - ''' if isinstance(other, OrderedDict): - return len(self)==len(other) and self.items() == other.items() + return len(self) == len(other) and self.items() == other.items() return dict.__eq__(self, other) def __ne__(self, other): |