Add Python 3 compatibility imports

This commit is contained in:
Jason R. Coombs 2012-10-07 20:35:28 -04:00
parent 77b6a1d988
commit de99fce9fb

View File

@ -1,5 +1,10 @@
import urllib try:
import urllib2 import urllib.parse as urllib_parse
import urllib.request as urllib_request
except ImportError:
import urllib as urllib_parse
import urllib2 as urllib_request
from xml.etree import ElementTree as etree from xml.etree import ElementTree as etree
class Result(object): class Result(object):
@ -54,12 +59,12 @@ class Client(object):
""" """
Query Wolfram|Alpha with query using the v2.0 API Query Wolfram|Alpha with query using the v2.0 API
""" """
query = urllib.urlencode(dict( query = urllib_parse.urlencode(dict(
input=query, input=query,
appid=self.app_id, appid=self.app_id,
)) ))
url = 'http://api.wolframalpha.com/v2/query?' + query url = 'http://api.wolframalpha.com/v2/query?' + query
resp = urllib2.urlopen(url) resp = urllib_request.urlopen(url)
assert resp.headers.gettype() == 'text/xml' assert resp.headers.gettype() == 'text/xml'
assert resp.headers.getparam('charset') == 'utf-8' assert resp.headers.getparam('charset') == 'utf-8'
return Result(resp) return Result(resp)