Skip to content

Commit

Permalink
DOC-3787 add missing code samples for JSON (#3229)
Browse files Browse the repository at this point in the history
Python examples for the JSON datatype page
(https://redis.io/docs/latest/develop/data-types/json/) are available in the repo but
haven't been added to the page yet. While adding them, I discovered that the Python
samples don't match the CLI samples in a few places, so I've updated them.
  • Loading branch information
andy-stark-redis authored May 15, 2024
1 parent d93f533 commit 35e4850
Showing 1 changed file with 115 additions and 26 deletions.
141 changes: 115 additions & 26 deletions doctests/dt_json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# EXAMPLE: json_tutorial
# HIDE_START
"""
Code samples for JSON doc pages: https://redis.io/docs/latest/develop/data-types/json/
"""
import redis

r = redis.Redis(decode_responses=True)
Expand Down Expand Up @@ -139,23 +142,37 @@
{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there\u2019s room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.",
"description": "This is a mid-travel trail slayer that is a fantastic "
"daily driver or one bike quiver. The Shimano Claris 8-speed groupset "
"gives plenty of gear range to tackle hills and there\u2019s room for "
"mudguards and a rack too. This is the bike for the rider who wants "
"trail manners with low fuss ownership.",
"price": 1920,
"specs": {"material": "carbon", "weight": 13.1},
"colors": ["black", "silver"],
},
{
"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.",
"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"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.",
"description": "This bike gives kids aged six years and older "
"a durable and uberlight mountain bike for their first experience "
"on tracks and easy cruising through forests and fields. A set of "
"powerful Shimano hydraulic disc brakes provide ample stopping "
"ability. If you're after a budget option, this is one of the best "
"bikes you could get.",
"price": 3264,
"specs": {"material": "alloy", "weight": 13.8},
},
Expand All @@ -164,15 +181,26 @@
{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano\u2019s, this is a bike which doesn\u2019t break the bank and delivers craved performance. It\u2019s for the rider who wants both efficiency and capability.",
"description": "This bike is a great option for anyone who just "
"wants a bike to get about on With a slick-shifting Claris gears "
"from Shimano\u2019s, this is a bike which doesn\u2019t break the "
"bank and delivers craved performance. It\u2019s for the rider "
"who wants both efficiency and capability.",
"price": 1475,
"specs": {"material": "aluminium", "weight": 16.6},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.",
"description": "A real joy to ride, this bike got very high "
"scores in last years Bike of the year report. The carefully "
"crafted 50-34 tooth chainset and 11-32 tooth cassette give an "
"easy-on-the-legs bottom gear for climbing, and the high-quality "
"Vittoria Zaffiro tires give balance and grip.It includes "
"a low-step frame , our memory foam seat, bump-resistant shocks and "
"conveniently placed thumb throttle. Put it all together and you "
"get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {"material": "alloy", "weight": 11.6},
},
Expand All @@ -187,9 +215,9 @@

# STEP_START get_bikes
res2 = r.json().get("bikes:inventory", "$.inventory.*")
print(
res2
) # >>> [[{'id': 'bike:1', 'model': 'Phoebe', 'description': 'This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there’s room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.', 'price': 1920, 'specs': {'material': 'carbon', 'weight': 13.1}, 'colors': ['black', 'silver']}, {'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']}, {'id': 'bike:3', 'model': 'Weywot', 'description': "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.", 'price': 3264, 'specs': {'material': 'alloy', 'weight': 13.8}}], [{'id': 'bike:4', 'model': 'Salacia', 'description': 'This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance. It’s for the rider who wants both efficiency and capability.', 'price': 1475, 'specs': {'material': 'aluminium', 'weight': 16.6}, 'colors': ['black', 'silver']}, {'id': 'bike:5', 'model': 'Mimas', 'description': 'A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.', 'price': 3941, 'specs': {'material': 'alloy', 'weight': 11.6}}]]
print(res2)
# >>> [[{'id': 'bike:1', 'model': 'Phoebe',
# >>> 'description': 'This is a mid-travel trail slayer...
# STEP_END

# STEP_START get_mtnbikes
Expand Down Expand Up @@ -227,21 +255,46 @@
assert res7 == ["Phoebe", "Quaoar"]
# REMOVE_END

# STEP_START filters
# STEP_START filter1
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(
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']}]
print(res8)
# >>> [{'id': 'bike:2', 'model': 'Quaoar',
# 'description': "Redesigned for the 2020 model year...
# STEP_END

# REMOVE_START
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"],
}
]
# REMOVE_END

# STEP_START filter2
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> [['Weywot', 'Mimas']]
print(res9) # >>> ['Weywot', 'Mimas']
# STEP_END

# REMOVE_START
assert res9 == ["Weywot", "Mimas"]
# REMOVE_END

# STEP_START filter3
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
Expand All @@ -252,35 +305,71 @@
assert res10 == ["Quaoar", "Weywot", "Salacia", "Mimas"]
# REMOVE_END

# STEP_START filter4
res11 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al"
)
res12 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al"
)
res13 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al"
)

res14 = r.json().get(
"bikes:inventory",
"$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
)
print(res14) # >>> ['Quaoar', 'Weywot']
# STEP_END

# REMOVE_START
assert res14 == ["Quaoar", "Weywot"]
# REMOVE_END

# STEP_START update_bikes
res11 = r.json().get("bikes:inventory", "$..price")
print(res11) # >>> [1920, 2072, 3264, 1475, 3941]
res15 = r.json().get("bikes:inventory", "$..price")
print(res15) # >>> [1920, 2072, 3264, 1475, 3941]

res16 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res16) # >>> [1820, 1972, 3164, 1375, 3841]

res12 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res12) # >>> [1820, 1972, 3164, 1375, 3841]
res17 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res17) # >>> [1920, 2072, 3264, 1475, 3941]
# STEP_END

res13 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res13) # >>> [1920, 2072, 3264, 1475, 3941]
# REMOVE_START
assert res15 == [1920, 2072, 3264, 1475, 3941]
assert res16 == [1820, 1972, 3164, 1375, 3841]
assert res17 == [1920, 2072, 3264, 1475, 3941]
# REMOVE_END

# STEP_START update_filters1
res18 = r.json().set(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500
)
res19 = r.json().get("bikes:inventory", "$..price")
print(res19) # >>> [1500, 2072, 3264, 1500, 3941]
# STEP_END

# REMOVE_START
assert res12 == [1820, 1972, 3164, 1375, 3841]
assert res19 == [1500, 2072, 3264, 1500, 3941]
# REMOVE_END

# STEP_START update_filters
res14 = r.json().arrappend(
# STEP_START update_filters2
res20 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
print(res14) # >>> [3, 3]
print(res20) # >>> [3, 3]

res15 = r.json().get("bikes:inventory", "$..[*].colors")
res21 = r.json().get("bikes:inventory", "$..[*].colors")
print(
res15
res21
) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
# STEP_END

# REMOVE_START
assert res15 == [
assert res21 == [
["black", "silver", "pink"],
["black", "white"],
["black", "silver", "pink"],
Expand Down

0 comments on commit 35e4850

Please sign in to comment.