1 """
2 Holds the KeyedList class.
3 """
4 import copy
5 import os
6
9
10
11 self.keyToIndex = {}
12 self.storedKeys = []
13
14 if hasattr(arg1, 'items'):
15 items = arg1.items()
16 else:
17 items = arg1
18
19 if items is not None:
20 for (key, value) in items:
21 self.set(key, value)
22
25
28
30 if len(order) != len(self):
31 raise ValueError, 'New order is of a different length!'
32
33 oldOrder = copy.copy(self.keyToIndex)
34 oldValues = copy.copy(self.values())
35 self.storedKeys = []
36 for (index, key) in enumerate(order):
37 self.keyToIndex[key] = index
38 self.storedKeys.append(key)
39 self[index] = oldValues[oldOrder[key]]
40
41 setOrder = set_order
42
45
49
51 list.__delitem__(self, index)
52 del self.storedKeys[index]
53 for key, ii in self.keyToIndex.items():
54 if ii > index:
55 self.keyToIndex[key] -= 1
56 elif ii == index:
57 del self.keyToIndex[key]
58
60
61 if stop > len(self):
62 stop = len(self)
63
64 for index in range(start, stop)[::-1]:
65 del self[index]
66
68 instance = self.__new__(self.__class__)
69 instance.__init__(self.items())
70 return instance
71
77
78
79
80
81 - def set(self, key, value):
82 try:
83 self[self.keyToIndex[key]] = value
84 except KeyError:
85 list.append(self, value)
86 self.storedKeys.append(key)
87 self.keyToIndex[key] = len(self)-1
88
90 return self.keyToIndex[key]
91
93 del self[self.keyToIndex[key]]
94
95 - def get(self, key, default = None):
96 try:
97 return self[self.keyToIndex[key]]
98 except KeyError:
99 return default
100
101 setByKey = set
102 getByKey = get
103 indexByKey = index_by_key
104 removeByKey = remove_by_key
105
108
110 if isinstance(other, dict) or isinstance(other, KeyedList):
111 for key, value in other.items():
112 self.set(key, value)
113 else:
114 if(len(self) != len(other)):
115 raise ValueError, 'Other list not of same length!'
116 for ii in range(len(self)):
117 self[ii] = other[ii]
118
120 return self.keyToIndex.has_key(key)
121
123 if not self.keyToIndex.has_key(key):
124 self.set(key, default)
125
128
131
133 return 'KeyedList(%s)' % repr(self.items())
134
136 return os.linesep.join(['['] +
137 [str(tup) + ',' for tup in self.items()] + [']'])
138
139
140
141
143 new_kl = self.copy()
144 new_kl += other
145 return new_kl
146
148 if not isinstance(other, self.__class__):
149 raise TypeError('Can only add another KeyedList to a KeyedList')
150 for k,v in other.items():
151 if not self.has_key(k):
152 self.set(k, v)
153 else:
154 raise ValueError('Addition would result in duplicated keys.')
155 return self
156
159
161 raise NotImplementedError
163 raise NotImplementedError
165 raise NotImplementedError
166
168 raise NotImplementedError
169
170 - def insert(self, index, object):
171 raise NotImplementedError
172
174 if self.has_key(key):
175 raise ValueError('Insertion would result in duplicated key: %s.'
176 % str(key))
177 list.insert(self, index, value)
178 self.storedKeys.insert(index, key)
179 for k, ii in self.keyToIndex.items():
180 if ii > index:
181 self.keyToIndex[k] += 1
182 self.keyToIndex[key] = index
183
185 val = self[index]
186 del self[index]
187 return val
188 pop = pop_value
189
191 key = self.storedKeys[index]
192 del self[index]
193 return key
194
196 item = (self.storedKeys[index], self[index])
197 del self[index]
198 return key
199
201 index = self.index(other)
202 del self[index]
203 remove = remove_by_value
204
212
214 """
215 Sort based on value of each entry
216 """
217 decorated = zip(self.values(), self.keys())
218 decorated.sort()
219 sorted_keys = [k for (v,k) in decorated]
220 self.set_order(sorted_keys)
221 sort = sort_by_value
222