root / logilab.pylintinstaller / logilab / common / compat.py

Revision 202:d67e86292521, 6.8 kB (checked in by tziade@…, 9 months ago)

added logilab.pylintinstaller

Line 
1# pylint: disable-msg=E0601,W0622,W0611
2#
3# Copyright (c) 2004-2006 LOGILAB S.A. (Paris, FRANCE).
4# http://www.logilab.fr/ -- mailto:contact@logilab.fr
5#
6# This program is free software; you can redistribute it and/or modify it under
7# the terms of the GNU General Public License as published by the Free Software
8# Foundation; either version 2 of the License, or (at your option) any later
9# version.
10#
11# This program is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
14#
15# You should have received a copy of the GNU General Public License along with
16# this program; if not, write to the Free Software Foundation, Inc.,
17# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18"""some wrapper around tools introduced into python 2.3, making them available
19in python 2.2
20"""
21from __future__ import generators
22
23from warnings import warn
24
25from logilab.common.deprecation import class_renamed
26
27try:
28    set = set
29    frozenset = frozenset
30except NameError:
31    try:
32        from sets import Set as set, ImmutableSet as frozenset
33    except ImportError:
34        class _baseset(object):
35            def __init__(self, values=()):
36                self._data = {}
37                warn("This implementation of Set is not complete !",
38                     stacklevel=2)
39                for v in values:
40                    self._data[v] = 1
41
42            def __or__(self, other):
43                result = self.__class__(self._data.keys())
44                for val in other:
45                    result.add(val)
46                return result
47            __add__ = __or__
48           
49            def __and__(self, other):
50                result = self.__class__()
51                for val in other:
52                    if val in self._data:
53                        result.add(val)
54                return result
55           
56            def __sub__(self, other):
57                result = self.__class__(self._data.keys())
58                for val in other:
59                    if val in self._data:
60                        result.remove(val)
61                return result
62           
63            def __cmp__(self, other):
64                keys = self._data.keys()
65                okeys = other._data.keys()
66                keys.sort()
67                okeys.sort()
68                return cmp(keys, okeys)
69           
70            def __len__(self):
71                return len(self._data)
72
73            def __repr__(self):
74                elements = self._data.keys()
75                return 'lcc.%s(%r)' % (self.__class__.__name__, elements)
76            __str__ = __repr__
77
78            def __iter__(self):
79                return iter(self._data)
80
81        class frozenset(_baseset):
82            """immutable set (can be set in dictionnaries)"""
83            def __init__(self, values=()):
84                super(frozenset, self).__init__(values)
85                self._hashcode = None
86               
87            def _compute_hash(self):
88                """taken from python stdlib (sets.py)"""
89                # Calculate hash code for a set by xor'ing the hash codes of
90                # the elements.  This ensures that the hash code does not depend
91                # on the order in which elements are added to the set.  This is
92                # not called __hash__ because a BaseSet should not be hashable;
93                # only an ImmutableSet is hashable.
94                result = 0
95                for elt in self:
96                    result ^= hash(elt)
97                return result
98           
99            def __hash__(self):
100                """taken from python stdlib (sets.py)"""
101                if self._hashcode is None:
102                    self._hashcode = self._compute_hash()
103                return self._hashcode
104
105           
106        class set(_baseset):
107            """mutable set"""
108            def add(self, value):
109                self._data[value] = 1
110
111            def remove(self, element):
112                """removes <element> from set"""
113                del self._data[element]
114
115            def pop(self):
116                """pops an arbitrary element from set"""
117                return self._data.popitem()[0]
118
119            def __hash__(self):
120                """mutable et cannot be hashed."""
121                raise TypeError("set objects are not hashable")
122
123        del _baseset # don't explicity provide this class
124
125Set = class_renamed('Set', set, 'logilab.common.compat.Set is deprecated, '
126                    'use logilab.common.compat.set instead')
127
128try:
129    from itertools import izip, chain, imap
130except ImportError:
131    # from itertools documentation ###
132    def izip(*iterables):
133        iterables = map(iter, iterables)
134        while iterables:
135            result = [i.next() for i in iterables]
136            yield tuple(result)
137
138    def chain(*iterables):
139        for it in iterables:
140            for element in it:
141                yield element
142               
143    def imap(function, *iterables):
144        iterables = map(iter, iterables)
145        while True:
146            args = [i.next() for i in iterables]
147            if function is None:
148                yield tuple(args)
149            else:
150                yield function(*args)               
151try:
152    sum = sum
153    enumerate = enumerate
154except NameError:
155    # define the sum and enumerate functions (builtins introduced in py 2.3)
156    import operator
157    def sum(seq, start=0):
158        """Returns the sum of all elements in the sequence"""
159        return reduce(operator.add, seq, start)
160
161    def enumerate(iterable):
162        """emulates the python2.3 enumerate() function"""
163        i = 0
164        for val in iterable:
165            yield i, val
166            i += 1
167        #return zip(range(len(iterable)), iterable)
168try:
169    sorted = sorted
170    reversed = reversed
171except NameError:
172   
173    def sorted(iterable, cmp=None, key=None, reverse=False):
174        original = list(iterable)
175        if key:
176            l2 = [(key(elt), index) for index, elt in enumerate(original)]
177        else:
178            l2 = original
179        l2.sort(cmp)
180        if reverse:
181            l2.reverse()
182        if key:
183            return [original[index] for elt, index in l2]
184        return l2
185   
186    def reversed(l):
187        l2 = list(l)
188        l2.reverse()
189        return l2
190
191# Python2.5 builtins
192try:
193    any = any
194    all = all
195except NameError:
196    def any(iterable):
197        """any(iterable) -> bool
198
199        Return True if bool(x) is True for any x in the iterable.
200        """
201        for elt in iterable:
202            if elt:
203                return True
204        return False
205   
206    def all(iterable):
207        """all(iterable) -> bool
208
209        Return True if bool(x) is True for all values x in the iterable.
210        """
211        for elt in iterable:
212            if not elt:
213                return False
214        return True
Note: See TracBrowser for help on using the browser.