Skip to content

Commit 3a95cd9

Browse files
committed
chore: change istep to i
1 parent 4ba2f6e commit 3a95cd9

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

jsonpath/__init__.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author : zhangxianbing1
33
Date : 2020-12-27 09:22:14
44
LastEditors : zhangxianbing1
5-
LastEditTime : 2020-12-31 18:14:16
5+
LastEditTime : 2021-01-04 09:59:44
66
Description : JSONPath
77
"""
88
__version__ = "1.0.0"
@@ -153,64 +153,64 @@ def _filter(self, obj, istep: int, step):
153153
if r:
154154
self._trace(obj, istep)
155155

156-
def _trace(self, obj, istep: int):
156+
def _trace(self, obj, i: int):
157157
"""Perform operation on object.
158158
159159
Args:
160160
obj ([type]): current operating object
161-
istep (int): current operation specified by index in self.steps
161+
i (int): current operation specified by index in self.steps
162162
"""
163163

164164
# store
165-
if istep >= self.lpath:
165+
if i >= self.lpath:
166166
self.result.append(obj)
167167
print(obj)
168168
return
169169

170-
step = self.steps[istep]
170+
step = self.steps[i]
171171

172172
# wildcard
173173
if step == "*":
174-
self._traverse(self._trace, obj, istep + 1)
174+
self._traverse(self._trace, obj, i + 1)
175175
return
176176

177177
# recursive descent
178178
if step == "..":
179-
self._trace(obj, istep + 1)
180-
self._traverse(self._trace, obj, istep)
179+
self._trace(obj, i + 1)
180+
self._traverse(self._trace, obj, i)
181181
return
182182

183183
# get value from list
184184
if isinstance(obj, list) and step.isdigit():
185185
ikey = int(step)
186186
if ikey < len(obj):
187-
self._trace(obj[ikey], istep + 1)
187+
self._trace(obj[ikey], i + 1)
188188
return
189189

190190
# get value from dict
191191
if isinstance(obj, dict) and step in obj:
192-
self._trace(obj[step], istep + 1)
192+
self._trace(obj[step], i + 1)
193193
return
194194

195195
# slice
196196
if isinstance(obj, list) and REP_SLICE_CONTENT.fullmatch(step):
197197
vals = eval(f"obj[{step}]")
198198
for v in vals:
199-
self._trace(v, istep + 1)
199+
self._trace(v, i + 1)
200200
return
201201

202202
# select
203203
if isinstance(obj, dict) and REP_SELECT_CONTENT.fullmatch(step):
204204
for k in step.split(","):
205205
if k in obj:
206-
self._trace(obj[k], istep + 1)
206+
self._trace(obj[k], i + 1)
207207
return
208208

209209
# filter
210210
if step.startswith("?(") and step.endswith(")"):
211211
step = step[2:-1]
212212
step = REP_FILTER_CONTENT.sub(self._f_brackets, step)
213-
self._traverse(self._filter, obj, istep + 1, step)
213+
self._traverse(self._filter, obj, i + 1, step)
214214
return
215215

216216
# sort
@@ -225,16 +225,11 @@ def _trace(self, obj, istep: int):
225225
else:
226226
obj.sort(key=lambda t, k=sortby: _getattr(t, k))
227227

228-
self._traverse(self._trace, obj, istep + 1)
228+
self._traverse(self._trace, obj, i + 1)
229229
return
230230

231231

232232
if __name__ == "__main__":
233-
# JSONPath("$.a.'b.c'.'d e'.[f,g][h][*][j.k][l m][2:4]..d", result_type="FIELD")
234233
with open("test/data/2.json", "rb") as f:
235234
d = json.load(f)
236-
# JSONPath(
237-
# '$.book[?(@.title=="Herman Melville" or @.title=="Evelyn Waugh")].title'
238-
# ).parse(d)
239-
# JSONPath("$..price").parse(d)
240235
JSONPath("$.book[/(price)].price").parse(d)

test/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
"bicycle": {"color": "red", "price": 19.95},
4141
}
4242

43-
prices = [8.95, 12.99, 8.99, 22.99, 19.95]
44-
4543

4644
@pytest.fixture(
4745
params=[
@@ -50,7 +48,7 @@
5048
TestCase("$[book]", data, [data["book"]]),
5149
TestCase("$.'a.b c'", data, [data["a.b c"]]),
5250
TestCase("$['a.b c']", data, [data["a.b c"]]),
53-
TestCase("$..price", data, prices),
51+
TestCase("$..price", data, [8.95, 12.99, 8.99, 22.99, 19.95]),
5452
TestCase("$.book[1:3]", data, data["book"][1:3]),
5553
TestCase("$.book[1:-1]", data, data["book"][1:-1]),
5654
TestCase("$.book[0:-1:2]", data, data["book"][0:-1:2]),

0 commit comments

Comments
 (0)