OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 class _OrderedDictIterator(object): | |
6 def __init__(self, items): | |
7 self._items = items | |
8 self._current = 0 | |
9 | |
10 def next(self): | |
11 if self._current > len(self._items) - 1: | |
12 raise StopIteration | |
13 else: | |
14 self._current += 1 | |
15 return self._items[self._current - 1][0] | |
16 | |
17 class OrderedDict(object): | |
Aaron Boodman
2012/10/16 06:22:34
Makes me feel icky to have the O(n) stuff in here.
cduvall
2012/10/17 00:30:13
Done.
| |
18 """This class is used because it makes sense for the documentation to have the | |
19 functions, events, types, and properties in the order they were declared in | |
20 the schemas. In Python 2.6, there is no OrderedDict class, so this will be | |
21 used. | |
22 """ | |
23 def __init__(self, items=None): | |
24 if items is not None: | |
25 self._dict = [(k, v) for k, v in items] | |
26 else: | |
27 self._dict = [] | |
28 | |
29 def __getitem__(self, key): | |
30 for k, v in self._dict: | |
31 if k == key: | |
32 return v | |
33 raise KeyError(key) | |
34 | |
35 def get(self, key, default=None): | |
36 if key not in self: | |
37 return default | |
38 return self[key] | |
39 | |
40 def __setitem__(self, key, value): | |
41 if key in self: | |
42 for i, item in enumerate(self._dict): | |
43 if item[0] == key: | |
44 self._dict[i] = (key, value) | |
45 return | |
46 self._dict.append((key, value)) | |
47 | |
48 def values(self): | |
49 return [v for k, v in self._dict] | |
50 | |
51 def keys(self): | |
52 return [k for k, v in self._dict] | |
53 | |
54 def iteritems(self): | |
55 return self._dict | |
56 | |
57 def items(self): | |
58 return self._dict[:] | |
59 | |
60 def update(self, other): | |
61 for k, v in other.iteritems(): | |
62 self[k] = v | |
63 | |
64 def __iter__(self): | |
65 return _OrderedDictIterator(self._dict) | |
66 | |
67 def __contains__(self, key): | |
68 return any(key == k for k, v in self._dict) | |
Aaron Boodman
2012/10/16 06:22:34
Does any() stop on the first result?
cduvall
2012/10/17 00:30:13
Using the dict now, so any isn't used, but it does
| |
69 | |
70 def __repr__(self): | |
71 return 'OrderedDict(%s)' % str(self._dict) | |
72 | |
73 def __delitem__(self, item): | |
74 for i, k in enumerate(self.keys()): | |
75 if k == item: | |
76 self._dict.pop(i) | |
77 | |
78 def __eq__(self, other): | |
79 if isinstance(other, dict): | |
80 return dict(self._dict) == other | |
81 elif isinstance(other, OrderedDict): | |
Aaron Boodman
2012/10/16 06:22:34
Can you do self._dict == other._dict?
cduvall
2012/10/17 00:30:13
Done.
| |
82 for i, item in enumerate(self._dict): | |
83 if other._dict[i] != item: | |
84 return False | |
85 return True | |
86 return False | |
87 | |
88 def __ne__(self, other): | |
89 return not (self == other) | |
90 | |
91 def __len__(self): | |
92 return len(self._dict) | |
OLD | NEW |