Reputation: 6513
I'm looking for suggestions on how to combine the two code snippets so that they work with both python 2 and 3. The goal is to make it "neat", ideally keeping it to one line and limiting any if/else/try/except constructs.
For python 3.x
import xml.etree.ElementTree as ET, urllib.request, gzip, io
url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urllib.request.urlopen(url).read())))
For python 2.x
import xml.etree.ElementTree as ET, urllib, gzip, io
url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urllib.urlopen(url).read())))
Upvotes: 6
Views: 5269
Reputation: 17285
This is exactly what six
was created for. It's a library designed to allow your code to work with both Python 2 and 3. (Don't let "library" scare you, it's intentionally just a single .py file to make it very easy to integrate/package.)
Instead of using the built-in urllib
module, you'd use six's version which automatically redirects to the built-in module in both Python 2 and 3.
Here's what your code would look like:
import xml.etree.ElementTree as ET, gzip, io
from six.moves.urllib.request import urlopen
url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urlopen(url).read())))
See: https://six.readthedocs.io/#module-six.moves.urllib.request
Upvotes: 11
Reputation: 310079
If you don't want an extra dependency, you could simply use a try
except
block to import either module under the same alias...:
try:
import urllib.request as urlrequest
except ImportError:
import urllib as urlrequest
url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urlrequest.urlopen(url).read())))
Upvotes: 13