annotate martini/odict.py @ 12:4faed08eb8d8

py35
author Jeff Hammel <k0scist@gmail.com>
date Mon, 20 Feb 2017 14:34:18 -0800
parents 81aed4352851
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 # odict.py
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 # An Ordered Dictionary object
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 # Copyright (C) 2005 Nicola Larosa, Michael Foord
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 # E-mail: nico AT tekNico DOT net, fuzzyman AT voidspace DOT org DOT uk
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 # This software is licensed under the terms of the BSD license.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 # http://www.voidspace.org.uk/python/license.shtml
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 # Basically you're free to copy, modify, distribute and relicense it,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 # So long as you keep a copy of the license with it.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 # Documentation at http://www.voidspace.org.uk/python/odict.html
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 # For information about bugfixes, updates and support, please join the
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 # Pythonutils mailing list:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 # http://groups.google.com/group/pythonutils/
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 # Comments, suggestions and bug reports welcome.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 """A dict that keeps keys in insertion order"""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 from __future__ import generators
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 __author__ = ('Nicola Larosa <nico-NoSp@m-tekNico.net>,'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 'Michael Foord <fuzzyman AT voidspace DOT org DOT uk>')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 __docformat__ = "restructuredtext en"
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 __revision__ = '$Id: odict.py 129 2005-09-12 18:15:28Z teknico $'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 __version__ = '0.2.2'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 __all__ = ['OrderedDict', 'SequenceOrderedDict']
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 import sys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 INTP_VER = sys.version_info[:2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 if INTP_VER < (2, 2):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 raise RuntimeError("Python v.2.2 or later required")
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 import types, warnings
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 class OrderedDict(dict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 A class of dictionary that keeps the insertion order of keys.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 All appropriate methods return keys, items, or values in an ordered way.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 All normal dictionary methods are available. Update and comparison is
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 restricted to other OrderedDict objects.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 Various sequence methods are available, including the ability to explicitly
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 mutate the key ordering.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 __contains__ tests:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 >>> d = OrderedDict(((1, 3),))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 >>> 1 in d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 >>> 4 in d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 __getitem__ tests:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 >>> OrderedDict(((1, 3), (3, 2), (2, 1)))[2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 >>> OrderedDict(((1, 3), (3, 2), (2, 1)))[4]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 KeyError: 4
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 __len__ tests:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 >>> len(OrderedDict())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 >>> len(OrderedDict(((1, 3), (3, 2), (2, 1))))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 get tests:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 >>> d.get(1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 >>> d.get(4) is None
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 >>> d.get(4, 5)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 5
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 OrderedDict([(1, 3), (3, 2), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 has_key tests:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 >>> d.has_key(1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 >>> d.has_key(4)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 def __init__(self, init_val=(), strict=False):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 Create a new ordered dictionary. Cannot init from a normal dict,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 nor from kwargs, since items order is undefined in those cases.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 If the ``strict`` keyword argument is ``True`` (``False`` is the
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 default) then when doing slice assignment - the ``OrderedDict`` you are
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101 assigning from *must not* contain any keys in the remaining dict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103 >>> OrderedDict()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104 OrderedDict([])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 >>> OrderedDict({1: 1})
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107 TypeError: undefined order, cannot get items from dict
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 >>> OrderedDict({1: 1}.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 OrderedDict([(1, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 OrderedDict([(1, 3), (3, 2), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113 >>> OrderedDict(d)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 OrderedDict([(1, 3), (3, 2), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 self.strict = strict
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117 dict.__init__(self)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118 if isinstance(init_val, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
119 self._sequence = init_val.keys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
120 dict.update(self, init_val)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
121 elif isinstance(init_val, dict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
122 # we lose compatibility with other ordered dict types this way
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
123 raise TypeError('undefined order, cannot get items from dict')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
124 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
125 self._sequence = []
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
126 self.update(init_val)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
127
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
128 ### Special methods ###
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
129
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
130 def __delitem__(self, key):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
131 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
132 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
133 >>> del d[3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
134 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
135 OrderedDict([(1, 3), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
136 >>> del d[3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
137 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
138 KeyError: 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
139 >>> d[3] = 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
140 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
141 OrderedDict([(1, 3), (2, 1), (3, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
142 >>> del d[0:1]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
143 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
144 OrderedDict([(2, 1), (3, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
145 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
146 if isinstance(key, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
147 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
148 keys = self._sequence[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
149 for entry in keys:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
150 dict.__delitem__(self, entry)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
151 del self._sequence[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
152 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
153 # do the dict.__delitem__ *first* as it raises
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
154 # the more appropriate error
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
155 dict.__delitem__(self, key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
156 self._sequence.remove(key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
157
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
158 def __eq__(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
159 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
160 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
161 >>> d == OrderedDict(d)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
162 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
163 >>> d == OrderedDict(((1, 3), (2, 1), (3, 2)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
164 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
165 >>> d == OrderedDict(((1, 0), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
166 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
167 >>> d == OrderedDict(((0, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
168 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
169 >>> d == dict(d)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
170 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
171 >>> d == False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
172 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
173 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
174 if isinstance(other, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
175 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
176 # Generate both item lists for each compare
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
177 return (self.items() == other.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
178 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
179 return False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
180
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
181 def __lt__(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
182 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
183 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
184 >>> c = OrderedDict(((0, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
185 >>> c < d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
186 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
187 >>> d < c
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
188 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
189 >>> d < dict(c)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
190 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
191 TypeError: Can only compare with other OrderedDicts
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
192 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
193 if not isinstance(other, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
194 raise TypeError('Can only compare with other OrderedDicts')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
195 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
196 # Generate both item lists for each compare
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
197 return (self.items() < other.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
198
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
199 def __le__(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
200 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
201 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
202 >>> c = OrderedDict(((0, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
203 >>> e = OrderedDict(d)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
204 >>> c <= d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
205 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
206 >>> d <= c
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
207 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
208 >>> d <= dict(c)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
209 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
210 TypeError: Can only compare with other OrderedDicts
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
211 >>> d <= e
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
212 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
213 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
214 if not isinstance(other, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
215 raise TypeError('Can only compare with other OrderedDicts')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
216 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
217 # Generate both item lists for each compare
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
218 return (self.items() <= other.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
219
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
220 def __ne__(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
221 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
222 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
223 >>> d != OrderedDict(d)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
224 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
225 >>> d != OrderedDict(((1, 3), (2, 1), (3, 2)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
226 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
227 >>> d != OrderedDict(((1, 0), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
228 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
229 >>> d == OrderedDict(((0, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
230 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
231 >>> d != dict(d)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
232 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
233 >>> d != False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
234 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
235 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
236 if isinstance(other, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
237 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
238 # Generate both item lists for each compare
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
239 return not (self.items() == other.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
240 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
241 return True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
242
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
243 def __gt__(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
244 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
245 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
246 >>> c = OrderedDict(((0, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
247 >>> d > c
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
248 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
249 >>> c > d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
250 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
251 >>> d > dict(c)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
252 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
253 TypeError: Can only compare with other OrderedDicts
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
254 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
255 if not isinstance(other, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
256 raise TypeError('Can only compare with other OrderedDicts')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
257 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
258 # Generate both item lists for each compare
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
259 return (self.items() > other.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
260
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
261 def __ge__(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
262 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
263 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
264 >>> c = OrderedDict(((0, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
265 >>> e = OrderedDict(d)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
266 >>> c >= d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
267 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
268 >>> d >= c
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
269 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
270 >>> d >= dict(c)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
271 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
272 TypeError: Can only compare with other OrderedDicts
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
273 >>> e >= d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
274 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
275 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
276 if not isinstance(other, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
277 raise TypeError('Can only compare with other OrderedDicts')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
278 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
279 # Generate both item lists for each compare
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
280 return (self.items() >= other.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
281
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
282 def __repr__(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
283 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
284 Used for __repr__ and __str__
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
285
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
286 >>> r1 = repr(OrderedDict((('a', 'b'), ('c', 'd'), ('e', 'f'))))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
287 >>> r1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
288 "OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f')])"
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
289 >>> r2 = repr(OrderedDict((('a', 'b'), ('e', 'f'), ('c', 'd'))))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
290 >>> r2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
291 "OrderedDict([('a', 'b'), ('e', 'f'), ('c', 'd')])"
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
292 >>> r1 == str(OrderedDict((('a', 'b'), ('c', 'd'), ('e', 'f'))))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
293 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
294 >>> r2 == str(OrderedDict((('a', 'b'), ('e', 'f'), ('c', 'd'))))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
295 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
296 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
297 return '%s([%s])' % (self.__class__.__name__, ', '.join(
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
298 ['(%r, %r)' % (key, self[key]) for key in self._sequence]))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
299
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
300 def __setitem__(self, key, val):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
301 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
302 Allows slice assignment, so long as the slice is an OrderedDict
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
303 >>> d = OrderedDict()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
304 >>> d['a'] = 'b'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
305 >>> d['b'] = 'a'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
306 >>> d[3] = 12
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
307 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
308 OrderedDict([('a', 'b'), ('b', 'a'), (3, 12)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
309 >>> d[:] = OrderedDict(((1, 2), (2, 3), (3, 4)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
310 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
311 OrderedDict([(1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
312 >>> d[::2] = OrderedDict(((7, 8), (9, 10)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
313 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
314 OrderedDict([(7, 8), (2, 3), (9, 10)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
315 >>> d = OrderedDict(((0, 1), (1, 2), (2, 3), (3, 4)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
316 >>> d[1:3] = OrderedDict(((1, 2), (5, 6), (7, 8)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
317 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
318 OrderedDict([(0, 1), (1, 2), (5, 6), (7, 8), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
319 >>> d = OrderedDict(((0, 1), (1, 2), (2, 3), (3, 4)), strict=True)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
320 >>> d[1:3] = OrderedDict(((1, 2), (5, 6), (7, 8)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
321 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
322 OrderedDict([(0, 1), (1, 2), (5, 6), (7, 8), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
323
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
324 >>> a = OrderedDict(((0, 1), (1, 2), (2, 3)), strict=True)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
325 >>> a[3] = 4
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
326 >>> a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
327 OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
328 >>> a[::1] = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
329 >>> a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
330 OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
331 >>> a[:2] = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
332 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
333 ValueError: slice assignment must be from unique keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
334 >>> a = OrderedDict(((0, 1), (1, 2), (2, 3)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
335 >>> a[3] = 4
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
336 >>> a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
337 OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
338 >>> a[::1] = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
339 >>> a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
340 OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
341 >>> a[:2] = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
342 >>> a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
343 OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
344 >>> a[::-1] = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
345 >>> a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
346 OrderedDict([(3, 4), (2, 3), (1, 2), (0, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
347
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
348 >>> d = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
349 >>> d[:1] = 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
350 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
351 TypeError: slice assignment requires an OrderedDict
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
352
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
353 >>> d = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
354 >>> d[:1] = OrderedDict([(9, 8)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
355 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
356 OrderedDict([(9, 8), (1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
357 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
358 if isinstance(key, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
359 if not isinstance(val, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
360 # FIXME: allow a list of tuples?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
361 raise TypeError('slice assignment requires an OrderedDict')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
362 keys = self._sequence[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
363 # NOTE: Could use ``range(*key.indices(len(self._sequence)))``
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
364 indexes = range(len(self._sequence))[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
365 if key.step is None:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
366 # NOTE: new slice may not be the same size as the one being
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
367 # overwritten !
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
368 # NOTE: What is the algorithm for an impossible slice?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
369 # e.g. d[5:3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
370 pos = key.start or 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
371 del self[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
372 newkeys = val.keys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
373 for k in newkeys:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
374 if k in self:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
375 if self.strict:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
376 raise ValueError('slice assignment must be from '
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
377 'unique keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
378 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
379 # NOTE: This removes duplicate keys *first*
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
380 # so start position might have changed?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
381 del self[k]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
382 self._sequence = (self._sequence[:pos] + newkeys +
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
383 self._sequence[pos:])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
384 dict.update(self, val)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
385 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
386 # extended slice - length of new slice must be the same
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
387 # as the one being replaced
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
388 if len(keys) != len(val):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
389 raise ValueError('attempt to assign sequence of size %s '
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
390 'to extended slice of size %s' % (len(val), len(keys)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
391 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
392 del self[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
393 item_list = zip(indexes, val.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
394 # smallest indexes first - higher indexes not guaranteed to
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
395 # exist
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
396 item_list.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
397 for pos, (newkey, newval) in item_list:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
398 if self.strict and newkey in self:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
399 raise ValueError('slice assignment must be from unique'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
400 ' keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
401 self.insert(pos, newkey, newval)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
402 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
403 if key not in self:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
404 self._sequence.append(key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
405 dict.__setitem__(self, key, val)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
406
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
407 def __getitem__(self, key):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
408 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
409 Allows slicing. Returns an OrderedDict if you slice.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
410 >>> b = OrderedDict([(7, 0), (6, 1), (5, 2), (4, 3), (3, 4), (2, 5), (1, 6)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
411 >>> b[::-1]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
412 OrderedDict([(1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1), (7, 0)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
413 >>> b[2:5]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
414 OrderedDict([(5, 2), (4, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
415 >>> type(b[2:4])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
416 <class '__main__.OrderedDict'>
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
417 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
418 if isinstance(key, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
419 # FIXME: does this raise the error we want?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
420 keys = self._sequence[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
421 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
422 return OrderedDict([(entry, self[entry]) for entry in keys])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
423 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
424 return dict.__getitem__(self, key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
425
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
426 __str__ = __repr__
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
427
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
428 def __setattr__(self, name, value):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
429 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
430 Implemented so that accesses to ``sequence`` raise a warning and are
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
431 diverted to the new ``setkeys`` method.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
432 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
433 if name == 'sequence':
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
434 warnings.warn('Use of the sequence attribute is deprecated.'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
435 ' Use the keys method instead.', DeprecationWarning)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
436 # NOTE: doesn't return anything
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
437 self.setkeys(value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
438 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
439 # FIXME: do we want to allow arbitrary setting of attributes?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
440 # Or do we want to manage it?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
441 object.__setattr__(self, name, value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
442
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
443 def __getattr__(self, name):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
444 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
445 Implemented so that access to ``sequence`` raises a warning.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
446
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
447 >>> d = OrderedDict()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
448 >>> d.sequence
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
449 []
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
450 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
451 if name == 'sequence':
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
452 warnings.warn('Use of the sequence attribute is deprecated.'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
453 ' Use the keys method instead.', DeprecationWarning)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
454 # NOTE: Still (currently) returns a direct reference. Need to
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
455 # because code that uses sequence will expect to be able to
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
456 # mutate it in place.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
457 return self._sequence
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
458 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
459 # raise the appropriate error
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
460 raise AttributeError("OrderedDict has no '%s' attribute" % name)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
461
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
462 def __deepcopy__(self, memo):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
463 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
464 To allow deepcopy to work with OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
465
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
466 >>> from copy import deepcopy
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
467 >>> a = OrderedDict([(1, 1), (2, 2), (3, 3)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
468 >>> a['test'] = {}
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
469 >>> b = deepcopy(a)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
470 >>> b == a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
471 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
472 >>> b is a
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
473 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
474 >>> a['test'] is b['test']
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
475 False
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
476 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
477 from copy import deepcopy
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
478 return self.__class__(deepcopy(self.items(), memo), self.strict)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
479
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
480
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
481 ### Read-only methods ###
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
482
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
483 def copy(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
484 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
485 >>> OrderedDict(((1, 3), (3, 2), (2, 1))).copy()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
486 OrderedDict([(1, 3), (3, 2), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
487 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
488 return OrderedDict(self)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
489
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
490 def items(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
491 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
492 ``items`` returns a list of tuples representing all the
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
493 ``(key, value)`` pairs in the dictionary.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
494
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
495 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
496 >>> d.items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
497 [(1, 3), (3, 2), (2, 1)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
498 >>> d.clear()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
499 >>> d.items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
500 []
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
501 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
502 return zip(self._sequence, self.values())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
503
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
504 def keys(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
505 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
506 Return a list of keys in the ``OrderedDict``.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
507
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
508 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
509 >>> d.keys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
510 [1, 3, 2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
511 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
512 return self._sequence[:]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
513
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
514 def values(self, values=None):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
515 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
516 Return a list of all the values in the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
517
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
518 Optionally you can pass in a list of values, which will replace the
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
519 current list. The value list must be the same len as the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
520
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
521 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
522 >>> d.values()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
523 [3, 2, 1]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
524 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
525 return [self[key] for key in self._sequence]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
526
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
527 def iteritems(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
528 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
529 >>> ii = OrderedDict(((1, 3), (3, 2), (2, 1))).iteritems()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
530 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
531 (1, 3)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
532 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
533 (3, 2)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
534 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
535 (2, 1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
536 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
537 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
538 StopIteration
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
539 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
540 def make_iter(self=self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
541 keys = self.iterkeys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
542 while True:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
543 key = keys.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
544 yield (key, self[key])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
545 return make_iter()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
546
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
547 def iterkeys(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
548 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
549 >>> ii = OrderedDict(((1, 3), (3, 2), (2, 1))).iterkeys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
550 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
551 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
552 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
553 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
554 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
555 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
556 >>> ii.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
557 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
558 StopIteration
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
559 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
560 return iter(self._sequence)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
561
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
562 __iter__ = iterkeys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
563
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
564 def itervalues(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
565 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
566 >>> iv = OrderedDict(((1, 3), (3, 2), (2, 1))).itervalues()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
567 >>> iv.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
568 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
569 >>> iv.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
570 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
571 >>> iv.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
572 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
573 >>> iv.next()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
574 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
575 StopIteration
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
576 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
577 def make_iter(self=self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
578 keys = self.iterkeys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
579 while True:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
580 yield self[keys.next()]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
581 return make_iter()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
582
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
583 ### Read-write methods ###
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
584
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
585 def clear(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
586 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
587 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
588 >>> d.clear()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
589 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
590 OrderedDict([])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
591 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
592 dict.clear(self)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
593 self._sequence = []
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
594
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
595 def pop(self, key, *args):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
596 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
597 No dict.pop in Python 2.2, gotta reimplement it
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
598
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
599 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
600 >>> d.pop(3)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
601 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
602 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
603 OrderedDict([(1, 3), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
604 >>> d.pop(4)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
605 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
606 KeyError: 4
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
607 >>> d.pop(4, 0)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
608 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
609 >>> d.pop(4, 0, 1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
610 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
611 TypeError: pop expected at most 2 arguments, got 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
612 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
613 if len(args) > 1:
12
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
614 raise TypeError('pop expected at most 2 arguments, got %s' %
8
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
615 (len(args) + 1))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
616 if key in self:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
617 val = self[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
618 del self[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
619 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
620 try:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
621 val = args[0]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
622 except IndexError:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
623 raise KeyError(key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
624 return val
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
625
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
626 def popitem(self, i=-1):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
627 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
628 Delete and return an item specified by index, not a random one as in
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
629 dict. The index is -1 by default (the last item).
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
630
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
631 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
632 >>> d.popitem()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
633 (2, 1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
634 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
635 OrderedDict([(1, 3), (3, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
636 >>> d.popitem(0)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
637 (1, 3)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
638 >>> OrderedDict().popitem()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
639 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
640 KeyError: 'popitem(): dictionary is empty'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
641 >>> d.popitem(2)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
642 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
643 IndexError: popitem(): index 2 not valid
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
644 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
645 if not self._sequence:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
646 raise KeyError('popitem(): dictionary is empty')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
647 try:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
648 key = self._sequence[i]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
649 except IndexError:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
650 raise IndexError('popitem(): index %s not valid' % i)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
651 return (key, self.pop(key))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
652
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
653 def setdefault(self, key, defval = None):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
654 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
655 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
656 >>> d.setdefault(1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
657 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
658 >>> d.setdefault(4) is None
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
659 True
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
660 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
661 OrderedDict([(1, 3), (3, 2), (2, 1), (4, None)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
662 >>> d.setdefault(5, 0)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
663 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
664 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
665 OrderedDict([(1, 3), (3, 2), (2, 1), (4, None), (5, 0)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
666 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
667 if key in self:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
668 return self[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
669 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
670 self[key] = defval
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
671 return defval
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
672
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
673 def update(self, from_od):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
674 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
675 Update from another OrderedDict or sequence of (key, value) pairs
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
676
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
677 >>> d = OrderedDict(((1, 0), (0, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
678 >>> d.update(OrderedDict(((1, 3), (3, 2), (2, 1))))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
679 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
680 OrderedDict([(1, 3), (0, 1), (3, 2), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
681 >>> d.update({4: 4})
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
682 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
683 TypeError: undefined order, cannot get items from dict
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
684 >>> d.update((4, 4))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
685 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
686 TypeError: cannot convert dictionary update sequence element "4" to a 2-item sequence
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
687 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
688 if isinstance(from_od, OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
689 for key, val in from_od.items():
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
690 self[key] = val
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
691 elif isinstance(from_od, dict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
692 # we lose compatibility with other ordered dict types this way
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
693 raise TypeError('undefined order, cannot get items from dict')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
694 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
695 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
696 # sequence of 2-item sequences, or error
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
697 for item in from_od:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
698 try:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
699 key, val = item
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
700 except TypeError:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
701 raise TypeError('cannot convert dictionary update'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
702 ' sequence element "%s" to a 2-item sequence' % item)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
703 self[key] = val
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
704
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
705 def rename(self, old_key, new_key):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
706 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
707 Rename the key for a given value, without modifying sequence order.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
708
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
709 For the case where new_key already exists this raise an exception,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
710 since if new_key exists, it is ambiguous as to what happens to the
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
711 associated values, and the position of new_key in the sequence.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
712
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
713 >>> od = OrderedDict()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
714 >>> od['a'] = 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
715 >>> od['b'] = 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
716 >>> od.items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
717 [('a', 1), ('b', 2)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
718 >>> od.rename('b', 'c')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
719 >>> od.items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
720 [('a', 1), ('c', 2)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
721 >>> od.rename('c', 'a')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
722 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
723 ValueError: New key already exists: 'a'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
724 >>> od.rename('d', 'b')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
725 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
726 KeyError: 'd'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
727 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
728 if new_key == old_key:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
729 # no-op
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
730 return
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
731 if new_key in self:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
732 raise ValueError("New key already exists: %r" % new_key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
733 # rename sequence entry
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
734 value = self[old_key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
735 old_idx = self._sequence.index(old_key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
736 self._sequence[old_idx] = new_key
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
737 # rename internal dict entry
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
738 dict.__delitem__(self, old_key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
739 dict.__setitem__(self, new_key, value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
740
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
741 def setitems(self, items):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
742 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
743 This method allows you to set the items in the dict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
744
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
745 It takes a list of tuples - of the same sort returned by the ``items``
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
746 method.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
747
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
748 >>> d = OrderedDict()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
749 >>> d.setitems(((3, 1), (2, 3), (1, 2)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
750 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
751 OrderedDict([(3, 1), (2, 3), (1, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
752 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
753 self.clear()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
754 # FIXME: this allows you to pass in an OrderedDict as well :-)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
755 self.update(items)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
756
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
757 def setkeys(self, keys):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
758 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
759 ``setkeys`` all ows you to pass in a new list of keys which will
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
760 replace the current set. This must contain the same set of keys, but
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
761 need not be in the same order.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
762
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
763 If you pass in new keys that don't match, a ``KeyError`` will be
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
764 raised.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
765
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
766 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
767 >>> d.keys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
768 [1, 3, 2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
769 >>> d.setkeys((1, 2, 3))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
770 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
771 OrderedDict([(1, 3), (2, 1), (3, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
772 >>> d.setkeys(['a', 'b', 'c'])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
773 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
774 KeyError: 'Keylist is not the same as current keylist.'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
775 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
776 # FIXME: Efficiency? (use set for Python 2.4 :-)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
777 # NOTE: list(keys) rather than keys[:] because keys[:] returns
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
778 # a tuple, if keys is a tuple.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
779 kcopy = list(keys)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
780 kcopy.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
781 self._sequence.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
782 if kcopy != self._sequence:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
783 raise KeyError('Keylist is not the same as current keylist.')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
784 # NOTE: This makes the _sequence attribute a new object, instead
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
785 # of changing it in place.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
786 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
787 self._sequence = list(keys)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
788
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
789 def setvalues(self, values):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
790 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
791 You can pass in a list of values, which will replace the
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
792 current list. The value list must be the same len as the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
793
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
794 (Or a ``ValueError`` is raised.)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
795
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
796 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
797 >>> d.setvalues((1, 2, 3))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
798 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
799 OrderedDict([(1, 1), (3, 2), (2, 3)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
800 >>> d.setvalues([6])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
801 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
802 ValueError: Value list is not the same length as the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
803 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
804 if len(values) != len(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
805 # FIXME: correct error to raise?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
806 raise ValueError('Value list is not the same length as the '
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
807 'OrderedDict.')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
808 self.update(zip(self, values))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
809
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
810 ### Sequence Methods ###
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
811
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
812 def index(self, key):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
813 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
814 Return the position of the specified key in the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
815
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
816 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
817 >>> d.index(3)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
818 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
819 >>> d.index(4)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
820 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
821 ValueError: list.index(x): x not in list
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
822 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
823 return self._sequence.index(key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
824
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
825 def insert(self, index, key, value):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
826 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
827 Takes ``index``, ``key``, and ``value`` as arguments.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
828
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
829 Sets ``key`` to ``value``, so that ``key`` is at position ``index`` in
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
830 the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
831
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
832 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
833 >>> d.insert(0, 4, 0)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
834 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
835 OrderedDict([(4, 0), (1, 3), (3, 2), (2, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
836 >>> d.insert(0, 2, 1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
837 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
838 OrderedDict([(2, 1), (4, 0), (1, 3), (3, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
839 >>> d.insert(8, 8, 1)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
840 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
841 OrderedDict([(2, 1), (4, 0), (1, 3), (3, 2), (8, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
842 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
843 if key in self:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
844 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
845 del self[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
846 self._sequence.insert(index, key)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
847 dict.__setitem__(self, key, value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
848
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
849 def reverse(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
850 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
851 Reverse the order of the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
852
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
853 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
854 >>> d.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
855 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
856 OrderedDict([(2, 1), (3, 2), (1, 3)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
857 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
858 self._sequence.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
859
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
860 def sort(self, *args, **kwargs):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
861 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
862 Sort the key order in the OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
863
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
864 This method takes the same arguments as the ``list.sort`` method on
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
865 your version of Python.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
866
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
867 >>> d = OrderedDict(((4, 1), (2, 2), (3, 3), (1, 4)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
868 >>> d.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
869 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
870 OrderedDict([(1, 4), (2, 2), (3, 3), (4, 1)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
871 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
872 self._sequence.sort(*args, **kwargs)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
873
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
874 class Keys(object):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
875 # FIXME: should this object be a subclass of list?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
876 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
877 Custom object for accessing the keys of an OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
878
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
879 Can be called like the normal ``OrderedDict.keys`` method, but also
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
880 supports indexing and sequence methods.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
881 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
882
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
883 def __init__(self, main):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
884 self._main = main
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
885
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
886 def __call__(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
887 """Pretend to be the keys method."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
888 return self._main._keys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
889
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
890 def __getitem__(self, index):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
891 """Fetch the key at position i."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
892 # NOTE: this automatically supports slicing :-)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
893 return self._main._sequence[index]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
894
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
895 def __setitem__(self, index, name):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
896 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
897 You cannot assign to keys, but you can do slice assignment to re-order
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
898 them.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
899
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
900 You can only do slice assignment if the new set of keys is a reordering
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
901 of the original set.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
902 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
903 if isinstance(index, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
904 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
905 # check length is the same
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
906 indexes = range(len(self._main._sequence))[index]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
907 if len(indexes) != len(name):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
908 raise ValueError('attempt to assign sequence of size %s '
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
909 'to slice of size %s' % (len(name), len(indexes)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
910 # check they are the same keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
911 # FIXME: Use set
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
912 old_keys = self._main._sequence[index]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
913 new_keys = list(name)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
914 old_keys.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
915 new_keys.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
916 if old_keys != new_keys:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
917 raise KeyError('Keylist is not the same as current keylist.')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
918 orig_vals = [self._main[k] for k in name]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
919 del self._main[index]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
920 vals = zip(indexes, name, orig_vals)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
921 vals.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
922 for i, k, v in vals:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
923 if self._main.strict and k in self._main:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
924 raise ValueError('slice assignment must be from '
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
925 'unique keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
926 self._main.insert(i, k, v)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
927 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
928 raise ValueError('Cannot assign to keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
929
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
930 ### following methods pinched from UserList and adapted ###
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
931 def __repr__(self): return repr(self._main._sequence)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
932
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
933 # FIXME: do we need to check if we are comparing with another ``Keys``
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
934 # object? (like the __cast method of UserList)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
935 def __lt__(self, other): return self._main._sequence < other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
936 def __le__(self, other): return self._main._sequence <= other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
937 def __eq__(self, other): return self._main._sequence == other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
938 def __ne__(self, other): return self._main._sequence != other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
939 def __gt__(self, other): return self._main._sequence > other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
940 def __ge__(self, other): return self._main._sequence >= other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
941 # FIXME: do we need __cmp__ as well as rich comparisons?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
942 def __cmp__(self, other): return cmp(self._main._sequence, other)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
943
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
944 def __contains__(self, item): return item in self._main._sequence
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
945 def __len__(self): return len(self._main._sequence)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
946 def __iter__(self): return self._main.iterkeys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
947 def count(self, item): return self._main._sequence.count(item)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
948 def index(self, item, *args): return self._main._sequence.index(item, *args)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
949 def reverse(self): self._main._sequence.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
950 def sort(self, *args, **kwds): self._main._sequence.sort(*args, **kwds)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
951 def __mul__(self, n): return self._main._sequence*n
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
952 __rmul__ = __mul__
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
953 def __add__(self, other): return self._main._sequence + other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
954 def __radd__(self, other): return other + self._main._sequence
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
955
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
956 ## following methods not implemented for keys ##
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
957 def __delitem__(self, i): raise TypeError('Can\'t delete items from keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
958 def __iadd__(self, other): raise TypeError('Can\'t add in place to keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
959 def __imul__(self, n): raise TypeError('Can\'t multiply keys in place')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
960 def append(self, item): raise TypeError('Can\'t append items to keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
961 def insert(self, i, item): raise TypeError('Can\'t insert items into keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
962 def pop(self, i=-1): raise TypeError('Can\'t pop items from keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
963 def remove(self, item): raise TypeError('Can\'t remove items from keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
964 def extend(self, other): raise TypeError('Can\'t extend keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
965
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
966 class Items(object):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
967 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
968 Custom object for accessing the items of an OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
969
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
970 Can be called like the normal ``OrderedDict.items`` method, but also
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
971 supports indexing and sequence methods.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
972 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
973
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
974 def __init__(self, main):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
975 self._main = main
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
976
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
977 def __call__(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
978 """Pretend to be the items method."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
979 return self._main._items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
980
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
981 def __getitem__(self, index):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
982 """Fetch the item at position i."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
983 if isinstance(index, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
984 # fetching a slice returns an OrderedDict
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
985 return self._main[index].items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
986 key = self._main._sequence[index]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
987 return (key, self._main[key])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
988
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
989 def __setitem__(self, index, item):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
990 """Set item at position i to item."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
991 if isinstance(index, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
992 # NOTE: item must be an iterable (list of tuples)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
993 self._main[index] = OrderedDict(item)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
994 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
995 # FIXME: Does this raise a sensible error?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
996 orig = self._main.keys[index]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
997 key, value = item
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
998 if self._main.strict and key in self and (key != orig):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
999 raise ValueError('slice assignment must be from '
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1000 'unique keys')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1001 # delete the current one
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1002 del self._main[self._main._sequence[index]]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1003 self._main.insert(index, key, value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1004
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1005 def __delitem__(self, i):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1006 """Delete the item at position i."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1007 key = self._main._sequence[i]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1008 if isinstance(i, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1009 for k in key:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1010 # FIXME: efficiency?
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1011 del self._main[k]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1012 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1013 del self._main[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1014
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1015 ### following methods pinched from UserList and adapted ###
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1016 def __repr__(self): return repr(self._main.items())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1017
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1018 # FIXME: do we need to check if we are comparing with another ``Items``
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1019 # object? (like the __cast method of UserList)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1020 def __lt__(self, other): return self._main.items() < other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1021 def __le__(self, other): return self._main.items() <= other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1022 def __eq__(self, other): return self._main.items() == other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1023 def __ne__(self, other): return self._main.items() != other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1024 def __gt__(self, other): return self._main.items() > other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1025 def __ge__(self, other): return self._main.items() >= other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1026 def __cmp__(self, other): return cmp(self._main.items(), other)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1027
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1028 def __contains__(self, item): return item in self._main.items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1029 def __len__(self): return len(self._main._sequence) # easier :-)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1030 def __iter__(self): return self._main.iteritems()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1031 def count(self, item): return self._main.items().count(item)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1032 def index(self, item, *args): return self._main.items().index(item, *args)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1033 def reverse(self): self._main.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1034 def sort(self, *args, **kwds): self._main.sort(*args, **kwds)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1035 def __mul__(self, n): return self._main.items()*n
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1036 __rmul__ = __mul__
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1037 def __add__(self, other): return self._main.items() + other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1038 def __radd__(self, other): return other + self._main.items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1039
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1040 def append(self, item):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1041 """Add an item to the end."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1042 # FIXME: this is only append if the key isn't already present
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1043 key, value = item
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1044 self._main[key] = value
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1045
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1046 def insert(self, i, item):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1047 key, value = item
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1048 self._main.insert(i, key, value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1049
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1050 def pop(self, i=-1):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1051 key = self._main._sequence[i]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1052 return (key, self._main.pop(key))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1053
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1054 def remove(self, item):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1055 key, value = item
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1056 try:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1057 assert value == self._main[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1058 except (KeyError, AssertionError):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1059 raise ValueError('ValueError: list.remove(x): x not in list')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1060 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1061 del self._main[key]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1062
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1063 def extend(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1064 # FIXME: is only a true extend if none of the keys already present
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1065 for item in other:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1066 key, value = item
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1067 self._main[key] = value
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1068
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1069 def __iadd__(self, other):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1070 self.extend(other)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1071
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1072 ## following methods not implemented for items ##
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1073
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1074 def __imul__(self, n): raise TypeError('Can\'t multiply items in place')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1075
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1076 class Values(object):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1077 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1078 Custom object for accessing the values of an OrderedDict.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1079
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1080 Can be called like the normal ``OrderedDict.values`` method, but also
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1081 supports indexing and sequence methods.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1082 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1083
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1084 def __init__(self, main):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1085 self._main = main
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1086
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1087 def __call__(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1088 """Pretend to be the values method."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1089 return self._main._values()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1090
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1091 def __getitem__(self, index):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1092 """Fetch the value at position i."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1093 if isinstance(index, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1094 return [self._main[key] for key in self._main._sequence[index]]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1095 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1096 return self._main[self._main._sequence[index]]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1097
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1098 def __setitem__(self, index, value):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1099 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1100 Set the value at position i to value.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1101
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1102 You can only do slice assignment to values if you supply a sequence of
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1103 equal length to the slice you are replacing.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1104 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1105 if isinstance(index, types.SliceType):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1106 keys = self._main._sequence[index]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1107 if len(keys) != len(value):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1108 raise ValueError('attempt to assign sequence of size %s '
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1109 'to slice of size %s' % (len(name), len(keys)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1110 # FIXME: efficiency? Would be better to calculate the indexes
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1111 # directly from the slice object
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1112 # NOTE: the new keys can collide with existing keys (or even
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1113 # contain duplicates) - these will overwrite
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1114 for key, val in zip(keys, value):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1115 self._main[key] = val
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1116 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1117 self._main[self._main._sequence[index]] = value
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1118
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1119 ### following methods pinched from UserList and adapted ###
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1120 def __repr__(self): return repr(self._main.values())
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1121
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1122 # FIXME: do we need to check if we are comparing with another ``Values``
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1123 # object? (like the __cast method of UserList)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1124 def __lt__(self, other): return self._main.values() < other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1125 def __le__(self, other): return self._main.values() <= other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1126 def __eq__(self, other): return self._main.values() == other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1127 def __ne__(self, other): return self._main.values() != other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1128 def __gt__(self, other): return self._main.values() > other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1129 def __ge__(self, other): return self._main.values() >= other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1130 def __cmp__(self, other): return cmp(self._main.values(), other)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1131
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1132 def __contains__(self, item): return item in self._main.values()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1133 def __len__(self): return len(self._main._sequence) # easier :-)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1134 def __iter__(self): return self._main.itervalues()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1135 def count(self, item): return self._main.values().count(item)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1136 def index(self, item, *args): return self._main.values().index(item, *args)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1137
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1138 def reverse(self):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1139 """Reverse the values"""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1140 vals = self._main.values()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1141 vals.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1142 # FIXME: efficiency
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1143 self[:] = vals
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1144
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1145 def sort(self, *args, **kwds):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1146 """Sort the values."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1147 vals = self._main.values()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1148 vals.sort(*args, **kwds)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1149 self[:] = vals
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1150
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1151 def __mul__(self, n): return self._main.values()*n
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1152 __rmul__ = __mul__
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1153 def __add__(self, other): return self._main.values() + other
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1154 def __radd__(self, other): return other + self._main.values()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1155
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1156 ## following methods not implemented for values ##
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1157 def __delitem__(self, i): raise TypeError('Can\'t delete items from values')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1158 def __iadd__(self, other): raise TypeError('Can\'t add in place to values')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1159 def __imul__(self, n): raise TypeError('Can\'t multiply values in place')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1160 def append(self, item): raise TypeError('Can\'t append items to values')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1161 def insert(self, i, item): raise TypeError('Can\'t insert items into values')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1162 def pop(self, i=-1): raise TypeError('Can\'t pop items from values')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1163 def remove(self, item): raise TypeError('Can\'t remove items from values')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1164 def extend(self, other): raise TypeError('Can\'t extend values')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1165
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1166 class SequenceOrderedDict(OrderedDict):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1167 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1168 Experimental version of OrderedDict that has a custom object for ``keys``,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1169 ``values``, and ``items``.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1170
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1171 These are callable sequence objects that work as methods, or can be
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1172 manipulated directly as sequences.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1173
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1174 Test for ``keys``, ``items`` and ``values``.
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1175
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1176 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1177 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1178 SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1179 >>> d.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1180 [1, 2, 3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1181 >>> d.keys()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1182 [1, 2, 3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1183 >>> d.setkeys((3, 2, 1))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1184 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1185 SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1186 >>> d.setkeys((1, 2, 3))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1187 >>> d.keys[0]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1188 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1189 >>> d.keys[:]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1190 [1, 2, 3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1191 >>> d.keys[-1]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1192 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1193 >>> d.keys[-2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1194 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1195 >>> d.keys[0:2] = [2, 1]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1196 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1197 SequenceOrderedDict([(2, 3), (1, 2), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1198 >>> d.keys.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1199 >>> d.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1200 [3, 1, 2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1201 >>> d.keys = [1, 2, 3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1202 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1203 SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1204 >>> d.keys = [3, 1, 2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1205 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1206 SequenceOrderedDict([(3, 4), (1, 2), (2, 3)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1207 >>> a = SequenceOrderedDict()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1208 >>> b = SequenceOrderedDict()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1209 >>> a.keys == b.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1210 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1211 >>> a['a'] = 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1212 >>> a.keys == b.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1213 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1214 >>> b['a'] = 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1215 >>> a.keys == b.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1216 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1217 >>> b['b'] = 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1218 >>> a.keys == b.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1219 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1220 >>> a.keys > b.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1221 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1222 >>> a.keys < b.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1223 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1224 >>> 'a' in a.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1225 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1226 >>> len(b.keys)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1227 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1228 >>> 'c' in d.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1229 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1230 >>> 1 in d.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1231 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1232 >>> [v for v in d.keys]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1233 [3, 1, 2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1234 >>> d.keys.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1235 >>> d.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1236 [1, 2, 3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1237 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)), strict=True)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1238 >>> d.keys[::-1] = [1, 2, 3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1239 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1240 SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1241 >>> d.keys[:2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1242 [3, 2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1243 >>> d.keys[:2] = [1, 3]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1244 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1245 KeyError: 'Keylist is not the same as current keylist.'
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1246
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1247 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1248 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1249 SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1250 >>> d.values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1251 [2, 3, 4]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1252 >>> d.values()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1253 [2, 3, 4]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1254 >>> d.setvalues((4, 3, 2))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1255 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1256 SequenceOrderedDict([(1, 4), (2, 3), (3, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1257 >>> d.values[::-1]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1258 [2, 3, 4]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1259 >>> d.values[0]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1260 4
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1261 >>> d.values[-2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1262 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1263 >>> del d.values[0]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1264 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1265 TypeError: Can't delete items from values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1266 >>> d.values[::2] = [2, 4]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1267 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1268 SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1269 >>> 7 in d.values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1270 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1271 >>> len(d.values)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1272 3
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1273 >>> [val for val in d.values]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1274 [2, 3, 4]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1275 >>> d.values[-1] = 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1276 >>> d.values.count(2)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1277 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1278 >>> d.values.index(2)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1279 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1280 >>> d.values[-1] = 7
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1281 >>> d.values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1282 [2, 3, 7]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1283 >>> d.values.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1284 >>> d.values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1285 [7, 3, 2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1286 >>> d.values.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1287 >>> d.values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1288 [2, 3, 7]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1289 >>> d.values.append('anything')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1290 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1291 TypeError: Can't append items to values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1292 >>> d.values = (1, 2, 3)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1293 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1294 SequenceOrderedDict([(1, 1), (2, 2), (3, 3)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1295
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1296 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1297 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1298 SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1299 >>> d.items()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1300 [(1, 2), (2, 3), (3, 4)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1301 >>> d.setitems([(3, 4), (2 ,3), (1, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1302 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1303 SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1304 >>> d.items[0]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1305 (3, 4)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1306 >>> d.items[:-1]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1307 [(3, 4), (2, 3)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1308 >>> d.items[1] = (6, 3)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1309 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1310 [(3, 4), (6, 3), (1, 2)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1311 >>> d.items[1:2] = [(9, 9)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1312 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1313 SequenceOrderedDict([(3, 4), (9, 9), (1, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1314 >>> del d.items[1:2]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1315 >>> d
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1316 SequenceOrderedDict([(3, 4), (1, 2)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1317 >>> (3, 4) in d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1318 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1319 >>> (4, 3) in d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1320 0
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1321 >>> len(d.items)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1322 2
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1323 >>> [v for v in d.items]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1324 [(3, 4), (1, 2)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1325 >>> d.items.count((3, 4))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1326 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1327 >>> d.items.index((1, 2))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1328 1
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1329 >>> d.items.index((2, 1))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1330 Traceback (most recent call last):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1331 ValueError: list.index(x): x not in list
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1332 >>> d.items.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1333 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1334 [(1, 2), (3, 4)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1335 >>> d.items.reverse()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1336 >>> d.items.sort()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1337 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1338 [(1, 2), (3, 4)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1339 >>> d.items.append((5, 6))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1340 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1341 [(1, 2), (3, 4), (5, 6)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1342 >>> d.items.insert(0, (0, 0))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1343 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1344 [(0, 0), (1, 2), (3, 4), (5, 6)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1345 >>> d.items.insert(-1, (7, 8))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1346 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1347 [(0, 0), (1, 2), (3, 4), (7, 8), (5, 6)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1348 >>> d.items.pop()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1349 (5, 6)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1350 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1351 [(0, 0), (1, 2), (3, 4), (7, 8)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1352 >>> d.items.remove((1, 2))
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1353 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1354 [(0, 0), (3, 4), (7, 8)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1355 >>> d.items.extend([(1, 2), (5, 6)])
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1356 >>> d.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1357 [(0, 0), (3, 4), (7, 8), (1, 2), (5, 6)]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1358 """
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1359
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1360 def __init__(self, init_val=(), strict=True):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1361 OrderedDict.__init__(self, init_val, strict=strict)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1362 self._keys = self.keys
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1363 self._values = self.values
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1364 self._items = self.items
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1365 self.keys = Keys(self)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1366 self.values = Values(self)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1367 self.items = Items(self)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1368 self._att_dict = {
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1369 'keys': self.setkeys,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1370 'items': self.setitems,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1371 'values': self.setvalues,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1372 }
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1373
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1374 def __setattr__(self, name, value):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1375 """Protect keys, items, and values."""
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1376 if not '_att_dict' in self.__dict__:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1377 object.__setattr__(self, name, value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1378 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1379 try:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1380 fun = self._att_dict[name]
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1381 except KeyError:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1382 OrderedDict.__setattr__(self, name, value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1383 else:
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1384 fun(value)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1385
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1386 if __name__ == '__main__':
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1387 if INTP_VER < (2, 3):
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1388 raise RuntimeError("Tests require Python v.2.3 or later")
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1389 # turn off warnings for tests
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1390 warnings.filterwarnings('ignore')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1391 # run the code tests in doctest format
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1392 import doctest
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1393 m = sys.modules.get('__main__')
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1394 globs = m.__dict__.copy()
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1395 globs.update({
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1396 'INTP_VER': INTP_VER,
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1397 })
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1398 doctest.testmod(m, globs=globs)
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1399