[kupfer] relevance: Port to Python 3 for dual compatibility.
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer] relevance: Port to Python 3 for dual compatibility.
- Date: Tue, 15 Sep 2009 17:53:49 +0000 (UTC)
commit c8c940730a8208084a123027d7ca8e9baf5435c0
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Mon Sep 14 23:43:25 2009 +0200
relevance: Port to Python 3 for dual compatibility.
We only need one small trick to be Python 2 and 3 compatible: On
Python 2, we assign "range = xrange", and on Python 3 we catch the
NameError and ignore.
kupfer/relevance.py | 20 ++++++++++++++------
1 files changed, 14 insertions(+), 6 deletions(-)
---
diff --git a/kupfer/relevance.py b/kupfer/relevance.py
index 93b2bab..22c19e2 100644
--- a/kupfer/relevance.py
+++ b/kupfer/relevance.py
@@ -28,10 +28,18 @@ based on the relevance. It originates in Gnome-Do.
* Module updated by Ulrik Sverdrup to clean up and dramatically speed up
the code, by using more pythonic constructs as well as doing less work.
+
+Compatibility: Python 2.4 and later, including Python 3
"""
from __future__ import division
+# This module is compatible with both Python 2 and Python 3;
+# we need the iterator form of range for either version, stored in range()
+try:
+ range = xrange
+except NameError:
+ pass
def formatCommonSubstrings(s, query, format_clean=None, format_match=None):
"""
@@ -62,7 +70,7 @@ def formatCommonSubstrings(s, query, format_clean=None, format_match=None):
return format(s)
# find longest perfect match, put in slc
- for slc in xrange(len(query), 0, -1):
+ for slc in range(len(query), 0, -1):
if query[:slc] == ls[first:first+slc]:
break
key, nextkey = query[:slc], query[slc:]
@@ -93,13 +101,13 @@ def score(s, query):
Returns: a float between 0 and 1
- >>> print score('terminal', 'trml')
+ >>> print(score('terminal', 'trml'))
0.735098684211
- >>> print score(u'terminal', u'term')
+ >>> print(score('terminal', 'term'))
0.992302631579
- >>> print score('terminal', 'try')
+ >>> print(score('terminal', 'try'))
0.0
- >>> print score('terminal', '')
+ >>> print(score('terminal', ''))
1.0
"""
if not query:
@@ -123,7 +131,7 @@ def score(s, query):
good = 0
bad = 1
firstCount = 0
- for i in xrange(first, last-1):
+ for i in range(first, last-1):
if ls[i] in " -":
if ls[i + 1] in query:
firstCount += 1
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]