[distances] Correctly check if index is not None rather than if index, as one can used the first column (index=0) for processing, closes #257026
authorVincent Michel <vincent.michel@logilab.fr>
Tue, 15 Jul 2014 17:34:38 +0000
changeset 461 d5dc8d2c4311
parent 458 9527d4b3d381
child 462 cf376d4f1a5a
[distances] Correctly check if index is not None rather than if index, as one can used the first column (index=0) for processing, closes #257026
test/test_distances.py
utils/distances.py
--- a/test/test_distances.py	Tue Jul 01 15:10:34 2014 +0200
+++ b/test/test_distances.py	Tue Jul 15 17:34:38 2014 +0000
@@ -174,6 +174,20 @@
         pdist = processing.pdist(_input)
         self.assertEqual([1, 0., 1], pdist)
 
+    def test_index(self):
+        processing = ExactMatchProcessing()
+        ref_record = ['Victor Hugo', 0]
+        target_record = ['Victor Hugo', 1]
+        d = processing.distance(ref_record, target_record)
+        self.assertEqual(d, 1)
+
+    def test_index_2(self):
+        processing = ExactMatchProcessing(ref_attr_index=0, target_attr_index=0)
+        ref_record = ['Victor Hugo', 0]
+        target_record = ['Victor Hugo', 1]
+        d = processing.distance(ref_record, target_record)
+        self.assertEqual(d, 0)
+
 
 class LevenshteinTestCase(unittest.TestCase):
 
--- a/utils/distances.py	Tue Jul 01 15:10:34 2014 +0200
+++ b/utils/distances.py	Tue Jul 15 17:34:38 2014 +0000
@@ -387,7 +387,7 @@
         if isinstance(index, tuple) and len(index) == 2:
             return (record[index[0]], record[index[1]])
         else:
-            return (record[index] if index else record)
+            return (record[index] if index is not None else record)
 
     def distance(self, reference_record, target_record):
         """ Compute the distance between two records