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

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: Tests! Created 8 years, 2 months 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
« no previous file with comments | « tools/json_schema_compiler/model.py ('k') | tools/json_schema_compiler/ordered_dict_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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, items, func):
9 self._items = items
10 self._current = 0
11 self._func = func
12
13 def __iter__(self):
14 return self
15
16 def next(self):
17 if self._current > len(self._items) - 1:
18 raise StopIteration
19 else:
20 self._current += 1
21 return self._func(self._items[self._current - 1])
22
23 class OrderedDict(object):
24 """This class is used because it makes sense for the documentation to have the
25 functions, events, types, and properties in the order they were declared in
26 the schemas. In Python 2.6, there is no OrderedDict class, so this will be
27 used.
28 """
29 def __init__(self, items=None):
30 if items is not None:
31 self._list = [(k, v) for k, v in items]
32 self._dict = dict(self._list)
Aaron Boodman 2012/10/24 01:52:22 You should make one of these a pointer into the ot
cduvall 2012/10/26 01:09:57 Done.
33 else:
34 self._list = []
35 self._dict = {}
36
37 def __getitem__(self, key):
38 return self._dict[key]
39
40 def get(self, key, default=None):
41 if key not in self:
42 return default
43 return self[key]
44
45 def __setitem__(self, key, value):
46 self._dict[key] = value
47 if key in self:
Aaron Boodman 2012/10/24 01:52:22 Won't this always be true due to the above line?
cduvall 2012/10/26 01:09:57 Done.
48 for i, item in enumerate(self._list):
49 if item[0] == key:
50 self._list[i] = (key, value)
51 return
52 self._list.append((key, value))
53
54 def values(self):
55 return [v for k, v in self._list]
56
57 def keys(self):
58 return [k for k, v in self._list]
59
60 def iteritems(self):
61 return _OrderedDictIterator(self._list, lambda x: x)
62
63 def items(self):
64 return self._list[:]
65
66 def update(self, other):
67 self._dict.update(other)
Aaron Boodman 2012/10/24 01:52:22 Seems this is unnecessary?
cduvall 2012/10/26 01:09:57 Done.
68 for k, v in other.iteritems():
69 self[k] = v
70
71 def __iter__(self):
72 return _OrderedDictIterator(self._list, operator.itemgetter(0))
73
74 def __contains__(self, key):
75 return key in self._dict
76
77 def __repr__(self):
78 return 'OrderedDict(%s)' % str(self._list)
79
80 def pop(self, item):
81 del self._dict[item]
82 for i, k in enumerate(self.keys()):
83 if k == item:
84 return self._list.pop(i)[1]
85
86 def __delitem__(self, item):
87 self.pop(item)
88
89 def __eq__(self, other):
90 if isinstance(other, dict):
91 return dict(self._list) == other
92 elif isinstance(other, OrderedDict):
93 return self._list == other._list
94 return False
95
96 def __ne__(self, other):
97 return not (self == other)
98
99 def __len__(self):
100 return len(self._list)
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/model.py ('k') | tools/json_schema_compiler/ordered_dict_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698