annotate rhodecode/lib/compat.py @ 1547:fbc762ae3496 beta

unified generation of repo groups choices added on-the-fly conversion to new repository groups format (supporting path as a link)
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 11 Oct 2011 23:39:03 +0200
parents 87ec80c280bb
children d6cb805c92fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 rhodecode.lib.compat
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 Python backward compatibility functions and common libs
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 :created_on: Oct 7, 2011
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 :author: marcink
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 :license: GPLv3, see COPYING for more details.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 # This program is free software: you can redistribute it and/or modify
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 # it under the terms of the GNU General Public License as published by
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 # the Free Software Foundation, either version 3 of the License, or
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 # (at your option) any later version.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 #
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # This program is distributed in the hope that it will be useful,
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 # GNU General Public License for more details.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 #
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 # You should have received a copy of the GNU General Public License
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 # json
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 import json
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 except ImportError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 import simplejson as json
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 # izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 from itertools import izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 except ImportError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 import itertools
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 def izip_longest(*args, **kwds): # noqa
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 fillvalue = kwds.get("fillvalue")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 def sentinel(counter=([fillvalue] * (len(args) - 1)).pop):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 yield counter() # yields the fillvalue, or raises IndexError
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 fillers = itertools.repeat(fillvalue)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 iters = [itertools.chain(it, sentinel(), fillers)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 for it in args]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 for tup in itertools.izip(*iters):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 yield tup
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 except IndexError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 pass
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 # OrderedDict
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 # Python Software Foundation License
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 # XXX: it feels like using the class with "is" and "is not" instead of "==" and
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 # "!=" should be faster.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 class _Nil(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 return "nil"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 def __eq__(self, other):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 if (isinstance(other, _Nil)):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 return True
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 return NotImplemented
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 def __ne__(self, other):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 if (isinstance(other, _Nil)):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 return False
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 return NotImplemented
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 _nil = _Nil()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 class _odict(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 """Ordered dict data structure, with O(1) complexity for dict operations
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 that modify one element.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 Overwriting values doesn't change their original sequential order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 return None
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 def __init__(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 """This doesn't accept keyword initialization as normal dicts to avoid
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 a trap - inside a function or method the keyword args are accessible
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 only as a dict, without a defined order, so their original order is
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 lost.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 raise TypeError("__init__() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 self._dict_impl().__init__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 # If you give a normal dict, then the order of elements is undefined
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 for key, val in data.iteritems():
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 # Double-linked list header
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 def _get_lh(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 if not hasattr(self, '_lh'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 dict_impl.__setattr__(self, '_lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 return dict_impl.__getattribute__(self, '_lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 def _set_lh(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 self._dict_impl().__setattr__(self, '_lh', val)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 lh = property(_get_lh, _set_lh)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 # Double-linked list tail
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 def _get_lt(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 if not hasattr(self, '_lt'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 dict_impl.__setattr__(self, '_lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 return dict_impl.__getattribute__(self, '_lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 def _set_lt(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 self._dict_impl().__setattr__(self, '_lt', val)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 lt = property(_get_lt, _set_lt)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 def __getitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 return self._dict_impl().__getitem__(self, key)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 def __setitem__(self, key, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 dict_impl.__getitem__(self, key)[1] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 except KeyError, e:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 new = [dict_impl.__getattribute__(self, 'lt'), val, _nil]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 dict_impl.__setitem__(self, key, new)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 if dict_impl.__getattribute__(self, 'lt') == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 dict_impl.__setattr__(self, 'lh', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 dict_impl.__getitem__(
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 self, dict_impl.__getattribute__(self, 'lt'))[2] = key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 dict_impl.__setattr__(self, 'lt', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 def __delitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 pred, _ , succ = self._dict_impl().__getitem__(self, key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 if pred == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 dict_impl.__setattr__(self, 'lh', succ)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 dict_impl.__getitem__(self, pred)[2] = succ
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 if succ == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 dict_impl.__setattr__(self, 'lt', pred)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 dict_impl.__getitem__(self, succ)[0] = pred
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 dict_impl.__delitem__(self, key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 def __contains__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 return key in self.keys()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 def __len__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 return len(self.keys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 def __str__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 pairs = ("%r: %r" % (k, v) for k, v in self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 return "{%s}" % ", ".join(pairs)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 pairs = ("(%r, %r)" % (k, v) for k, v in self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 return "odict([%s])" % ", ".join(pairs)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 return "odict()"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 def get(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 if k in self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 return self._dict_impl().__getitem__(self, k)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192 def __iter__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 curr_key = dict_impl.__getitem__(self, curr_key)[2]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 iterkeys = __iter__
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 def keys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202 return list(self.iterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204 def itervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 _, val, curr_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 yield val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211 def values(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212 return list(self.itervalues())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214 def iteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 _, val, next_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 curr_key = next_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222 def items(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223 return list(self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
224
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
225 def sort(self, cmp=None, key=None, reverse=False):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226 items = [(k, v) for k, v in self.items()]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227 if cmp is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 items = sorted(items, cmp=cmp)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229 elif key is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 items = sorted(items, key=key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 items = sorted(items, key=lambda x: x[1])
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 if reverse:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 items.reverse()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 self.clear()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 self.__init__(items)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 def clear(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240 dict_impl.clear(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241 dict_impl.__setattr__(self, 'lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242 dict_impl.__setattr__(self, 'lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 def copy(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 return self.__class__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 def update(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
249 raise TypeError("update() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 data = data.iteritems()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 def setdefault(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 return self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 self[k] = x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263 def pop(self, k, x=_nil):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
264 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
265 val = self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266 del self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267 return val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 if x == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270 raise
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273 def popitem(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276 key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277 return key, self.pop(key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 raise KeyError("'popitem(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 def riterkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282 """To iterate on keys in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
283 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 curr_key = dict_impl.__getitem__(self, curr_key)[0]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
289
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290 __reversed__ = riterkeys
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
292 def rkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293 """List of the keys in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
294 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
295 return list(self.riterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
296
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
297 def ritervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298 """To iterate on values in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 curr_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 yield val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 def rvalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307 """List of the values in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 return list(self.ritervalues())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
310
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
311 def riteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312 """To iterate on (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 pred_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 curr_key = pred_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 def ritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322 """List of the (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324 return list(self.riteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326 def firstkey(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328 return self._dict_impl().__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330 raise KeyError("'firstkey(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 def lastkey(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 return self._dict_impl().__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336 raise KeyError("'lastkey(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338 def as_dict(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339 return self._dict_impl()(self.items())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 def _repr(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342 """_repr(): low level repr of the whole data contained in the odict.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 Useful for debugging.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 form = "odict low level repr lh,lt,data: %r, %r, %s"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347 return form % (dict_impl.__getattribute__(self, 'lh'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348 dict_impl.__getattribute__(self, 'lt'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349 dict_impl.__repr__(self))
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351 class OrderedDict(_odict, dict):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
354 return dict
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
355
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
356
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
358 # OrderedSet
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 from sqlalchemy.util import OrderedSet