diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2009-06-29 16:53:18 +0200 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2009-06-29 16:53:18 +0200 |
commit | b85ce269cf16e68fcc5ae407b2582fd6b1b526ce (patch) | |
tree | 2ebd0f5be7979818675dab0676131734175f7db6 | |
parent | 1fd66a3f81e81c8816c1cc761e42db07895c466c (diff) | |
download | logilab-common-b85ce269cf16e68fcc5ae407b2582fd6b1b526ce.tar.gz |
d-t-w
-rw-r--r-- | compat.py | 32 | ||||
-rw-r--r-- | deprecation.py | 6 |
2 files changed, 18 insertions, 20 deletions
@@ -37,28 +37,28 @@ except NameError: 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) @@ -75,7 +75,7 @@ except NameError: 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 @@ -87,14 +87,14 @@ except NameError: 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): @@ -131,7 +131,7 @@ except ImportError: for it in iterables: for element in it: yield element - + def imap(function, *iterables): iterables = map(iter, iterables) while True: @@ -139,7 +139,7 @@ except ImportError: if function is None: yield tuple(args) else: - yield function(*args) + yield function(*args) try: sum = sum enumerate = enumerate @@ -161,7 +161,7 @@ try: sorted = sorted reversed = reversed except NameError: - + def sorted(iterable, cmp=None, key=None, reverse=False): original = list(iterable) if key: @@ -174,7 +174,7 @@ except NameError: if key: return [original[index] for elt, index in l2] return l2 - + def reversed(l): l2 = list(l) l2.reverse() @@ -199,13 +199,13 @@ except TypeError: items = iter(args[0]) else: items = iter(args) - + try: best_item = items.next() best_value = key(best_item) except StopIteration: raise ValueError("max() arg is an empty sequence") - + for item in items: value = key(item) if value > best_value: @@ -215,9 +215,6 @@ except TypeError: return best_item - - - # Python2.5 builtins try: any = any @@ -232,7 +229,7 @@ except NameError: if elt: return True return False - + def all(iterable): """all(iterable) -> bool @@ -243,6 +240,7 @@ except NameError: return False return True + # Python2.5 subprocess added functions and exceptions from subprocess import Popen class CalledProcessError(Exception): diff --git a/deprecation.py b/deprecation.py index b2250ab..0822f07 100644 --- a/deprecation.py +++ b/deprecation.py @@ -12,7 +12,7 @@ from warnings import warn class deprecated(type): """metaclass to print a warning on instantiation of a deprecated class""" - + def __call__(cls, *args, **kwargs): msg = getattr(cls, "__deprecation_warning__", "%s is deprecated" % cls.__name__) @@ -23,7 +23,7 @@ class deprecated(type): def class_renamed(old_name, new_class, message=None): """automatically creates a class which fires a DeprecationWarning when instantiated. - + >>> Set = class_renamed('Set', set, 'Set is now replaced by set') >>> s = Set() sample.py:57: DeprecationWarning: Set is now replaced by set @@ -64,7 +64,7 @@ def deprecated_function(new_func, message=None): """Creates a function which fires a DeprecationWarning when used. For example, if <bar> is deprecated in favour of <foo>: - + >>> bar = deprecated_function(foo, 'bar is deprecated') >>> bar() sample.py:57: DeprecationWarning: bar is deprecated |