[Python-modules-commits] [python-pyld] 247/276: pyld: functools.cmp_to_key Python 2.6

Wolfgang Borgert debacle at moszumanska.debian.org
Wed Oct 8 23:48:16 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository python-pyld.

commit 37d57e11b231d0fb68726c5092da84d2afebc1c0
Author: Adrian-Tudor Panescu <adrian.tudor.panescu at cern.ch>
Date:   Thu Feb 27 15:37:30 2014 +0100

    pyld: functools.cmp_to_key Python 2.6
    
    * Port inexistent functools function for Python <=2.6. Related pull request:
      https://github.com/digitalbazaar/pyld/pull/20
    
    Signed-off-by: Adrian-Tudor Panescu <adrian.tudor.panescu at cern.ch>
---
 lib/pyld/jsonld.py | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/lib/pyld/jsonld.py b/lib/pyld/jsonld.py
index 7ea840b..5fcafcf 100644
--- a/lib/pyld/jsonld.py
+++ b/lib/pyld/jsonld.py
@@ -38,9 +38,43 @@ import sys
 import traceback
 from collections import deque
 from contextlib import closing
-from functools import cmp_to_key
 from numbers import Integral, Real
 
+try:
+    from functools import cmp_to_key
+except ImportError:
+    def cmp_to_key(mycmp):
+        """
+        Convert a cmp= function into a key= function
+
+        Source: http://hg.python.org/cpython/file/default/Lib/functools.py
+        """
+        class K(object):
+            __slots__ = ['obj']
+
+            def __init__(self, obj):
+                self.obj = obj
+
+            def __lt__(self, other):
+                return mycmp(self.obj, other.obj) < 0
+
+            def __gt__(self, other):
+                return mycmp(self.obj, other.obj) > 0
+
+            def __eq__(self, other):
+                return mycmp(self.obj, other.obj) == 0
+
+            def __le__(self, other):
+                return mycmp(self.obj, other.obj) <= 0
+
+            def __ge__(self, other):
+                return mycmp(self.obj, other.obj) >= 0
+
+            def __ne__(self, other):
+                return mycmp(self.obj, other.obj) != 0
+            __hash__ = None
+        return K
+
 # support python 2
 if sys.version_info.major >= 3:
     from urllib.request import build_opener as urllib_build_opener

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-pyld.git



More information about the Python-modules-commits mailing list