Skip to content

Commit 41a91d2

Browse files
DOC-3787 add missing code samples for JSON
1 parent d93f533 commit 41a91d2

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

doctests/dt_json.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,33 @@
227227
assert res7 == ["Phoebe", "Quaoar"]
228228
# REMOVE_END
229229

230-
# STEP_START filters
230+
# STEP_START filter1
231231
res8 = r.json().get(
232232
"bikes:inventory",
233233
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
234234
)
235235
print(
236236
res8
237237
) # >>> [{'id': 'bike:2', 'model': 'Quaoar', 'description': "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.", 'price': 2072, 'specs': {'material': 'aluminium', 'weight': 7.9}, 'colors': ['black', 'white']}]
238+
# STEP_END
238239

240+
# REMOVE_START
241+
assert res8 == [{'id': 'bike:2', 'model': 'Quaoar', 'description': "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.", 'price': 2072, 'specs': {'material': 'aluminium', 'weight': 7.9}, 'colors': ['black', 'white']}]
242+
# REMOVE_END
243+
244+
# STEP_START filter2
239245
# names of bikes made from an alloy
240246
res9 = r.json().get(
241247
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
242248
)
243-
print(res9) # >>> [['Weywot', 'Mimas']]
249+
print(res9) # >>> ['Weywot', 'Mimas']
250+
# STEP_END
244251

252+
# REMOVE_START
253+
assert res9 == ['Weywot', 'Mimas']
254+
# REMOVE_END
255+
256+
# STEP_START filter3
245257
res10 = r.json().get(
246258
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
247259
)
@@ -252,6 +264,19 @@
252264
assert res10 == ["Quaoar", "Weywot", "Salacia", "Mimas"]
253265
# REMOVE_END
254266

267+
# STEP_START filter4
268+
res11 = r.json().set("bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al")
269+
res12 = r.json().set("bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al")
270+
res13 = r.json().set("bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al")
271+
272+
res14 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model")
273+
print(res14) # >>> ['Quaoar', 'Weywot']
274+
# STEP_END
275+
276+
# REMOVE_START
277+
assert res14 == ['Quaoar', 'Weywot']
278+
# REMOVE_END
279+
255280
# STEP_START update_bikes
256281
res11 = r.json().get("bikes:inventory", "$..price")
257282
print(res11) # >>> [1920, 2072, 3264, 1475, 3941]
@@ -267,7 +292,17 @@
267292
assert res12 == [1820, 1972, 3164, 1375, 3841]
268293
# REMOVE_END
269294

270-
# STEP_START update_filters
295+
# STEP_START update_filters1
296+
res16 = r.json().set("bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500)
297+
res17 = r.json().get("bikes:inventory", "$..price")
298+
print(res17) # >>> [1500, 2072, 3264, 1500, 3941]
299+
# STEP_END
300+
301+
# REMOVE_START
302+
assert res17 == [1500, 2072, 3264, 1500, 3941]
303+
# REMOVE_END
304+
305+
# STEP_START update_filters2
271306
res14 = r.json().arrappend(
272307
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
273308
)

0 commit comments

Comments
 (0)