Chromium Code Reviews| Index: tools/json_schema_compiler/ordered_dict.py |
| diff --git a/tools/json_schema_compiler/ordered_dict.py b/tools/json_schema_compiler/ordered_dict.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cac5392f64211f77488aa445fe1d648e45fe3825 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/ordered_dict.py |
| @@ -0,0 +1,92 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +class _OrderedDictIterator(object): |
| + def __init__(self, items): |
| + self._items = items |
| + self._current = 0 |
| + |
| + def next(self): |
| + if self._current > len(self._items) - 1: |
| + raise StopIteration |
| + else: |
| + self._current += 1 |
| + return self._items[self._current - 1][0] |
| + |
| +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.
|
| + """This class is used because it makes sense for the documentation to have the |
| + functions, events, types, and properties in the order they were declared in |
| + the schemas. In Python 2.6, there is no OrderedDict class, so this will be |
| + used. |
| + """ |
| + def __init__(self, items=None): |
| + if items is not None: |
| + self._dict = [(k, v) for k, v in items] |
| + else: |
| + self._dict = [] |
| + |
| + def __getitem__(self, key): |
| + for k, v in self._dict: |
| + if k == key: |
| + return v |
| + raise KeyError(key) |
| + |
| + def get(self, key, default=None): |
| + if key not in self: |
| + return default |
| + return self[key] |
| + |
| + def __setitem__(self, key, value): |
| + if key in self: |
| + for i, item in enumerate(self._dict): |
| + if item[0] == key: |
| + self._dict[i] = (key, value) |
| + return |
| + self._dict.append((key, value)) |
| + |
| + def values(self): |
| + return [v for k, v in self._dict] |
| + |
| + def keys(self): |
| + return [k for k, v in self._dict] |
| + |
| + def iteritems(self): |
| + return self._dict |
| + |
| + def items(self): |
| + return self._dict[:] |
| + |
| + def update(self, other): |
| + for k, v in other.iteritems(): |
| + self[k] = v |
| + |
| + def __iter__(self): |
| + return _OrderedDictIterator(self._dict) |
| + |
| + def __contains__(self, key): |
| + 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
|
| + |
| + def __repr__(self): |
| + return 'OrderedDict(%s)' % str(self._dict) |
| + |
| + def __delitem__(self, item): |
| + for i, k in enumerate(self.keys()): |
| + if k == item: |
| + self._dict.pop(i) |
| + |
| + def __eq__(self, other): |
| + if isinstance(other, dict): |
| + return dict(self._dict) == other |
| + 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.
|
| + for i, item in enumerate(self._dict): |
| + if other._dict[i] != item: |
| + return False |
| + return True |
| + return False |
| + |
| + def __ne__(self, other): |
| + return not (self == other) |
| + |
| + def __len__(self): |
| + return len(self._dict) |