annotate rhodecode/lib/compat.py @ 4116:ffd45b185016 rhodecode-2.2.5-gpl

Imported some of the GPLv3'd changes from RhodeCode v2.2.5. This imports changes between changesets 21af6c4eab3d and 6177597791c2 in RhodeCode's original repository, including only changes to Python files and HTML. RhodeCode clearly licensed its changes to these files under GPLv3 in their /LICENSE file, which states the following: The Python code and integrated HTML are licensed under the GPLv3 license. (See: https://code.rhodecode.com/rhodecode/files/v2.2.5/LICENSE or http://web.archive.org/web/20140512193334/https://code.rhodecode.com/rhodecode/files/f3b123159901f15426d18e3dc395e8369f70ebe0/LICENSE for an online copy of that LICENSE file) Conservancy reviewed these changes and confirmed that they can be licensed as a whole to the Kallithea project under GPLv3-only. While some of the contents committed herein are clearly licensed GPLv3-or-later, on the whole we must assume the are GPLv3-only, since the statement above from RhodeCode indicates that they intend GPLv3-only as their license, per GPLv3ยง14 and other relevant sections of GPLv3.
author Bradley M. Kuhn <bkuhn@sfconservancy.org>
date Wed, 02 Jul 2014 19:03:13 -0400
parents 2b9da8749065
children 7e5f8c12a3fc
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 # 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
3 # 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
4 # 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
5 # (at your option) any later version.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 #
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 # 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
8 # 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
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 # GNU General Public License for more details.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 #
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 # 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
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
14 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
15 rhodecode.lib.compat
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
16 ~~~~~~~~~~~~~~~~~~~~
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
17
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
18 Python backward compatibility functions and common libs
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
19
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
20
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
21 :created_on: Oct 7, 2011
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
22 :author: marcink
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
23 :copyright: (c) 2013 RhodeCode GmbH.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
24 :license: GPLv3, see LICENSE for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
25 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
26
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
28 import os
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
29 import functools
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
30 import importlib
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2826
diff changeset
31 from rhodecode import __py_version__, is_windows
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
32
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 # json
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 #==============================================================================
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents: 2160
diff changeset
36 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
37
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
38 # alias for formatted json
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
39 formatted_json = functools.partial(json.dumps, indent=4, sort_keys=True)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
40
3872
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
41 if __py_version__ >= (2, 7):
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
42 import unittest
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
43 else:
2b9da8749065 Use unittest2 for testing
Marcin Kuzminski <marcin@python-works.com>
parents: 3797
diff changeset
44 import unittest2 as unittest
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45
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 # izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 from itertools import izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 except ImportError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 import itertools
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 2068
diff changeset
54 def izip_longest(*args, **kwds):
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 fillvalue = kwds.get("fillvalue")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 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
58 yield counter() # yields the fillvalue, or raises IndexError
1514
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 fillers = itertools.repeat(fillvalue)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 iters = [itertools.chain(it, sentinel(), fillers)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 for it in args]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 for tup in itertools.izip(*iters):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 yield tup
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 except IndexError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 pass
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68
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 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 # OrderedDict
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
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 # Python Software Foundation License
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 # 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
77 # "!=" should be faster.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 class _Nil(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 return "nil"
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 __eq__(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 True
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 def __ne__(self, other):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 if (isinstance(other, _Nil)):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 return False
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 return NotImplemented
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 _nil = _Nil()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
97
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 class _odict(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 """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
100 that modify one element.
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1549
diff changeset
101
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 Overwriting values doesn't change their original sequential order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 return None
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 def __init__(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 """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
110 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
111 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
112 lost.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 raise TypeError("__init__() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 self._dict_impl().__init__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 # 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
119 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 for key, val in data.iteritems():
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 # Double-linked list header
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 def _get_lh(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 if not hasattr(self, '_lh'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 dict_impl.__setattr__(self, '_lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 return dict_impl.__getattribute__(self, '_lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 def _set_lh(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 self._dict_impl().__setattr__(self, '_lh', val)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 lh = property(_get_lh, _set_lh)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 # Double-linked list tail
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 def _get_lt(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 if not hasattr(self, '_lt'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 dict_impl.__setattr__(self, '_lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 return dict_impl.__getattribute__(self, '_lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 def _set_lt(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 self._dict_impl().__setattr__(self, '_lt', val)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 lt = property(_get_lt, _set_lt)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 def __getitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 return self._dict_impl().__getitem__(self, key)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 def __setitem__(self, key, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 dict_impl.__getitem__(self, key)[1] = val
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
157 except KeyError:
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 new = [dict_impl.__getattribute__(self, 'lt'), val, _nil]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 dict_impl.__setitem__(self, key, new)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 if dict_impl.__getattribute__(self, 'lt') == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 dict_impl.__setattr__(self, 'lh', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 dict_impl.__getitem__(
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 self, dict_impl.__getattribute__(self, 'lt'))[2] = key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 dict_impl.__setattr__(self, 'lt', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 def __delitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 dict_impl = self._dict_impl()
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
169 pred, _, succ = self._dict_impl().__getitem__(self, key)
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 if pred == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 dict_impl.__setattr__(self, 'lh', succ)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 dict_impl.__getitem__(self, pred)[2] = succ
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 if succ == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 dict_impl.__setattr__(self, 'lt', pred)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 dict_impl.__getitem__(self, succ)[0] = pred
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 dict_impl.__delitem__(self, key)
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 __contains__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 return key in self.keys()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 def __len__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 return len(self.keys())
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 __str__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 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
188 return "{%s}" % ", ".join(pairs)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192 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
193 return "odict([%s])" % ", ".join(pairs)
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 "odict()"
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 get(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198 if k in self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 return self._dict_impl().__getitem__(self, k)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 def __iter__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 curr_key = dict_impl.__getitem__(self, curr_key)[2]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 iterkeys = __iter__
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212 def keys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213 return list(self.iterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215 def itervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 _, val, curr_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 yield val
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 values(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223 return list(self.itervalues())
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 iteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229 _, val, next_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 curr_key = next_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 def items(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 return list(self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 def sort(self, cmp=None, key=None, reverse=False):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237 items = [(k, v) for k, v in self.items()]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 if cmp is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 items = sorted(items, cmp=cmp)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240 elif key is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241 items = sorted(items, key=key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243 items = sorted(items, key=lambda x: x[1])
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 if reverse:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 items.reverse()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246 self.clear()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 self.__init__(items)
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 clear(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 dict_impl.clear(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 dict_impl.__setattr__(self, 'lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 dict_impl.__setattr__(self, 'lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255 def copy(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 return self.__class__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 def update(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 raise TypeError("update() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263 data = data.iteritems()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
264 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
265 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267 def setdefault(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 return self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271 self[k] = x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274 def pop(self, k, x=_nil):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276 val = self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277 del self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278 return val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 if x == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 raise
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282 return x
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 def popitem(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 return key, self.pop(key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
289 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290 raise KeyError("'popitem(): ordered dictionary is empty'")
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 riterkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293 """To iterate on 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 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
296 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
297 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299 curr_key = dict_impl.__getitem__(self, curr_key)[0]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 __reversed__ = riterkeys
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 def rkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 """List of the keys in reversed order.
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 return list(self.riterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 def ritervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 """To iterate on values in reversed order.
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 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 curr_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315 yield val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 def rvalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318 """List of the values in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 return list(self.ritervalues())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322 def riteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 """To iterate on (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328 pred_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330 curr_key = pred_key
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 ritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333 """List of the (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 return list(self.riteritems())
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 firstkey(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, 'lh')
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("'firstkey(): 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 lastkey(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 return self._dict_impl().__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347 raise KeyError("'lastkey(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349 def as_dict(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 return self._dict_impl()(self.items())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352 def _repr(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 """_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
354 Useful for debugging.
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 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357 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
358 return form % (dict_impl.__getattribute__(self, 'lh'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359 dict_impl.__getattribute__(self, 'lt'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 dict_impl.__repr__(self))
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
361
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
362
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
363 class OrderedDict(_odict, dict):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
364
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
365 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
366 return dict
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
367
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
368
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
369 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
370 # OrderedSet
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
371 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
372 from sqlalchemy.util import OrderedSet
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
373
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 #==============================================================================
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
376 # Hybrid property/method
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
377 #==============================================================================
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
378 from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
379
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
380
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3872
diff changeset
381 #==============================================================================
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
382 # kill FUNCTIONS
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
383 #==============================================================================
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2826
diff changeset
384 if is_windows:
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
385 import ctypes
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
386
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
387 def kill(pid, sig):
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
388 """kill function for Win32"""
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
389 kernel32 = ctypes.windll.kernel32
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
390 handle = kernel32.OpenProcess(1, 0, pid)
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
391 return (0 != kernel32.TerminateProcess(handle, 0))
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
392
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
393 else:
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
394 kill = os.kill
2068
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
395
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
396
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
397 #==============================================================================
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
398 # itertools.product
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
399 #==============================================================================
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
400
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
401 try:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
402 from itertools import product
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
403 except ImportError:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
404 def product(*args, **kwds):
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
405 # 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
406 # 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
407 pools = map(tuple, args) * kwds.get('repeat', 1)
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
408 result = [[]]
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
409 for pool in pools:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
410 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
411 for prod in result:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
412 yield tuple(prod)
2552
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
413
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
414
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
415 #==============================================================================
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
416 # BytesIO
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
417 #==============================================================================
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
418
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
419 try:
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
420 from io import BytesIO
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
421 except ImportError:
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
422 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
423
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
424
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
425 #==============================================================================
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
426 # bytes
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
427 #==============================================================================
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
428 if __py_version__ >= (2, 6):
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
429 _bytes = bytes
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
430 else:
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
431 # 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
432 _bytes = str
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
433
2826
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
434 if __py_version__ >= (2, 6):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
435 _bytearray = bytearray
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
436 else:
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2826
diff changeset
437 import array
2826
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
438 # 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
439 # i think we never use bytearray anyway
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
440 _bytearray = array
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
441
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
442
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
443 #==============================================================================
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
444 # deque
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
445 #==============================================================================
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
446
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
447 if __py_version__ >= (2, 6):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
448 from collections import deque
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
449 else:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
450 #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
451 class deque(object):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
452
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
453 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
454 if not hasattr(self, 'data'):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
455 self.left = self.right = 0
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
456 self.data = {}
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
457 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
458 self.extend(iterable)
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 append(self, x):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
461 self.data[self.right] = x
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
462 self.right += 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
463 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
464 self.popleft()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
465
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
466 def appendleft(self, x):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
467 self.left -= 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
468 self.data[self.left] = x
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
469 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
470 self.pop()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
471
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
472 def pop(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
473 if self.left == self.right:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
474 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
475 self.right -= 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
476 elem = self.data[self.right]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
477 del self.data[self.right]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
478 return elem
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 popleft(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
481 if self.left == self.right:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
482 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
483 elem = self.data[self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
484 del self.data[self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
485 self.left += 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
486 return 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 clear(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
489 self.data.clear()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
490 self.left = self.right = 0
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
491
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
492 def extend(self, iterable):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
493 for elem in iterable:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
494 self.append(elem)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
495
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
496 def extendleft(self, iterable):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
497 for elem in iterable:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
498 self.appendleft(elem)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
499
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
500 def rotate(self, n=1):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
501 if self:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
502 n %= len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
503 for i in xrange(n):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
504 self.appendleft(self.pop())
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
505
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
506 def __getitem__(self, i):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
507 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
508 i += len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
509 try:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
510 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
511 except KeyError:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
512 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
513
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
514 def __setitem__(self, i, value):
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 += len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
517 try:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
518 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
519 except KeyError:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
520 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
521
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
522 def __delitem__(self, i):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
523 size = len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
524 if not (-size <= i < size):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
525 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
526 data = self.data
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
527 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
528 i += size
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
529 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
530 data[j] = data[j + 1]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
531 self.pop()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
532
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
533 def __len__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
534 return self.right - self.left
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
535
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
536 def __cmp__(self, other):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
537 if type(self) != type(other):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
538 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
539 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
540
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
541 def __repr__(self, _track=[]):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
542 if id(self) in _track:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
543 return '...'
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
544 _track.append(id(self))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
545 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
546 _track.remove(id(self))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
547 return r
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 __getstate__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
550 return (tuple(self),)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
551
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
552 def __setstate__(self, s):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
553 self.__init__(s[0])
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
554
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
555 def __hash__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
556 raise TypeError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
557
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
558 def __copy__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
559 return self.__class__(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
560
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
561 def __deepcopy__(self, memo={}):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
562 from copy import deepcopy
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
563 result = self.__class__()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
564 memo[id(self)] = result
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
565 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
566 return result