python-modernize -w
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 02 May 2018 18:27:22 +0200
changeset 524 cdc8b5ada1a2
parent 523 cbd338590fa4
child 525 f0a04950e103
python-modernize -w
examples/demo.py
setup.py
test/test_alignment.py
test/test_blocking.py
test/test_dataio.py
test/test_distances.py
test/test_filters.py
test/test_minhashing.py
test/test_ner.py
test/test_normalize.py
test/test_preprocessors.py
test/test_tokenizer.py
--- a/examples/demo.py	Wed May 02 18:25:46 2018 +0200
+++ b/examples/demo.py	Wed May 02 18:27:22 2018 +0200
@@ -1,9 +1,11 @@
 #!/usr/bin/python
 #-*- coding:utf-8 -*-
 
+from __future__ import absolute_import
+from __future__ import print_function
 from os import path
 
-import urllib
+import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error
 
 import nazca.distances as ald
 import nazca.normalize as aln
@@ -22,7 +24,7 @@
         return string
 
 def parserql(host, rql):
-    filehandle = urllib.urlopen('%(host)sview?'
+    filehandle = six.moves.urllib.request.urlopen('%(host)sview?'
                                 'rql=%(rql)s&vid=csvexport'
                                 % {'rql': rql, 'host': host})
     filehandle.readline()
@@ -44,9 +46,9 @@
        }
     """
 
-    print "Sending query to dbpedia"
+    print("Sending query to dbpedia")
     targetset = sparqlquery('http://dbpedia.org/sparql', query)
-    print "Reading the prixgoncourt file"
+    print("Reading the prixgoncourt file")
     alignset = parsefile(dpath('prixgoncourt'), indexes=[1, 1])
 
     tr_name = {'normalization': [lambda x:remove_after(x, '('),
@@ -56,11 +58,11 @@
 
     processings = {1: tr_name}
 
-    print "Alignment started"
+    print("Alignment started")
     align(alignset, targetset, 0.4, processings,
           dpath('demo0_results'))
 
-    print "Done, see the resuls in %s" % dpath('demo0_results')
+    print("Done, see the resuls in %s" % dpath('demo0_results'))
 
 def demo_1():
     # FR.txt is an extract of geonames, where locations have been sorted by name
@@ -70,7 +72,7 @@
     # position (longitude, latitude)
     # ``nbmax`` is the number of locations to load
 
-    print "Parsing the input files"
+    print("Parsing the input files")
     targetset = parsefile(dpath('FR.txt'), indexes=[0, 1, (4, 5)],
                           nbmax=2000)
     alignset = parsefile(dpath('frenchbnf'),
@@ -94,7 +96,7 @@
 
     processings = {1: tr_name, 2: tr_geo}
 
-    print "Alignment started"
+    print("Alignment started")
     align(alignset,           # The dataset to align
           targetset,          # The target dataset
           0.4,                # The maximal distance
@@ -108,7 +110,7 @@
     # 0. the computed distance matrix
     # 1. a boolean, True if at least one alignment has been done, False
     #    otherwise
-    print "Done, see the results in %s" % dpath('demo1_results')
+    print("Done, see the results in %s" % dpath('demo1_results'))
 
 def demo_2():
     targetset = parsefile(dpath('FR.txt'), indexes=[0, 1, (4, 5)],
@@ -116,7 +118,7 @@
     alignset = parsefile(dpath('frenchbnf'), indexes=[0, 2, (14, 12)],
                          formatopt={2:lambda x:x.decode('utf-8')}, nbmax=30000)
 
-    print "Finding neighbours"
+    print("Finding neighbours")
     neighbours = findneighbours(alignset, targetset, indexes=(2, 2),
                                mode='minibatch')
 
@@ -129,9 +131,9 @@
               }
 
     processings = {1: tr_name}
-    print "Start computation"
+    print("Start computation")
     for ind, (alignid, targetid) in enumerate(neighbours):
-        print '%3d' % ind, len(alignid), 'x', len(targetid)
+        print('%3d' % ind, len(alignid), 'x', len(targetid))
         _, matched = subalign(alignset,   # The dataset to align
                               targetset,  # The target dataset
                               alignid,
@@ -139,26 +141,26 @@
                               0.3,
                               processings)
         write_results(matched, alignset, targetset, dpath('demo2_results'))
-    print "Done, see the results in %s" % dpath('demo2_results')
+    print("Done, see the results in %s" % dpath('demo2_results'))
 
 def demo_3():
-    print "Parsing files"
+    print("Parsing files")
     alignset = parserql(host='http://demo.cubicweb.org/elections/',
                         rql='Any E, N WHERE X is Commune, X eid E, X label N')
     targetset = parsefile(dpath('FR.txt'), indexes=[0, 1])
-    print '%s×%s' % (len(alignset), len(targetset))
+    print('%s×%s' % (len(alignset), len(targetset)))
 
     tr_name = {'normalization': [aln.simplify],
                'metric': 'levenshtein'
               }
 
-    print "Alignment started"
+    print("Alignment started")
     results = alignall(alignset, targetset, 0.75, processings={1: tr_name},
                        indexes=(1,1), mode='minhashing', kwordsgram=1, siglen=200,
                        uniq=True)
     dicresults = dict([(a, b) for (a, b) in results])
 
-    print "Done, writing output"
+    print("Done, writing output")
 
     with open(dpath('demo3_results'), 'w') as fout:
         for line in alignset:
@@ -167,7 +169,7 @@
                    % (line[0], dicresults.get(line[0], 'not_found'))
             fout.write(sent.encode('utf-8'))
 
-    print "See the results in %s" % dpath('demo3_results')
+    print("See the results in %s" % dpath('demo3_results'))
 
 if __name__ == '__main__':
     import sys
@@ -176,22 +178,22 @@
 
     t = time()
     if runall or '0' in sys.argv:
-        print "Running demo_0"
+        print("Running demo_0")
         demo_0()
 
     if runall or '1' in sys.argv:
-        print "Running demo_1"
+        print("Running demo_1")
         demo_1()
 
     if runall or '2' in sys.argv:
-        print "Running demo_2"
+        print("Running demo_2")
         ## Same as demo_1, but in a more efficient way, using a method to find
         ## neighbours
         demo_2()
 
     if runall or '3' in sys.argv:
-        print "Running demo_3"
+        print("Running demo_3")
         demo_3()
 
-    print "Demo terminated"
-    print "Took %d min" % ((time() - t)/60.)
+    print("Demo terminated")
+    print("Took %d min" % ((time() - t)/60.))
--- a/setup.py	Wed May 02 18:25:46 2018 +0200
+++ b/setup.py	Wed May 02 18:27:22 2018 +0200
@@ -19,6 +19,7 @@
 
 """Generic Setup script, takes package info from __pkginfo__.py file.
 """
+from __future__ import absolute_import
 __docformat__ = "restructuredtext en"
 
 from os.path import join, dirname
--- a/test/test_alignment.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_alignment.py	Wed May 02 18:27:22 2018 +0200
@@ -16,7 +16,9 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
+import six
 if sys.version_info >= (2, 7):
     import unittest
 else:
@@ -51,7 +53,7 @@
         aligner = alig.BaseAligner(threshold=30, processings=processings)
         mat, matched = aligner.align(refset, targetset)
         true_matched = [(0,0), (0, 2), (1,2), (3,1)]
-        for k, values in matched.iteritems():
+        for k, values in six.iteritems(matched):
             for v, distance in values:
                 self.assertIn((k,v), true_matched)
 
@@ -76,7 +78,7 @@
         predict_matched = set()
         for alignind, targetind in blocking.iter_indice_blocks():
             mat, matched = aligner._get_match(refset, targetset, alignind, targetind)
-            for k, values in matched.iteritems():
+            for k, values in six.iteritems(matched):
                 for v, distance in values:
                     predict_matched.add((k, v))
         self.assertEqual(true_matched, predict_matched)
@@ -100,7 +102,7 @@
                                                      threshold=0.3))
         global_mat, global_matched = aligner.align(refset, targetset)
         predict_matched = set()
-        for k, values in global_matched.iteritems():
+        for k, values in six.iteritems(global_matched):
             for v, distance in values:
                 predict_matched.add((k, v))
         self.assertEqual(true_matched, predict_matched)
--- a/test/test_blocking.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_blocking.py	Wed May 02 18:27:22 2018 +0200
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest
--- a/test/test_dataio.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_dataio.py	Wed May 02 18:27:22 2018 +0200
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest
--- a/test/test_distances.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_distances.py	Wed May 02 18:27:22 2018 +0200
@@ -16,7 +16,9 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
+from six.moves import range
 if sys.version_info >= (2, 7):
     import unittest
 else:
@@ -204,8 +206,8 @@
         i1, i2 = self.input1, self.input2
         m = self.matrix
 
-        for i in xrange(len(i1)):
-            for j in xrange(len(i2)):
+        for i in range(len(i1)):
+            for j in range(len(i2)):
                 self.assertAlmostEqual(m[i, j], d(i1[i], i2[j]), 4)
 
     def test_operation(self):
--- a/test/test_filters.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_filters.py	Wed May 02 18:27:22 2018 +0200
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest
--- a/test/test_minhashing.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_minhashing.py	Wed May 02 18:27:22 2018 +0200
@@ -15,6 +15,7 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest
--- a/test/test_ner.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_ner.py	Wed May 02 18:27:22 2018 +0200
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest
--- a/test/test_normalize.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_normalize.py	Wed May 02 18:27:22 2018 +0200
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest
--- a/test/test_preprocessors.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_preprocessors.py	Wed May 02 18:27:22 2018 +0200
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest
--- a/test/test_tokenizer.py	Wed May 02 18:25:46 2018 +0200
+++ b/test/test_tokenizer.py	Wed May 02 18:27:22 2018 +0200
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 import sys
 if sys.version_info >= (2, 7):
     import unittest