Permalink
Browse files

python3

  • Loading branch information...
1 parent 55abd18 commit d13c14f4c2651e26dfb8d72a93f517cac15407f0 @eire1130 eire1130 committed Apr 8, 2014
View
@@ -6,6 +6,8 @@ services:
python:
- "2.6"
- "2.7"
+ - "3.3"
+ - "3.4"
notifications:
email: false
View
@@ -2,6 +2,8 @@
import logging
from datetime import datetime
from operator import itemgetter
+import six
+from six import string_types
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk_index
@@ -87,7 +89,7 @@ def _build_key(urls, timeout, **settings):
# elasticsearch allows urls to be a string, so we make sure to
# account for that when converting whatever it is into a tuple.
- if isinstance(urls, basestring):
+ if isinstance(urls, string_types):
urls = (urls,)
else:
urls = tuple(urls)
@@ -257,7 +259,10 @@ class F(object):
"""
def __init__(self, **filters):
"""Creates an F"""
- filters = filters.items()
+ if six.PY2:
+ filters = filters.items()
+ else:
+ filters = list(filters.items())
if len(filters) > 1:
self.filters = [{'and': filters}]
else:
@@ -416,7 +421,7 @@ def to_python(self, obj):
This does the conversion in-place!
"""
- if isinstance(obj, basestring):
+ if isinstance(obj, string_types):
if len(obj) == 26:
try:
return datetime.strptime(obj, '%Y-%m-%dT%H:%M:%S.%f')
@@ -794,7 +799,8 @@ def filter(self, *filters, **kw):
details on adding support for new filter types.
"""
- return self._clone(next_step=('filter', list(filters) + kw.items()))
+ return self._clone(
+ next_step=('filter', list(filters) + list(kw.items())))
def filter_raw(self, filter_):
"""
@@ -926,7 +932,7 @@ def facet_raw(self, **kw):
Return a new S instance with raw facet args combined with
existing set.
"""
- return self._clone(next_step=('facet_raw', kw.items()))
+ return self._clone(next_step=('facet_raw', list(kw.items())))
def highlight(self, *fields, **kwargs):
"""Set highlight/excerpting with specified options.
@@ -1074,7 +1080,7 @@ def build_search(self):
if action == 'order_by':
sort = []
for key in value:
- if isinstance(key, basestring) and key.startswith('-'):
+ if isinstance(key, string_types) and key.startswith('-'):
sort.append({key[1:]: 'desc'})
else:
sort.append(key)
@@ -1191,7 +1197,7 @@ def build_search(self):
if explain:
qs['explain'] = True
- for suggestion, (term, kwargs) in suggestions.iteritems():
+ for suggestion, (term, kwargs) in six.iteritems(suggestions):
qs.setdefault('suggest', {})[suggestion] = {
'text': term,
'term': {
@@ -1226,7 +1232,10 @@ def _process_filters(self, filters):
continue
elif isinstance(f, dict):
- key = f.keys()[0]
+ if six.PY2:
+ key = f.keys()[0]
+ else:
+ key = list(f.keys())[0]
val = f[key]
key = key.strip('_')
@@ -1433,7 +1442,7 @@ def get_indexes(self, default_indexes=DEFAULT_INDEXES):
if self.type is not None:
indexes = self.type.get_index()
- if isinstance(indexes, basestring):
+ if isinstance(indexes, string_types):
indexes = [indexes]
return indexes
@@ -1,3 +1,4 @@
+import six
import logging
from functools import wraps
@@ -177,14 +178,14 @@ def get_indexes(self, default_indexes=None):
doctype = self.type.get_mapping_type_name()
indexes = (settings.ES_INDEXES.get(doctype) or
settings.ES_INDEXES['default'])
- if isinstance(indexes, basestring):
+ if isinstance(indexes, six.string_types):
indexes = [indexes]
return super(S, self).get_indexes(default_indexes=indexes)
def get_doctypes(self, default_doctypes=None):
"""Returns the doctypes (or mapping type names) to use."""
doctypes = self.type.get_mapping_type_name()
- if isinstance(doctypes, basestring):
+ if isinstance(doctypes, six.string_types):
doctypes = [doctypes]
return super(S, self).get_doctypes(default_doctypes=doctypes)
@@ -238,7 +239,7 @@ def get_index(cls):
"""
indexes = settings.ES_INDEXES
index = indexes.get(cls.get_mapping_type_name()) or indexes['default']
- if not (isinstance(index, basestring)):
+ if not (isinstance(index, six.string_types)):
# FIXME - not sure what to do here, but we only want one
# index and somehow this isn't one index.
index = index[0]
@@ -1,11 +1,13 @@
"""
With `test_utils` you can use this testcase.
"""
+from __future__ import print_function
from django.test import TestCase
from django.conf import settings
from elasticsearch.exceptions import ConnectionError
from elasticsearch.helpers import bulk_index
+import six
# Try really really hard to find a valid skip thing.
try:
@@ -24,7 +26,7 @@ def skip_this_test():
skip('skipping: es not set up')
except ImportError:
def skip_this_test():
- print 'SKIPPING: es not set up'
+ print('SKIPPING: es not set up')
return
@@ -33,15 +35,15 @@ def skip_this_test():
def testify(indexes):
"""Returns indexes with '_eutest' suffix.
-
+
:arg indexes: dict of mapping type name -> index name(s)
-
+
:returns: dict with ``_eutest`` appended to all index names
-
+
"""
ret = {}
for k, v in indexes.items():
- if isinstance(v, basestring):
+ if isinstance(v, six.string_types):
ret[k] = v + '_eutest'
elif isinstance(v, (list, tuple)):
ret[k] = [v_item + '_eutest' for v_item in v]
@@ -8,6 +8,7 @@
InvalidFlagsError, SearchResults, DefaultMappingType, MappingType,
DEFAULT_INDEXES, DEFAULT_DOCTYPES)
from elasticutils.tests import ESTestCase, facet_counts_dict, require_version
+import six
def eqish_(item1, item2):
@@ -476,8 +477,13 @@ def _get_queries(search):
# with a single key. So we extract that and put it in a
# dict so we don't have to deal with the order of things
# in the 'must' list.
- return dict([clause.items()[0]
+ if six.PY2:
+ out = dict([clause.items()[0]
for clause in search['query']['bool']['must']])
+ else:
+ out = dict([list(clause.items())[0]
+ for clause in search['query']['bool']['must']])
+ return out
q1 = self.get_s().boost(foo=4.0).query(foo='car', foo__prefix='car')
eq_(_get_queries(q1.build_search())['term']['foo']['boost'], 4.0)
@@ -87,7 +87,9 @@ def test_values_list_no_fields(self):
assert isinstance(searcher[0], tuple)
# We sort the result and expected result here so that the
# order is stable and comparable.
- eq_(sorted(searcher[0]), sorted((u'2', u'bar', u'awesome', 1)))
+ eq_(
+ sorted(searcher[0], key=str),
+ sorted((u'2', u'bar', u'awesome', 1), key=str))
def test_values_list_results(self):
"""With values_list fields, returns list of tuples."""
View
@@ -4,3 +4,4 @@
nose
Sphinx
tox
+six
View
@@ -28,7 +28,8 @@ def get_version():
license='BSD',
packages=find_packages(),
install_requires=[
- 'elasticsearch >= 0.4.3, < 1.0'
+ 'elasticsearch >= 0.4.3, < 1.0',
+ 'six'
],
include_package_data=True,
classifiers=[

0 comments on commit d13c14f

Please sign in to comment.