summaryrefslogtreecommitdiff
path: root/compat.py
diff options
context:
space:
mode:
authorEmile Anclin <emile.anclin@logilab.fr>2010-10-06 12:39:20 +0200
committerEmile Anclin <emile.anclin@logilab.fr>2010-10-06 12:39:20 +0200
commit885ead94dc7fb402137f86b2ed0200db94ae0215 (patch)
tree6ba9c37fd438eb824dc5a91527537d193b8c3841 /compat.py
parent3a830854f0b538e918524d3b28c9f881b2422793 (diff)
downloadlogilab-common-885ead94dc7fb402137f86b2ed0200db94ae0215.tar.gz
remove py2.2 compat ; use deprecation warnings
Diffstat (limited to 'compat.py')
-rw-r--r--compat.py157
1 files changed, 18 insertions, 139 deletions
diff --git a/compat.py b/compat.py
index 480fb78..eced4ef 100644
--- a/compat.py
+++ b/compat.py
@@ -50,6 +50,7 @@ else:
def str_encode(string, encoding):
return str(string)
+# XXX shouldn't we remove this and just let 2to3 do his job ?
try:
callable = callable
except NameError:# callable removed from py3k
@@ -77,154 +78,31 @@ try:
except ImportError:
import pickle
-# Python 2.3 doesn't have `set`
try:
set = set
frozenset = frozenset
-except NameError:
- try:
- from sets import Set as set, ImmutableSet as frozenset
- except ImportError:
- class _baseset(object):
- def __init__(self, values=()):
- self._data = {}
- warn("This implementation of Set is not complete !",
- stacklevel=2)
- for v in values:
- self._data[v] = 1
-
- def __or__(self, other):
- result = self.__class__(self._data.keys())
- for val in other:
- result.add(val)
- return result
- __add__ = __or__
-
- def __and__(self, other):
- result = self.__class__()
- for val in other:
- if val in self._data:
- result.add(val)
- return result
-
- def __sub__(self, other):
- result = self.__class__(self._data.keys())
- for val in other:
- if val in self._data:
- result.remove(val)
- return result
-
- def __cmp__(self, other):
- keys = self._data.keys()
- okeys = other._data.keys()
- keys.sort()
- okeys.sort()
- return cmp(keys, okeys)
-
- def __len__(self):
- return len(self._data)
-
- def __repr__(self):
- elements = self._data.keys()
- return 'lcc.%s(%r)' % (self.__class__.__name__, elements)
- __str__ = __repr__
-
- def __iter__(self):
- return iter(self._data)
-
- class frozenset(_baseset):
- """immutable set (can be set in dictionaries)"""
- def __init__(self, values=()):
- super(frozenset, self).__init__(values)
- self._hashcode = None
-
- def _compute_hash(self):
- """taken from python stdlib (sets.py)"""
- # Calculate hash code for a set by xor'ing the hash codes of
- # the elements. This ensures that the hash code does not depend
- # on the order in which elements are added to the set. This is
- # not called __hash__ because a BaseSet should not be hashable;
- # only an ImmutableSet is hashable.
- result = 0
- for elt in self:
- result ^= hash(elt)
- return result
-
- def __hash__(self):
- """taken from python stdlib (sets.py)"""
- if self._hashcode is None:
- self._hashcode = self._compute_hash()
- return self._hashcode
-
-
- class set(_baseset):
- """mutable set"""
- def add(self, value):
- self._data[value] = 1
-
- def remove(self, element):
- """removes <element> from set"""
- del self._data[element]
-
- def pop(self):
- """pops an arbitrary element from set"""
- return self._data.popitem()[0]
-
- def __hash__(self):
- """mutable set cannot be hashed."""
- raise TypeError("set objects are not hashable")
-
- del _baseset # don't explicitly provide this class
+except NameError:# Python 2.3 doesn't have `set`
+ from sets import Set as set, ImmutableSet as frozenset
+
+from logilab.common.deprecation import deprecated
+
+from itertools import izip, chain, imap
+izip = deprecated('izip exists in itertools since py2.3')(izip)
+imap = deprecated('imap exists in itertools since py2.3')(imap)
+chain = deprecated('chain exists in itertools since py2.3')(chain)
+
+sum = deprecated('sum exists in __builtins since py2.3')(sum)
+enumerate = deprecated('enumerate exists in __builtins since py2.3')(enumerate)
-try:
- from itertools import izip, chain, imap
-except ImportError:
- # from itertools documentation ###
- def izip(*iterables):
- iterables = map(iter, iterables)
- while iterables:
- result = [i.next() for i in iterables]
- yield tuple(result)
-
- def chain(*iterables):
- for it in iterables:
- for element in it:
- yield element
-
- def imap(function, *iterables):
- iterables = map(iter, iterables)
- while True:
- args = [i.next() for i in iterables]
- if function is None:
- yield tuple(args)
- else:
- yield function(*args)
-try:
- sum = sum
- enumerate = enumerate
-except NameError:
- # define the sum and enumerate functions (builtins introduced in py 2.3)
- import operator
- def sum(seq, start=0):
- """Returns the sum of all elements in the sequence"""
- return reduce(operator.add, seq, start)
-
- def enumerate(iterable):
- """emulates the python2.3 enumerate() function"""
- i = 0
- for val in iterable:
- yield i, val
- i += 1
- #return zip(range(len(iterable)), iterable)
try:
sorted = sorted
reversed = reversed
-except NameError:
+except NameError: # py2.3
def sorted(iterable, cmp=None, key=None, reverse=False):
original = list(iterable)
if key:
- l2 = [(key(elt), index) for index, elt in enumerate(original)]
+ l2 = [(key(elt), index) for index, elt in builtins.enumerate(original)]
else:
l2 = original
l2.sort(cmp)
@@ -239,9 +117,9 @@ except NameError:
l2.reverse()
return l2
-try: #
+try:
max = max
- max(("ab","cde"),key=len)
+ max(("ab","cde"),key=len) # does not work in py2.3
except TypeError:
def max( *args, **kargs):
if len(args) == 0:
@@ -386,6 +264,7 @@ else:
obj.__init__(*args, **kwargs)
return obj
+# XXX shouldn't we remove this and just let 2to3 do his job ?
# range or xrange?
try:
range = xrange