Mercurial > kallithea
annotate rhodecode/lib/compat.py @ 2095:17c9393e9645 beta
docs
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 02 Mar 2012 22:14:03 +0200 |
parents | f664d3b57fa4 |
children | 12ceeda33339 |
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 |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
28 from rhodecode import __platform__, PLATFORM_WIN |
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 #============================================================================== |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 try: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 import json |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 except ImportError: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 import simplejson as json |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 #============================================================================== |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 # izip_longest |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 #============================================================================== |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 try: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 from itertools import izip_longest |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 except ImportError: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 import itertools |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 def izip_longest(*args, **kwds): # noqa |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 fillvalue = kwds.get("fillvalue") |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 def sentinel(counter=([fillvalue] * (len(args) - 1)).pop): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 yield counter() # yields the fillvalue, or raises IndexError |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 fillers = itertools.repeat(fillvalue) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 iters = [itertools.chain(it, sentinel(), fillers) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 for it in args] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 try: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 for tup in itertools.izip(*iters): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 yield tup |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 except IndexError: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 pass |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 |
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 # OrderedDict |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 #============================================================================== |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 # Python Software Foundation License |
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 # 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
|
70 # "!=" should be faster. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 class _Nil(object): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 def __repr__(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 return "nil" |
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 def __eq__(self, other): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 if (isinstance(other, _Nil)): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 return True |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
80 return NotImplemented |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 def __ne__(self, other): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 if (isinstance(other, _Nil)): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 return False |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 return NotImplemented |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 _nil = _Nil() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 |
1890
a3efaaa6ed4f
fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
90 |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 class _odict(object): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 """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
|
93 that modify one element. |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1549
diff
changeset
|
94 |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 Overwriting values doesn't change their original sequential order. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
96 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 def _dict_impl(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 return None |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 def __init__(self, data=(), **kwds): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 """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
|
103 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
|
104 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
|
105 lost. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
106 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 if kwds: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 raise TypeError("__init__() of ordered dict takes no keyword " |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 "arguments to avoid an ordering trap.") |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 self._dict_impl().__init__(self) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 # 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
|
112 if hasattr(data, "iteritems"): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 for key, val in data.iteritems(): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 self[key] = val |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 for key, val in data: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 self[key] = val |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 # Double-linked list header |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 def _get_lh(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 if not hasattr(self, '_lh'): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 dict_impl.__setattr__(self, '_lh', _nil) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 return dict_impl.__getattribute__(self, '_lh') |
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 def _set_lh(self, val): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 self._dict_impl().__setattr__(self, '_lh', val) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 lh = property(_get_lh, _set_lh) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
131 # Double-linked list tail |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 def _get_lt(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
134 if not hasattr(self, '_lt'): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 dict_impl.__setattr__(self, '_lt', _nil) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 return dict_impl.__getattribute__(self, '_lt') |
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 def _set_lt(self, val): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 self._dict_impl().__setattr__(self, '_lt', val) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 lt = property(_get_lt, _set_lt) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
142 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
143 def __getitem__(self, key): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
144 return self._dict_impl().__getitem__(self, key)[1] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
145 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
146 def __setitem__(self, key, val): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
147 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
148 try: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 dict_impl.__getitem__(self, key)[1] = val |
1890
a3efaaa6ed4f
fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
150 except KeyError: |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
151 new = [dict_impl.__getattribute__(self, 'lt'), val, _nil] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
152 dict_impl.__setitem__(self, key, new) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
153 if dict_impl.__getattribute__(self, 'lt') == _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 dict_impl.__setattr__(self, 'lh', key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
155 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 dict_impl.__getitem__( |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
157 self, dict_impl.__getattribute__(self, 'lt'))[2] = key |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
158 dict_impl.__setattr__(self, 'lt', key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
159 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 def __delitem__(self, key): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
161 dict_impl = self._dict_impl() |
1890
a3efaaa6ed4f
fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
162 pred, _, succ = self._dict_impl().__getitem__(self, key) |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
163 if pred == _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
164 dict_impl.__setattr__(self, 'lh', succ) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
165 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
166 dict_impl.__getitem__(self, pred)[2] = succ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
167 if succ == _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
168 dict_impl.__setattr__(self, 'lt', pred) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
169 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 dict_impl.__getitem__(self, succ)[0] = pred |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 dict_impl.__delitem__(self, key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
173 def __contains__(self, key): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
174 return key in self.keys() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
176 def __len__(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
177 return len(self.keys()) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
178 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
179 def __str__(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
180 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
|
181 return "{%s}" % ", ".join(pairs) |
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 __repr__(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
184 if self: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
185 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
|
186 return "odict([%s])" % ", ".join(pairs) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
187 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
188 return "odict()" |
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 get(self, k, x=None): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
191 if k in self: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
192 return self._dict_impl().__getitem__(self, k)[1] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
193 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
194 return x |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
195 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
196 def __iter__(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
197 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
198 curr_key = dict_impl.__getattribute__(self, 'lh') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
199 while curr_key != _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
200 yield curr_key |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
201 curr_key = dict_impl.__getitem__(self, curr_key)[2] |
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 iterkeys = __iter__ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
204 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
205 def keys(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
206 return list(self.iterkeys()) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
207 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
208 def itervalues(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
209 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
210 curr_key = dict_impl.__getattribute__(self, 'lh') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
211 while curr_key != _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
212 _, val, curr_key = dict_impl.__getitem__(self, curr_key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
213 yield val |
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 values(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
216 return list(self.itervalues()) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
217 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
218 def iteritems(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
219 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
220 curr_key = dict_impl.__getattribute__(self, 'lh') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
221 while curr_key != _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
222 _, val, next_key = dict_impl.__getitem__(self, curr_key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
223 yield curr_key, val |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
224 curr_key = next_key |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
225 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
226 def items(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
227 return list(self.iteritems()) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
228 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
229 def sort(self, cmp=None, key=None, reverse=False): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
230 items = [(k, v) for k, v in self.items()] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
231 if cmp is not None: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
232 items = sorted(items, cmp=cmp) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
233 elif key is not None: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
234 items = sorted(items, key=key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
235 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
236 items = sorted(items, key=lambda x: x[1]) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
237 if reverse: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
238 items.reverse() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
239 self.clear() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
240 self.__init__(items) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
241 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
242 def clear(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
243 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
244 dict_impl.clear(self) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
245 dict_impl.__setattr__(self, 'lh', _nil) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
246 dict_impl.__setattr__(self, 'lt', _nil) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
247 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
248 def copy(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
249 return self.__class__(self) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
250 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
251 def update(self, data=(), **kwds): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
252 if kwds: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
253 raise TypeError("update() of ordered dict takes no keyword " |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
254 "arguments to avoid an ordering trap.") |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
255 if hasattr(data, "iteritems"): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
256 data = data.iteritems() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
257 for key, val in data: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
258 self[key] = val |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
259 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
260 def setdefault(self, k, x=None): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
261 try: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
262 return self[k] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
263 except KeyError: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
264 self[k] = x |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
265 return x |
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 pop(self, k, x=_nil): |
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 val = self[k] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
270 del self[k] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
271 return val |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
272 except KeyError: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
273 if x == _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
274 raise |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
275 return x |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
276 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
277 def popitem(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
278 try: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
279 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
280 key = dict_impl.__getattribute__(self, 'lt') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
281 return key, self.pop(key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
282 except KeyError: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
283 raise KeyError("'popitem(): ordered dictionary is empty'") |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
284 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
285 def riterkeys(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
286 """To iterate on keys in reversed order. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
287 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
288 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
289 curr_key = dict_impl.__getattribute__(self, 'lt') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
290 while curr_key != _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
291 yield curr_key |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
292 curr_key = dict_impl.__getitem__(self, curr_key)[0] |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
293 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
294 __reversed__ = riterkeys |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
295 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
296 def rkeys(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
297 """List of the keys in reversed order. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
298 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
299 return list(self.riterkeys()) |
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 def ritervalues(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
302 """To iterate on values in reversed order. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
303 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
304 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
305 curr_key = dict_impl.__getattribute__(self, 'lt') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
306 while curr_key != _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
307 curr_key, val, _ = dict_impl.__getitem__(self, curr_key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
308 yield val |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
309 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
310 def rvalues(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
311 """List of the values in reversed order. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
312 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
313 return list(self.ritervalues()) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
314 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
315 def riteritems(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
316 """To iterate on (key, value) in reversed order. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
317 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
318 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
319 curr_key = dict_impl.__getattribute__(self, 'lt') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
320 while curr_key != _nil: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
321 pred_key, val, _ = dict_impl.__getitem__(self, curr_key) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
322 yield curr_key, val |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
323 curr_key = pred_key |
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 def ritems(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
326 """List of the (key, value) in reversed order. |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
327 """ |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
328 return list(self.riteritems()) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
329 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
330 def firstkey(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
331 if self: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
332 return self._dict_impl().__getattribute__(self, 'lh') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
333 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
334 raise KeyError("'firstkey(): ordered dictionary is empty'") |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
335 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
336 def lastkey(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
337 if self: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
338 return self._dict_impl().__getattribute__(self, 'lt') |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
339 else: |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
340 raise KeyError("'lastkey(): ordered dictionary is empty'") |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
341 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
342 def as_dict(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
343 return self._dict_impl()(self.items()) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
344 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
345 def _repr(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
346 """_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
|
347 Useful for debugging. |
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 dict_impl = self._dict_impl() |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
350 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
|
351 return form % (dict_impl.__getattribute__(self, 'lh'), |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
352 dict_impl.__getattribute__(self, 'lt'), |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
353 dict_impl.__repr__(self)) |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
354 |
1890
a3efaaa6ed4f
fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
355 |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
356 class OrderedDict(_odict, dict): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
357 |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
358 def _dict_impl(self): |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
359 return dict |
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
360 |
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 # OrderedSet |
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 from sqlalchemy.util import OrderedSet |
1549
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
366 |
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 # kill FUNCTIONS |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
370 #============================================================================== |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
371 if __platform__ in PLATFORM_WIN: |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
372 import ctypes |
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 def kill(pid, sig): |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
375 """kill function for Win32""" |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
376 kernel32 = ctypes.windll.kernel32 |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
377 handle = kernel32.OpenProcess(1, 0, pid) |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
378 return (0 != kernel32.TerminateProcess(handle, 0)) |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
379 |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
380 else: |
d6cb805c92fd
moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
381 kill = os.kill |
2068 | 382 |
383 | |
384 #============================================================================== | |
385 # itertools.product | |
386 #============================================================================== | |
387 | |
388 try: | |
389 from itertools import product | |
390 except ImportError: | |
391 def product(*args, **kwds): | |
392 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy | |
393 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 | |
394 pools = map(tuple, args) * kwds.get('repeat', 1) | |
395 result = [[]] | |
396 for pool in pools: | |
397 result = [x + [y] for x in result for y in pool] | |
398 for prod in result: | |
399 yield tuple(prod) |