Skip to content

Commit

Permalink
Add support for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
griffy committed May 22, 2015
1 parent f205e5b commit 28eeae4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
39 changes: 21 additions & 18 deletions pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@
# limitations under the License.
#
from __future__ import division
import urllib2
from __future__ import absolute_import

from six import iteritems
from six.moves import urllib

try:
import pandas
except ImportError:
print 'Warning: unable to import Pandas. The export_pandas method will not work.'
print('Warning: unable to import Pandas. The export_pandas method will not work.')
pass

from utils.aggregators import *
from utils.postaggregator import *
from utils.filters import *
from utils.having import *
from utils.query_utils import *

from .utils.aggregators import *
from .utils.postaggregator import *
from .utils.filters import *
from .utils.having import *
from .utils.query_utils import *

class PyDruid:
"""
Expand Down Expand Up @@ -106,23 +108,24 @@ def __init__(self, url, endpoint):

def __post(self, query):
try:
querystr = json.dumps(query)
querystr = json.dumps(query).encode('ascii')
if self.url.endswith('/'):
url = self.url + self.endpoint
else:
url = self.url + '/' + self.endpoint
headers = {'Content-Type': 'application/json'}
req = urllib2.Request(url, querystr, headers)
res = urllib2.urlopen(req)
req = urllib.request.Request(url, querystr, headers)
res = urllib.request.urlopen(req)
data = res.read()
self.result_json = data
res.close()
except urllib2.HTTPError, e:
except urllib.error.HTTPError:
_, e, _ = sys.exc_info()
err=None
if e.code==500:
# has Druid returned an error?
try:
err= json.loads(e.read())
err= json.loads(e.read())
except ValueError:
pass
else:
Expand Down Expand Up @@ -244,19 +247,19 @@ def export_pandas(self):
"""
if self.result:
if self.query_type == "timeseries":
nres = [v['result'].items() + [('timestamp', v['timestamp'])]
nres = [list(v['result'].items()) + [('timestamp', v['timestamp'])]
for v in self.result]
nres = [dict(v) for v in nres]
elif self.query_type == "topN":
nres = []
for item in self.result:
timestamp = item['timestamp']
results = item['result']
tres = [dict(res.items() + [('timestamp', timestamp)])
tres = [dict(list(res.items()) + [('timestamp', timestamp)])
for res in results]
nres += tres
elif self.query_type == "groupBy":
nres = [v['event'].items() + [('timestamp', v['timestamp'])]
nres = [list(v['event'].items()) + [('timestamp', v['timestamp'])]
for v in self.result]
nres = [dict(v) for v in nres]
else:
Expand All @@ -280,7 +283,7 @@ def validate_query(self, valid_parts, args):
:raise ValueError: if an invalid object is given
"""
valid_parts = valid_parts[:] + ['context']
for key, val in args.iteritems():
for key, val in iteritems(args):
if key not in valid_parts:
raise ValueError(
'Query component: {0} is not valid for query type: {1}.'
Expand All @@ -291,7 +294,7 @@ def validate_query(self, valid_parts, args):
def build_query(self, args):
query_dict = {'queryType': self.query_type}

for key, val in args.iteritems():
for key, val in iteritems(args):
if key == 'aggregations':
query_dict[key] = build_aggregators(val)
elif key == 'post_aggregations':
Expand Down
6 changes: 3 additions & 3 deletions pydruid/utils/aggregators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

from six import iteritems

def longsum(raw_metric):
return {"type": "longSum", "fieldName": raw_metric}
Expand All @@ -38,5 +38,5 @@ def hyperunique(raw_metric):
return {"type": "hyperUnique", "fieldName": raw_metric}

def build_aggregators(agg_input):
return [dict([('name', k)] + v.items())
for (k, v) in agg_input.iteritems()]
return [dict([('name', k)] + list(v.items()))
for (k, v) in iteritems(agg_input)]
4 changes: 2 additions & 2 deletions pydruid/utils/query_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
import csv
import codecs
import cStringIO
from six import StringIO

# A special CSV writer which will write rows to TSV file "f", which is encoded in utf-8.
# this is necessary because the values in druid are not all ASCII.
Expand All @@ -26,7 +26,7 @@ class UnicodeWriter:
# delimiter="\t"
def __init__(self, f, dialect="excel-tab", encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.queue = StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
long_description='See https://github.com/metamx/pydruid for more information.',
install_requires=[
"simplejson >= 3.3.0",
"six >= 1.9.0",
],
)

0 comments on commit 28eeae4

Please sign in to comment.