Package Bio :: Package _py3k :: Module _ordereddict
[hide private]
[frames] | no frames]

Source Code for Module Bio._py3k._ordereddict

  1  # Copyright (c) 2009 Raymond Hettinger 
  2  # 
  3  # Permission is hereby granted, free of charge, to any person 
  4  # obtaining a copy of this software and associated documentation files 
  5  # (the "Software"), to deal in the Software without restriction, 
  6  # including without limitation the rights to use, copy, modify, merge, 
  7  # publish, distribute, sublicense, and/or sell copies of the Software, 
  8  # and to permit persons to whom the Software is furnished to do so, 
  9  # subject to the following conditions: 
 10  # 
 11  #     The above copyright notice and this permission notice shall be 
 12  #     included in all copies or substantial portions of the Software. 
 13  # 
 14  #     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 15  #     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
 16  #     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 17  #     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 18  #     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 19  #     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 20  #     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
 21  #     OTHER DEALINGS IN THE SOFTWARE. 
 22  """Python 3 compatibility tools (PRIVATE).""" 
 23   
 24  from UserDict import DictMixin 
25 26 27 -class OrderedDict(dict, DictMixin):
28
29 - def __init__(self, *args, **kwds):
30 if len(args) > 1: 31 raise TypeError('expected at most 1 arguments, got %d' % len(args)) 32 try: 33 self.__end 34 except AttributeError: 35 self.clear() 36 self.update(*args, **kwds)
37
38 - def clear(self):
39 self.__end = end = [] 40 end += [None, end, end] # sentinel node for doubly linked list 41 self.__map = {} # key --> [key, prev, next] 42 dict.clear(self)
43
44 - def __setitem__(self, key, value):
45 if key not in self: 46 end = self.__end 47 curr = end[1] 48 curr[2] = end[1] = self.__map[key] = [key, curr, end] 49 dict.__setitem__(self, key, value)
50
51 - def __delitem__(self, key):
52 dict.__delitem__(self, key) 53 key, prev, next = self.__map.pop(key) 54 prev[2] = next 55 next[1] = prev
56
57 - def __iter__(self):
58 end = self.__end 59 curr = end[2] 60 while curr is not end: 61 yield curr[0] 62 curr = curr[2]
63
64 - def __reversed__(self):
65 end = self.__end 66 curr = end[1] 67 while curr is not end: 68 yield curr[0] 69 curr = curr[1]
70
71 - def popitem(self, last=True):
72 if not self: 73 raise KeyError('dictionary is empty') 74 if last: 75 key = reversed(self).next() 76 else: 77 key = iter(self).next() 78 value = self.pop(key) 79 return key, value
80
81 - def __reduce__(self):
82 items = [[k, self[k]] for k in self] 83 tmp = self.__map, self.__end 84 del self.__map, self.__end 85 inst_dict = vars(self).copy() 86 self.__map, self.__end = tmp 87 if inst_dict: 88 return (self.__class__, (items,), inst_dict) 89 return self.__class__, (items,)
90
91 - def keys(self):
92 return list(self)
93 94 setdefault = DictMixin.setdefault 95 update = DictMixin.update 96 pop = DictMixin.pop 97 values = DictMixin.values 98 items = DictMixin.items 99 iterkeys = DictMixin.iterkeys 100 itervalues = DictMixin.itervalues 101 iteritems = DictMixin.iteritems 102
103 - def __repr__(self):
104 if not self: 105 return '%s()' % (self.__class__.__name__,) 106 return '%s(%r)' % (self.__class__.__name__, self.items())
107
108 - def copy(self):
109 return self.__class__(self)
110 111 @classmethod
112 - def fromkeys(cls, iterable, value=None):
113 d = cls() 114 for key in iterable: 115 d[key] = value 116 return d
117
118 - def __eq__(self, other):
119 if isinstance(other, OrderedDict): 120 if len(self) != len(other): 121 return False 122 for p, q in zip(self.items(), other.items()): 123 if p != q: 124 return False 125 return True 126 return dict.__eq__(self, other)
127
128 - def __ne__(self, other):
129 return not self == other
130