summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenn Knowles <kenn.knowles@gmail.com>2013-03-26 14:05:30 -0400
committerKenn Knowles <kenn.knowles@gmail.com>2013-03-26 14:05:30 -0400
commit4ae5f3f436455d0af311bef5605e2ad7bfa8011e (patch)
tree1969b11f83ce7830b73df9e70b9f4f2cf625afdf
parent47855aff86278c7051223a8eda57f506782ed02b (diff)
downloadjsonpath-rw-4ae5f3f436455d0af311bef5605e2ad7bfa8011e.tar.gz
Python 2 & 3 support
-rw-r--r--.travis.yml3
-rw-r--r--jsonpath_rw/__init__.py4
-rw-r--r--jsonpath_rw/jsonpath.py4
-rw-r--r--jsonpath_rw/lexer.py5
-rw-r--r--jsonpath_rw/parser.py5
-rw-r--r--tests/test_jsonpath.py19
-rw-r--r--tests/test_lexer.py3
-rw-r--r--tests/test_parser.py3
8 files changed, 30 insertions, 16 deletions
diff --git a/.travis.yml b/.travis.yml
index 921dd7a..eccc798 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,9 @@
language: python
python:
+ - "2.6"
- "2.7"
+ - "3.2"
+ - "3.3"
install:
- "pip install . --use-mirrors"
- "pip install pytest --use-mirrors"
diff --git a/jsonpath_rw/__init__.py b/jsonpath_rw/__init__.py
index adbcc84..eb8244e 100644
--- a/jsonpath_rw/__init__.py
+++ b/jsonpath_rw/__init__.py
@@ -1,2 +1,2 @@
-from jsonpath import *
-from parser import parse
+from .jsonpath import *
+from .parser import parse
diff --git a/jsonpath_rw/jsonpath.py b/jsonpath_rw/jsonpath.py
index ce1bfcc..ca2cc48 100644
--- a/jsonpath_rw/jsonpath.py
+++ b/jsonpath_rw/jsonpath.py
@@ -1,5 +1,7 @@
+from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes
import logging
import six
+from six.moves import xrange
from itertools import *
logger = logging.getLogger(__name__)
@@ -283,7 +285,7 @@ class Descendants(JSONPath):
# Manually do the * or [*] to avoid coercion and recurse just the right-hand pattern
if isinstance(datum.value, list):
recursive_matches = [submatch
- for i in xrange(0, len(datum.value))
+ for i in range(0, len(datum.value))
for submatch in match_recursively(DatumInContext(datum.value[i], context=datum, path=Index(i)))]
elif isinstance(datum.value, dict):
diff --git a/jsonpath_rw/lexer.py b/jsonpath_rw/lexer.py
index 9862caf..c7e42fd 100644
--- a/jsonpath_rw/lexer.py
+++ b/jsonpath_rw/lexer.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes
import sys
import logging
@@ -40,7 +41,7 @@ class JsonPathLexer(object):
reserved_words = { 'where': 'WHERE' }
- tokens = ['DOUBLEDOT', 'NUMBER', 'ID'] + reserved_words.values()
+ tokens = ['DOUBLEDOT', 'NUMBER', 'ID'] + list(reserved_words.values())
states = [ ('singlequote', 'exclusive'),
('doublequote', 'exclusive') ]
@@ -107,4 +108,4 @@ if __name__ == '__main__':
logging.basicConfig()
lexer = JsonPathLexer(debug=True)
for token in lexer.tokenize(sys.stdin.read()):
- print '%-20s%s' % (token.value, token.type)
+ print('%-20s%s' % (token.value, token.type))
diff --git a/jsonpath_rw/parser.py b/jsonpath_rw/parser.py
index 483c9ea..b8c1231 100644
--- a/jsonpath_rw/parser.py
+++ b/jsonpath_rw/parser.py
@@ -1,3 +1,4 @@
+from __future__ import print_function, absolute_import, division, generators, nested_scopes
import sys
import os.path
import logging
@@ -163,7 +164,7 @@ class IteratorToTokenStream(object):
def token(self):
try:
- return self.iterator.next()
+ return next(self.iterator)
except StopIteration:
return None
@@ -171,4 +172,4 @@ class IteratorToTokenStream(object):
if __name__ == '__main__':
logging.basicConfig()
parser = JsonPathParser(debug=True)
- print parser.parse(sys.stdin.read())
+ print(parser.parse(sys.stdin.read()))
diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py
index 38adc11..d097094 100644
--- a/tests/test_jsonpath.py
+++ b/tests/test_jsonpath.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes
import unittest
from jsonpath_rw import jsonpath # For setting the global auto_id_field flag
@@ -89,10 +90,12 @@ class TestJsonPath(unittest.TestCase):
# Also, we coerce iterables, etc, into the desired target type
for string, data, target in test_cases:
- print 'parse("%s").find(%s) =?= %s' % (string, data, target)
+ print('parse("%s").find(%s) =?= %s' % (string, data, target))
result = parse(string).find(data)
if isinstance(target, list):
assert [r.value for r in result] == target
+ elif isinstance(target, set):
+ assert {r.value for r in result} == target
else:
assert result.value == target
@@ -100,10 +103,10 @@ class TestJsonPath(unittest.TestCase):
jsonpath.auto_id_field = None
self.check_cases([ ('foo', {'foo': 'baz'}, ['baz']),
('foo,baz', {'foo': 1, 'baz': 2}, [1, 2]),
- ('*', {'foo': 1, 'baz': 2}, [1, 2]) ])
+ ('*', {'foo': 1, 'baz': 2}, {1, 2}) ])
jsonpath.auto_id_field = 'id'
- self.check_cases([ ('*', {'foo': 1, 'baz': 2}, [1, 2, '@']) ])
+ self.check_cases([ ('*', {'foo': 1, 'baz': 2}, {1, 2, '@'}) ])
def test_index_value(self):
self.check_cases([
@@ -146,10 +149,12 @@ class TestJsonPath(unittest.TestCase):
# Also, we coerce iterables, etc, into the desired target type
for string, data, target in test_cases:
- print 'parse("%s").find(%s).paths =?= %s' % (string, data, target)
+ print('parse("%s").find(%s).paths =?= %s' % (string, data, target))
result = parse(string).find(data)
if isinstance(target, list):
assert [str(r.full_path) for r in result] == target
+ elif isinstance(target, set):
+ assert {str(r.full_path) for r in result} == target
else:
assert str(result.path) == target
@@ -157,10 +162,10 @@ class TestJsonPath(unittest.TestCase):
jsonpath.auto_id_field = None
self.check_paths([ ('foo', {'foo': 'baz'}, ['foo']),
('foo,baz', {'foo': 1, 'baz': 2}, ['foo', 'baz']),
- ('*', {'foo': 1, 'baz': 2}, ['foo', 'baz']) ])
+ ('*', {'foo': 1, 'baz': 2}, {'foo', 'baz'}) ])
jsonpath.auto_id_field = 'id'
- self.check_paths([ ('*', {'foo': 1, 'baz': 2}, ['foo', 'baz', 'id']) ])
+ self.check_paths([ ('*', {'foo': 1, 'baz': 2}, {'foo', 'baz', 'id'}) ])
def test_index_paths(self):
self.check_paths([('[0]', [42], ['[0]']),
@@ -190,7 +195,7 @@ class TestJsonPath(unittest.TestCase):
('*.id',
{'foo':{'id': 1},
'baz': 2},
- ['1', 'baz']) ])
+ {'1', 'baz'}) ])
def test_index_auto_id(self):
jsonpath.auto_id_field = "id"
diff --git a/tests/test_lexer.py b/tests/test_lexer.py
index db0bcd1..53ad7ef 100644
--- a/tests/test_lexer.py
+++ b/tests/test_lexer.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes
import logging
import unittest
@@ -22,7 +23,7 @@ class TestLexer(unittest.TestCase):
stream2 = list(stream2)
assert len(stream1) == len(stream2)
for token1, token2 in zip(stream1, stream2):
- print token1, token2
+ print(token1, token2)
assert token1.type == token2.type
assert token1.value == token2.value
diff --git a/tests/test_parser.py b/tests/test_parser.py
index f5f208b..ed41966 100644
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes
import unittest
from jsonpath_rw.lexer import JsonPathLexer
@@ -15,7 +16,7 @@ class TestParser(unittest.TestCase):
parser = JsonPathParser(debug=True, lexer_class=lambda:JsonPathLexer(debug=False)) # Note that just manually passing token streams avoids this dep, but that sucks
for string, parsed in test_cases:
- print string, '=?=', parsed # pytest captures this and we see it only on a failure, for debugging
+ print(string, '=?=', parsed) # pytest captures this and we see it only on a failure, for debugging
assert parser.parse(string) == parsed
def test_atomic(self):