Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: tools/json_schema_compiler/ordered_dict.py

Issue 11079010: Extensions Docs Server: Preserve JSON declaration order in extensions documentation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more efficient ordered_dict Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 import operator
6
7 class _OrderedDictIterator(object):
8 def __init__(self, keys, dict_, func):
9 self._keys = keys
10 self._dict = dict_
11 self._current = 0
12 self._func = func
13
14 def __iter__(self):
15 return self
16
17 def next(self):
18 if self._current > len(self._keys) - 1:
19 raise StopIteration
20 else:
21 self._current += 1
22 key = self._keys[self._current - 1]
23 return self._func((key, self._dict[key]))
24
25 class OrderedDict(object):
26 """This class is used because it makes sense for the documentation to have the
27 functions, events, types, and properties in the order they were declared in
28 the schemas. In Python 2.6, there is no OrderedDict class, so this will be
29 used.
30 """
31 def __init__(self, items=None):
32 if items is not None:
33 self._keys = [i[0] for i in items]
34 self._dict = dict(items)
35 else:
36 self._keys = []
37 self._dict = {}
38
39 def __getitem__(self, key):
40 return self._dict[key]
41
42 def get(self, key, default=None):
43 if key not in self:
44 return default
45 return self[key]
46
47 def __setitem__(self, key, value):
48 self._dict[key] = value
49 if key not in self._keys:
50 self._keys.append(key)
51
52 def values(self):
53 return [self._dict[k] for k in self._keys]
54
55 def keys(self):
56 return self._keys
57
58 def iteritems(self):
59 return _OrderedDictIterator(self._keys, self._dict, lambda x: x)
60
61 def items(self):
62 return [(k, self._dict[k]) for k in self._keys]
63
64 def update(self, other):
65 for k, v in other.iteritems():
66 self[k] = v
67
68 def __iter__(self):
69 return _OrderedDictIterator(self._keys, self._dict, operator.itemgetter(0))
70
71 def __contains__(self, key):
72 return key in self._keys
73
74 def __repr__(self):
75 return 'OrderedDict(%s)' % str(self.items())
76
77 def pop(self, item):
78 self._keys.remove(item)
79 return self._dict.pop(item)
80
81 def __delitem__(self, item):
82 self.pop(item)
83
84 def __eq__(self, other):
85 if isinstance(other, dict):
86 return self._dict == other
87 elif isinstance(other, OrderedDict):
88 return self._keys == other._keys and self._dict == other._dict
89 return False
90
91 def __ne__(self, other):
92 return not (self == other)
93
94 def __len__(self):
95 return len(self._keys)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698