fix py3 incompatibilities found by running tox -e py36
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Mon, 03 Sep 2018 11:30:09 +0200
changeset 543 a66a6b3ef992
parent 542 aabf5c567bb2
child 544 9c919d23bcd2
fix py3 incompatibilities found by running tox -e py36
nazca/utils/dataio.py
nazca/utils/distances.py
--- a/nazca/utils/dataio.py	Mon Sep 03 10:50:59 2018 +0200
+++ b/nazca/utils/dataio.py	Mon Sep 03 11:30:09 2018 +0200
@@ -20,10 +20,10 @@
 import codecs
 import json
 import csv
-import urllib
 
-from six import string_types, iteritems
+from six import binary_type, string_types, iteritems
 from six.moves import xrange
+from six.moves.urllib.request import urlopen
 
 from lxml import etree
 
@@ -48,7 +48,7 @@
             return float(data.replace(',', '.'))
         except ValueError:
             data = data.strip()
-            if encoding:
+            if encoding and isinstance(data, binary_type):
                 return data.decode(encoding)
             return data
 
@@ -86,9 +86,9 @@
         if host.endswith('/'):
             host = host[:-1]
         indexes = indexes or []
-        filehandle = urllib.urlopen('%(host)s/view?'
-                                    'rql=%(rql)s&vid=csvexport'
-                                    % {'rql': rql, 'host': host})
+        filehandle = urlopen('%(host)s/view?'
+                             'rql=%(rql)s&vid=csvexport'
+                             % {'rql': rql, 'host': host})
         filehandle.readline()  # Skip the first line
         return parsefile(filehandle, delimiter=';', indexes=indexes,
                          formatopt=formatopt, autocast_data=autocast_data)
@@ -315,7 +315,7 @@
             outfile.write(line)
         outfile.close()
         count += 1
-    return map(str, xrange(count))
+    return list(map(str, xrange(count)))
 
 
 ###############################################################################
@@ -368,7 +368,7 @@
     in valid XHTML.
     """
 
-    XHTML_DOC_TEMPLATE = '''\
+    XHTML_DOC_TEMPLATE = b'''\
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
@@ -380,8 +380,10 @@
 </html>'''  # noqa
 
     def is_valid(self, html):
+        if isinstance(html, string_types):
+            html = html.encode('utf-8')
         try:
-            etree.fromstring(self.XHTML_DOC_TEMPLATE % html.encode('utf-8'),
+            etree.fromstring(self.XHTML_DOC_TEMPLATE % html,
                              parser=etree.XMLParser(dtd_validation=True))
         except etree.XMLSyntaxError:
             return False
--- a/nazca/utils/distances.py	Mon Sep 03 10:50:59 2018 +0200
+++ b/nazca/utils/distances.py	Mon Sep 03 11:30:09 2018 +0200
@@ -148,7 +148,7 @@
 
     lenb = len(strb)
     onerowago = None
-    thisrow = range(1, lenb + 1) + [0]
+    thisrow = list(range(1, lenb + 1)) + [0]
     for x in xrange(len(stra)):
         onerowago, thisrow = thisrow, [0] * lenb + [x + 1]
         for y in xrange(lenb):