8000 More tests and fixes for Slice · j3hyde/python-jsonpath-rw@f5e6e39 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5e6e39

Browse files
committed
More tests and fixes for Slice
1 parent cadfa32 commit f5e6e39

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

jsonpath_rw/jsonpath.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,20 +458,20 @@ def __init__(self, start=None, end=None, step=None):
458458
self.end = end
459459
self.step = step
460460

461-
def find(self, data):
461+
def find(self, datum):
462+
datum = DatumInContext.wrap(datum)
463+
462464
# Here's the hack. If it is a dictionary or some kind of constant,
463465
# put it in a single-element list
464-
if (isinstance(data, dict) or isinstance(data, six.integer_types)
465-
or isinstance(data, six.string_types)):
466-
467-
return self.find([data])
466+
if (isinstance(datum.value, dict) or isinstance(datum.value, six.integer_types) or isinstance(datum.value, six.string_types)):
467+
return self.find(DatumInContext([datum.value], path=datum.path, context=datum.context))
468468

469469
# Some iterators do not support slicing but we can still
470470
# at least work for '*'
471471
if self.start == None and self.end == None and self.step == None:
472-
return [DatumInContext(data[i], Index(i)) for i in xrange(0, len(data))]
472+
return [DatumInContext(datum.value[i], path=Index(i), context=datum) for i in xrange(0, len(datum.value))]
473473
else:
474-
return [DatumInContext(data[i], Index(i)) for i in range(0, len(data))[self.start:self.end:self.step]]
474+
return [DatumInContext(datum.value[i], path=Index(i), context=datum) for i in range(0, len(datum.value))[self.start:self.end:self.step]]
475475

476476
def __str__(self):
477477
if self.start == None and self.end == None and self.step == None:
@@ -481,5 +481,8 @@ def __str__(self):
481481
':%d'%self.end if self.end else '',
482482
':%d'%self.step if self.step else '')
483483

484+
def __repr__(self):
485+
return '%s(start=%r,end=%r,step=%r)' % (self.__class__.__name__, self.start, self.end, self.step)
486+
484487
def __eq__(self, other):
485488
return isinstance(other, Slice) and other.start == self.start and self.end == other.end and other.step == self.step

tests/test_jsonpath.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ def test_slice_value(self):
118118
('[1:]', [1, 2, 3, 4], [2, 3, 4]),
119119
('[:2]', [1, 2, 3, 4], [1, 2])])
120120

121+
# Funky slice hacks
122+
self.check_cases([
123+
('[*]', 1, [1]), # This is a funky hack
124+
('[0:]', 1, [1]), # This is a funky hack
125+
('[*]', {'foo':1}, [{'foo': 1}]), # This is a funky hack
126+
('[*].foo', {'foo':1}, [1]), # This is a funky hack
127+
])
128+
121129
def test_child_value(self):
122130
self.check_cases([('foo.baz', {'foo': {'baz': 3}}, [3]),
123131
('foo.baz', {'foo': {'baz': [3]}}, [[3]]),

0 commit comments

Comments
 (0)
0