annotate rhodecode/lib/compat.py @ 4099:43b54436d459

update issue tracker and official site
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 01 Sep 2013 19:56:08 +0200
parents 2b9da8749065
children ffd45b185016
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
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1549
diff changeset
7
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1549
diff changeset
8
1514
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
1824
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1818
diff changeset
11 :copyright: (C) 2010-2010 Marcin Kuzminski <marcin@python-works.com>
1514
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
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
27 import os
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2826
diff changeset
28 from rhodecode import __py_version__, is_windows
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
29
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 # json
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 #==============================================================================
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents: 2160
diff changeset
33 from rhodecode.lib.ext_json import json
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 2068
diff changeset
34
3872
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
35 if __py_version__ >= (2, 7):
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
36 import unittest
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
37 else:
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
38 import unittest2 as unittest
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 # izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 from itertools import izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 except ImportError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 import itertools
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 2068
diff changeset
48 def izip_longest(*args, **kwds):
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 fillvalue = kwds.get("fillvalue")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 def sentinel(counter=([fillvalue] * (len(args) - 1)).pop):
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 2068
diff changeset
52 yield counter() # yields the fillvalue, or raises IndexError
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 fillers = itertools.repeat(fillvalue)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 iters = [itertools.chain(it, sentinel(), fillers)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 for it in args]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 for tup in itertools.izip(*iters):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 yield tup
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 except IndexError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 pass
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 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 # OrderedDict
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 # Python Software Foundation License
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 # 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
71 # "!=" should be faster.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 class _Nil(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 return "nil"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 def __eq__(self, other):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 if (isinstance(other, _Nil)):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 return True
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 return NotImplemented
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 def __ne__(self, other):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 if (isinstance(other, _Nil)):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 return False
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 return NotImplemented
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 _nil = _Nil()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
91
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 class _odict(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 """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
94 that modify one element.
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1549
diff changeset
95
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 Overwriting values doesn't change their original sequential order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 return None
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 def __init__(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 """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
104 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
105 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
106 lost.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 raise TypeError("__init__() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 self._dict_impl().__init__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 # 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
113 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 for key, val in data.iteritems():
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 # Double-linked list header
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 def _get_lh(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 if not hasattr(self, '_lh'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 dict_impl.__setattr__(self, '_lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 return dict_impl.__getattribute__(self, '_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 def _set_lh(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 self._dict_impl().__setattr__(self, '_lh', val)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 lh = property(_get_lh, _set_lh)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 # Double-linked list tail
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 def _get_lt(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 if not hasattr(self, '_lt'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 dict_impl.__setattr__(self, '_lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 return dict_impl.__getattribute__(self, '_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 _set_lt(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 self._dict_impl().__setattr__(self, '_lt', val)
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 lt = property(_get_lt, _set_lt)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 def __getitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 return self._dict_impl().__getitem__(self, key)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 def __setitem__(self, key, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 dict_impl.__getitem__(self, key)[1] = val
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
151 except KeyError:
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 new = [dict_impl.__getattribute__(self, 'lt'), val, _nil]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 dict_impl.__setitem__(self, key, new)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 if dict_impl.__getattribute__(self, 'lt') == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 dict_impl.__setattr__(self, 'lh', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 dict_impl.__getitem__(
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 self, dict_impl.__getattribute__(self, 'lt'))[2] = key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 dict_impl.__setattr__(self, 'lt', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 def __delitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 dict_impl = self._dict_impl()
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
163 pred, _, succ = self._dict_impl().__getitem__(self, key)
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 if pred == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 dict_impl.__setattr__(self, 'lh', succ)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 dict_impl.__getitem__(self, pred)[2] = succ
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 if succ == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 dict_impl.__setattr__(self, 'lt', pred)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 dict_impl.__getitem__(self, succ)[0] = pred
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 dict_impl.__delitem__(self, key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 def __contains__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 return key in self.keys()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 def __len__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 return len(self.keys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 def __str__(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 "{%s}" % ", ".join(pairs)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 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
187 return "odict([%s])" % ", ".join(pairs)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 return "odict()"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 def get(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192 if k in self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193 return self._dict_impl().__getitem__(self, k)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 def __iter__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202 curr_key = dict_impl.__getitem__(self, curr_key)[2]
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 iterkeys = __iter__
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 def keys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 return list(self.iterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 def itervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213 _, val, curr_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214 yield val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 def values(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217 return list(self.itervalues())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 def iteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223 _, val, next_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
224 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
225 curr_key = next_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227 def items(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 return list(self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 def sort(self, cmp=None, key=None, reverse=False):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 items = [(k, v) for k, v in self.items()]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 if cmp is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 items = sorted(items, cmp=cmp)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 elif key is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 items = sorted(items, key=key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237 items = sorted(items, key=lambda x: x[1])
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 if reverse:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 items.reverse()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240 self.clear()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241 self.__init__(items)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243 def clear(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 dict_impl.clear(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246 dict_impl.__setattr__(self, 'lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 dict_impl.__setattr__(self, 'lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
249 def copy(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 return self.__class__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 def update(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254 raise TypeError("update() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257 data = data.iteritems()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261 def setdefault(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263 return self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
264 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
265 self[k] = x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268 def pop(self, k, x=_nil):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270 val = self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271 del self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272 return val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274 if x == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 raise
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278 def popitem(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282 return key, self.pop(key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
283 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284 raise KeyError("'popitem(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286 def riterkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 """To iterate on keys in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
289 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
292 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293 curr_key = dict_impl.__getitem__(self, curr_key)[0]
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 __reversed__ = 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 rkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298 """List of the keys 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 return list(self.riterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 def ritervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 """To iterate on values in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 curr_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 yield val
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 rvalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312 """List of the values 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 return list(self.ritervalues())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316 def riteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 """To iterate on (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322 pred_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324 curr_key = pred_key
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 ritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 """List of the (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 return list(self.riteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331 def firstkey(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333 return self._dict_impl().__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 raise KeyError("'firstkey(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337 def lastkey(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339 return self._dict_impl().__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 raise KeyError("'lastkey(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 def as_dict(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344 return self._dict_impl()(self.items())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 def _repr(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347 """_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
348 Useful for debugging.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351 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
352 return form % (dict_impl.__getattribute__(self, 'lh'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 dict_impl.__getattribute__(self, 'lt'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
354 dict_impl.__repr__(self))
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
355
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
356
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357 class OrderedDict(_odict, dict):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
358
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 return dict
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
361
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
362
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
363 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
364 # OrderedSet
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
365 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
366 from sqlalchemy.util import OrderedSet
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
367
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
368
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
369 #==============================================================================
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
370 # kill FUNCTIONS
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
371 #==============================================================================
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2826
diff changeset
372 if is_windows:
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
373 import ctypes
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
374
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
375 def kill(pid, sig):
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
376 """kill function for Win32"""
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
377 kernel32 = ctypes.windll.kernel32
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
378 handle = kernel32.OpenProcess(1, 0, pid)
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
379 return (0 != kernel32.TerminateProcess(handle, 0))
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
380
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
381 else:
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
382 kill = os.kill
2068
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
383
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
384
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
385 #==============================================================================
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
386 # itertools.product
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
387 #==============================================================================
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
388
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
389 try:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
390 from itertools import product
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
391 except ImportError:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
392 def product(*args, **kwds):
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
393 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
394 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
395 pools = map(tuple, args) * kwds.get('repeat', 1)
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
396 result = [[]]
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
397 for pool in pools:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
398 result = [x + [y] for x in result for y in pool]
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
399 for prod in result:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
400 yield tuple(prod)
2552
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
401
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
402
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
403 #==============================================================================
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
404 # BytesIO
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
405 #==============================================================================
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
406
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
407 try:
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
408 from io import BytesIO
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
409 except ImportError:
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
410 from cStringIO import StringIO as BytesIO
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
411
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
412
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
413 #==============================================================================
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
414 # bytes
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
415 #==============================================================================
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
416 if __py_version__ >= (2, 6):
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
417 _bytes = bytes
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
418 else:
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
419 # in py2.6 bytes is a synonim for str
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
420 _bytes = str
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
421
2826
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
422 if __py_version__ >= (2, 6):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
423 _bytearray = bytearray
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
424 else:
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2826
diff changeset
425 import array
2826
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
426 # no idea if this is correct but all integration tests are passing
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
427 # i think we never use bytearray anyway
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
428 _bytearray = array
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
429
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
430
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
431 #==============================================================================
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
432 # deque
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
433 #==============================================================================
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
434
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
435 if __py_version__ >= (2, 6):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
436 from collections import deque
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
437 else:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
438 #need to implement our own deque with maxlen
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
439 class deque(object):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
440
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
441 def __init__(self, iterable=(), maxlen= -1):
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
442 if not hasattr(self, 'data'):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
443 self.left = self.right = 0
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
444 self.data = {}
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
445 self.maxlen = maxlen or -1
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
446 self.extend(iterable)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
447
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
448 def append(self, x):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
449 self.data[self.right] = x
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
450 self.right += 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
451 if self.maxlen != -1 and len(self) > self.maxlen:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
452 self.popleft()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
453
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
454 def appendleft(self, x):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
455 self.left -= 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
456 self.data[self.left] = x
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
457 if self.maxlen != -1 and len(self) > self.maxlen:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
458 self.pop()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
459
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
460 def pop(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
461 if self.left == self.right:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
462 raise IndexError('cannot pop from empty deque')
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
463 self.right -= 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
464 elem = self.data[self.right]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
465 del self.data[self.right]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
466 return elem
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
467
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
468 def popleft(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
469 if self.left == self.right:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
470 raise IndexError('cannot pop from empty deque')
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
471 elem = self.data[self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
472 del self.data[self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
473 self.left += 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
474 return elem
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
475
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
476 def clear(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
477 self.data.clear()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
478 self.left = self.right = 0
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
479
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
480 def extend(self, iterable):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
481 for elem in iterable:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
482 self.append(elem)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
483
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
484 def extendleft(self, iterable):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
485 for elem in iterable:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
486 self.appendleft(elem)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
487
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
488 def rotate(self, n=1):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
489 if self:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
490 n %= len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
491 for i in xrange(n):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
492 self.appendleft(self.pop())
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
493
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
494 def __getitem__(self, i):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
495 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
496 i += len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
497 try:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
498 return self.data[i + self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
499 except KeyError:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
500 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
501
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
502 def __setitem__(self, i, value):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
503 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
504 i += len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
505 try:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
506 self.data[i + self.left] = value
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
507 except KeyError:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
508 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
509
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
510 def __delitem__(self, i):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
511 size = len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
512 if not (-size <= i < size):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
513 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
514 data = self.data
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
515 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
516 i += size
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
517 for j in xrange(self.left + i, self.right - 1):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
518 data[j] = data[j + 1]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
519 self.pop()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
520
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
521 def __len__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
522 return self.right - self.left
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
523
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
524 def __cmp__(self, other):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
525 if type(self) != type(other):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
526 return cmp(type(self), type(other))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
527 return cmp(list(self), list(other))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
528
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
529 def __repr__(self, _track=[]):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
530 if id(self) in _track:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
531 return '...'
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
532 _track.append(id(self))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
533 r = 'deque(%r, maxlen=%s)' % (list(self), self.maxlen)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
534 _track.remove(id(self))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
535 return r
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
536
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
537 def __getstate__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
538 return (tuple(self),)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
539
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
540 def __setstate__(self, s):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
541 self.__init__(s[0])
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
542
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
543 def __hash__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
544 raise TypeError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
545
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
546 def __copy__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
547 return self.__class__(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
548
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
549 def __deepcopy__(self, memo={}):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
550 from copy import deepcopy
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
551 result = self.__class__()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
552 memo[id(self)] = result
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
553 result.__init__(deepcopy(tuple(self), memo))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
554 return result