Skip to content

Commit abc73d5

Browse files
author
Laurent PINSON
committed
added step in slice
1 parent 5f7e5ae commit abc73d5

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

jsonpath_ng/parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ def p_slice_any(self, p):
176176
p[0] = Slice()
177177

178178
def p_slice(self, p): # Currently does not support `step`
179-
"slice : maybe_int ':' maybe_int"
180-
p[0] = Slice(start=p[1], end=p[3])
179+
"""slice : maybe_int ':' maybe_int
180+
| maybe_int ':' maybe_int ':' maybe_int """
181+
p[0] = Slice(*p[1::2])
181182

182183
def p_maybe_int(self, p):
183184
"""maybe_int : NUMBER

tests/test_jsonpath.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ def test_slice_value(self):
140140
self.check_cases([('[*]', [1, 2, 3], [1, 2, 3]),
141141
('[*]', xrange(1, 4), [1, 2, 3]),
142142
('[1:]', [1, 2, 3, 4], [2, 3, 4]),
143-
('[:2]', [1, 2, 3, 4], [1, 2])
143+
('[:2]', [1, 2, 3, 4], [1, 2]),
144+
('[:3:2]', [1, 2, 3, 4], [1, 3]),
145+
('[1::2]', [1, 2, 3, 4], [2, 4]),
146+
('[1:5:3]', [1, 2, 3, 4, 5], [2, 5]),
147+
('[::-2]', [1, 2, 3, 4, 5], [5, 3, 1]),
144148
])
145149

146150
# Funky slice hacks
@@ -226,7 +230,11 @@ def test_index_paths(self):
226230

227231
def test_slice_paths(self):
228232
self.check_paths([ ('[*]', [1, 2, 3], ['[0]', '[1]', '[2]']),
229-
('[1:]', [1, 2, 3, 4], ['[1]', '[2]', '[3]'])
233+
('[1:]', [1, 2, 3, 4], ['[1]', '[2]', '[3]']),
234+
('[1:3]', [1, 2, 3, 4], ['[1]', '[2]']),
235+
('[1::2]', [1, 2, 3, 4], ['[1]', '[3]']),
236+
('[::-1]', [1, 2, 3], ['[2]', '[1]', '[0]']),
237+
('[1:6:3]', xrange(10), ['[1]', '[4]']),
230238
])
231239

232240
def test_child_paths(self):

0 commit comments

Comments
 (0)