1
2
3
4
5
6
7
9 """
10 A very rudamentary sorted dictionary, whose main purpose is to
11 allow dictionaries to be displayed in a consistent order in
12 regression tests. keys(), items(), values(), iter*(), and
13 __repr__ all sort their return values before returning them.
14 (note that the sort order for values() does *not* correspond to
15 the sort order for keys(). I.e., zip(d.keys(), d.values()) is not
16 necessarily equal to d.items().
17 """
28
29
30
31
33 """
34 This implementation of a dictionary keeps track of the order
35 in which keys were inserted.
36 """
37
41
45
54
58
60 for i in self._keys:
61 yield i, self[i]
62
65
67 if len(self._keys) == 0:
68 raise KeyError('dictionary is empty')
69 else:
70 key = self._keys[-1]
71 val = self[key]
72 del self[key]
73 return key, val
74
79
85
87 for i in self._keys:
88 yield self[i]
89
90 - def move(self, key, index):
91
92 """ Move the specified to key to *before* the specified index. """
93
94 try:
95 cur = self._keys.index(key)
96 except ValueError:
97 raise KeyError(key)
98 self._keys.insert(index, key)
99
100 if cur >= index: cur = cur + 1
101 del self._keys[cur]
102
107
108
109
110
111
112
113
114
115
117 """A Trie is like a dictionary in that it maps keys to
118 values. However, because of the way keys are stored, it allows
119 look up based on the longest prefix that matches. Keys must be
120 strings.
121 """
122
124 if trie is None:
125 self._root = [None, {}, 0]
126 else:
127 self._root = trie
128
130 self._root = [None, {}, 0]
131
133 """Return True if the key is present and it's a leaf of the
134 Trie, False otherwise."""
135
136 curr_node = self._root
137 for char in key:
138 curr_node_1 = curr_node[1]
139 if char in curr_node_1:
140 curr_node = curr_node_1[char]
141 else:
142 return False
143 return curr_node[0] is not None
144
146 """Find as much of the key as one can, by using the longest
147 prefix that has a value. Return (value, remainder) where
148 remainder is the rest of the given string."""
149
150 curr_node = self._root
151 remainder = key
152 for char in key:
153 if char in curr_node[1]:
154 curr_node = curr_node[1][char]
155 else:
156 return curr_node[0], remainder
157 remainder = remainder[1:]
158 return curr_node[0], remainder
159
161 curr_node = self._root
162 for char in key:
163 curr_node = curr_node[1][char]
164 return Trie(trie=curr_node)
165
168
170 return self._root == other._root
171
173 return not (self == other)
174
176 curr_node = self._root
177 for char in key:
178 curr_node[2] += 1
179 curr_node = curr_node[1].setdefault(char, [None, {}, 0])
180 curr_node[0] = value
181 curr_node[2] += 1
182
184 """Return the value for the given key if it is present, raises
185 a KeyError if key not found, and return None if it is present
186 a key2 that starts with key."""
187
188 curr_node = self._root
189 for char in key:
190 curr_node = curr_node[1][char]
191 return curr_node[0]
192
194 """Return True if the key is present or if it is present a
195 key2 string that starts with key."""
196
197 curr_node = self._root
198 for char in key:
199 curr_node_1 = curr_node[1]
200 if char in curr_node_1:
201 curr_node = curr_node_1[char]
202 else:
203 return False
204 return True
205
207 return str(self._root)
208
210 return "Trie(%r)" % self._root
211