From 23cb39ef9329fe495cd103c52b3d83640d600a10 Mon Sep 17 00:00:00 2001 From: Savannah Date: Thu, 12 Oct 2023 13:31:38 -0400 Subject: [PATCH 1/3] Create dt_set.py creating the python code for the tabbed example on the set data type page --- doctests/dt_set.py | 171 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 doctests/dt_set.py diff --git a/doctests/dt_set.py b/doctests/dt_set.py new file mode 100644 index 0000000000..1a8d9638b0 --- /dev/null +++ b/doctests/dt_set.py @@ -0,0 +1,171 @@ +# EXAMPLE: sets_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END +# REMOVE_START +r.delete("bikes:racing:france") +r.delete("bikes:racing:usa") +# REMOVE_END + +# STEP_START sadd +res1 = r.sadd("bikes:racing:france", "bike:1") +print(res1) # >>> 1 + +res2 = r.sadd("bikes:racing:france", "bike:1") +print(res2) # >>> 0 + +res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3") +print(res3) # >>> 2 + +res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4") +print(res4) # >>> 2 +# STEP_END + +# REMOVE_START +assert res1 == 1 +assert res2 == 0 +assert res3 == 2 +assert res4 == 2 +# REMOVE_END + +# STEP_START sismember +# HIDE_START +r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") +r.sadd("bikes:racing:usa", "bike:1", "bike:4") +# HIDE_END +res5 = r.sismember("bikes:racing:usa", "bike:1") +print(res5) # >>> True + +res6 = r.sismember("bikes:racing:usa", "bike:2") +print(res6) # >>> False +# STEP_END + +# REMOVE_START +assert res5 is True +assert res6 is False +# REMOVE_END + +# STEP_START sinter +# HIDE_START +r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") +r.sadd("bikes:racing:usa", "bike:1", "bike:4") +# HIDE_END +res7 = r.sinter("bikes:racing:france", "bikes:racing:usa") +print(res7) # >>> {'bike:1'} +# STEP_END + +# REMOVE_START +assert res7 == {"bike:1"} +# REMOVE_END + +# STEP_START scard +# HIDE_START +r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") +# HIDE_END +res8 = r.scard("bikes:racing:france") +print(res8) # >>> 3 +# STEP_END + +# REMOVE_START +assert res8 == 3 +r.delete("bikes:racing:france") +# REMOVE_END + +# STEP_START sadd_smembers +res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") +print(res9) # >>> 3 + +res10 = r.smembers("bikes:racing:france") +print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'} +# STEP_END + +# REMOVE_START +assert res9 == 3 +assert res10 == {"bike:1", "bike:2", "bike:3"} +# REMOVE_END + +# STEP_START smismember +res11 = r.sismember("bikes:racing:france", "bike:1") +print(res11) # >>> True + +res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4") +print(res12) # >>> [True, True, False] +# STEP_END + +# REMOVE_START +assert res11 is True +assert res12 == [True, True, False] +# REMOVE_END + +# STEP_START sdiff +r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") +r.sadd("bikes:racing:usa", "bike:1", "bike:4") + +res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa") +print(res13) # >>> {'bike:2', 'bike:3'} +# STEP_END + +# REMOVE_START +assert res13 == {"bike:2", "bike:3"} +r.delete("bikes:racing:france") +r.delete("bikes:racing:usa") +# REMOVE_END + +# STEP_START multisets +r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") +r.sadd("bikes:racing:usa", "bike:1", "bike:4") +r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4") + +res13 = r.sinter( + "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" +) +print(res13) # >>> {'bike:1'} + +res14 = r.sunion( + "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" +) +print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'} + +res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy") +print(res15) # >>> set() + +res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france") +print(res16) # >>> {'bike:4'} + +res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa") +print(res17) # >>> {'bike:2', 'bike:3'} +# STEP_END + +# REMOVE_START +assert res13 == {"bike:1"} +assert res14 == {"bike:1", "bike:2", "bike:3", "bike:4"} +assert res15 == set() +assert res16 == {"bike:4"} +assert res17 == {"bike:2", "bike:3"} +r.delete("bikes:racing:france") +r.delete("bikes:racing:usa") +r.delete("bikes:racing:italy") +# REMOVE_END + +# STEP_START srem +r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") + +res18 = r.srem("bikes:racing:france", "bike:1") +print(res18) # >>> 1 + +res19 = r.spop("bikes:racing:france") +print(res19) # >>> bike:3 + +res20 = r.smembers("bikes:racing:france") +print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'} + +res21 = r.srandmember("bikes:racing:france") +print(res21) # >>> bike:4 +# STEP_END + +# REMOVE_START +assert res18 == 1 +# none of the other results are deterministic +# REMOVE_END From 3445dc3cf6058d23631ffd834e5cfc34363e2413 Mon Sep 17 00:00:00 2001 From: Savannah Date: Thu, 12 Oct 2023 13:42:53 -0400 Subject: [PATCH 2/3] add the data type python code for tabbed examples each data type page on redis.io is getting tabbed examples in the fully supported client languages - this is the code for the python examples for each data type, minus a few that were already merged in --- doctests/dt_bitfield.py | 30 ++++ doctests/dt_bitmap.py | 36 +++++ doctests/dt_bloom.py | 37 +++++ doctests/dt_cms.py | 28 ++++ doctests/dt_cuckoo.py | 32 +++++ doctests/dt_hll.py | 30 ++++ doctests/dt_json.py | 288 +++++++++++++++++++++++++++++++++++++++ doctests/dt_ss.py | 115 ++++++++++++++++ doctests/dt_stream.py | 294 ++++++++++++++++++++++++++++++++++++++++ doctests/dt_tdigest.py | 76 +++++++++++ doctests/dt_topk.py | 33 +++++ 11 files changed, 999 insertions(+) create mode 100644 doctests/dt_bitfield.py create mode 100644 doctests/dt_bitmap.py create mode 100644 doctests/dt_bloom.py create mode 100644 doctests/dt_cms.py create mode 100644 doctests/dt_cuckoo.py create mode 100644 doctests/dt_hll.py create mode 100644 doctests/dt_json.py create mode 100644 doctests/dt_ss.py create mode 100644 doctests/dt_stream.py create mode 100644 doctests/dt_tdigest.py create mode 100644 doctests/dt_topk.py diff --git a/doctests/dt_bitfield.py b/doctests/dt_bitfield.py new file mode 100644 index 0000000000..6cd1a3916f --- /dev/null +++ b/doctests/dt_bitfield.py @@ -0,0 +1,30 @@ +# EXAMPLE: bitfield_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("bike:1:stats") +# REMOVE_END + +# STEP_START bf +bf = r.bitfield("bike:1:stats") +res1 = bf.set("u32", "#0", 1000).execute() +print(res1) # >>> [0] + +res2 = bf.incrby("u32", "#0", -50).incrby("u32", "#1", 1).execute() +print(res2) # >>> [950, 1] + +res3 = bf.incrby("u32", "#0", 500).incrby("u32", "#1", 1).execute() +print(res3) # >>> [1450, 2] + +res4 = bf.get("u32", "#0").get("u32", "#1").execute() +print(res4) # >>> [1450, 2] +# STEP_END + +# REMOVE_START +assert res1 == [0] +assert res4 == [1450, 2] +# REMOVE_END diff --git a/doctests/dt_bitmap.py b/doctests/dt_bitmap.py new file mode 100644 index 0000000000..ebdb345884 --- /dev/null +++ b/doctests/dt_bitmap.py @@ -0,0 +1,36 @@ +# EXAMPLE: bitmap_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("pings:2024-01-01-00:00") +# REMOVE_END + +# STEP_START ping +res1 = r.setbit("pings:2024-01-01-00:00", 123, 1) +print(res1) # >>> 0 + +res2 = r.getbit("pings:2024-01-01-00:00", 123) +print(res2) # >>> 1 + +res3 = r.getbit("pings:2024-01-01-00:00", 456) +print(res3) # >>> 0 +# STEP_END + +# REMOVE_START +assert res1 == 0 +# REMOVE_END + +# STEP_START bitcount +# HIDE_START +r.setbit("pings:2024-01-01-00:00", 123, 1) +# HIDE_END +res4 = r.bitcount("pings:2024-01-01-00:00") +print(res4) # >>> 1 +# STEP_END +# REMOVE_START +assert res4 == 1 +# REMOVE_END diff --git a/doctests/dt_bloom.py b/doctests/dt_bloom.py new file mode 100644 index 0000000000..03dc9c9138 --- /dev/null +++ b/doctests/dt_bloom.py @@ -0,0 +1,37 @@ +# EXAMPLE: bf_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# STEP_START bloom +res1 = r.bf().reserve("bikes:models", 0.01, 1000) +print(res1) # >>> True + +res2 = r.bf().add("bikes:models", "Smoky Mountain Striker") +print(res2) # >>> True + +res3 = r.bf().exists("bikes:models", "Smoky Mountain Striker") +print(res3) # >>> True + +res4 = r.bf().madd( + "bikes:models", + "Rocky Mountain Racer", + "Cloudy City Cruiser", + "Windy City Wippet", +) +print(res4) # >>> [True, True, True] + +res5 = r.bf().mexists( + "bikes:models", + "Rocky Mountain Racer", + "Cloudy City Cruiser", + "Windy City Wippet", +) +print(res5) # >>> [True, True, True] +# STEP_END + +# REMOVE_START +assert res1 is True +# REMOVE_END diff --git a/doctests/dt_cms.py b/doctests/dt_cms.py new file mode 100644 index 0000000000..6bcdc0eab2 --- /dev/null +++ b/doctests/dt_cms.py @@ -0,0 +1,28 @@ +# EXAMPLE: cms_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END +# REMOVE_START +r.delete("bikes:profit") +# REMOVE_END + +# STEP_START cms +res1 = r.cms().initbyprob("bikes:profit", 0.001, 0.002) +print(res1) # >>> True + +res2 = r.cms().incrby("bikes:profit", ["Smoky Mountain Striker"], [100]) +print(res2) # >>> [100] + +res3 = r.cms().incrby( + "bikes:profit", ["Rocky Mountain Racer", "Cloudy City Cruiser"], [200, 150] +) +print(res3) # >>> [200, 150] + +res4 = r.cms().query("bikes:profit", "Smoky Mountain Striker") +print(res4) # >>> [100] + +res5 = r.cms().info("bikes:profit") +print(res5.width, res5.depth, res5.count) # >>> 2000 9 450 +# STEP_END diff --git a/doctests/dt_cuckoo.py b/doctests/dt_cuckoo.py new file mode 100644 index 0000000000..2157178468 --- /dev/null +++ b/doctests/dt_cuckoo.py @@ -0,0 +1,32 @@ +# EXAMPLE: cuckoo_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("bikes:models") +# REMOVE_END + +# STEP_START cuckoo +res1 = r.cf().reserve("bikes:models", 1000000) +print(res1) # >>> True + +res2 = r.cf().add("bikes:models", "Smoky Mountain Striker") +print(res2) # >>> 1 + +res3 = r.cf().exists("bikes:models", "Smoky Mountain Striker") +print(res3) # >>> 1 + +res4 = r.cf().exists("bikes:models", "Terrible Bike Name") +print(res4) # >>> 0 + +res5 = r.cf().delete("bikes:models", "Smoky Mountain Striker") +print(res5) # >>> 1 +# STEP_END + +# REMOVE_START +assert res1 is True +assert res5 == 1 +# REMOVE_END diff --git a/doctests/dt_hll.py b/doctests/dt_hll.py new file mode 100644 index 0000000000..aedf8540fa --- /dev/null +++ b/doctests/dt_hll.py @@ -0,0 +1,30 @@ +import redis + +# HIDE_START +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("bikes", "commuter_bikes", "all_bikes") +# REMOVE_END + +# STEP_START pfadd +res1 = r.pfadd("bikes", "Hyperion", "Deimos", "Phoebe", "Quaoar") +print(res1) # >>> 1 + +res2 = r.pfcount("bikes") +print(res2) # >>> 4 + +res3 = r.pfadd("commuter_bikes", "Salacia", "Mimas", "Quaoar") +print(res3) # >>> 1 + +res4 = r.pfmerge("all_bikes", "bikes", "commuter_bikes") +print(res4) # >>> True + +res5 = r.pfcount("all_bikes") +print(res5) # >>> 6 +# STEP_END + +# REMOVE_START +assert res4 == True +# REMOVE_END diff --git a/doctests/dt_json.py b/doctests/dt_json.py new file mode 100644 index 0000000000..5f17a00c3d --- /dev/null +++ b/doctests/dt_json.py @@ -0,0 +1,288 @@ +# EXAMPLE: json_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("bike") +r.delete("crashes") +r.delete("newbike") +r.delete("rider") +# REMOVE_END + +# STEP_START set_get +res1 = r.json().set("bike", "$", '"Hyperion"') +print(res1) # >>> True + +res2 = r.json().get("bike", "$") +print(res2) # >>> ['"Hyperion"'] + +res3 = r.json().type("bike", "$") +print(res3) # >>> ['string'] +# STEP_END + +# REMOVE_START +assert res2 == ['"Hyperion"'] +# REMOVE_END + +# STEP_START str +res4 = r.json().strlen("bike", "$") +print(res4) # >>> [10] + +res5 = r.json().strappend("bike", '" (Enduro bikes)"') +print(res5) # >>> 27 + +res6 = r.json().get("bike", "$") +print(res6) # >>> ['"Hyperion"" (Enduro bikes)"'] +# STEP_END + +# REMOVE_START +assert res6 == ['"Hyperion"" (Enduro bikes)"'] +# REMOVE_END + +# STEP_START num +res7 = r.json().set("crashes", "$", 0) +print(res7) # >>> True + +res8 = r.json().numincrby("crashes", "$", 1) +print(res8) # >>> [1] + +res9 = r.json().numincrby("crashes", "$", 1.5) +print(res9) # >>> [2.5] + +res10 = r.json().numincrby("crashes", "$", -0.75) +print(res10) # >>> [1.75] +# STEP_END + +# REMOVE_START +assert res10 == [1.75] +# REMOVE_END + +# STEP_START arr +res11 = r.json().set("newbike", "$", ["Deimos", {"crashes": 0}, None]) +print(res11) # >>> True + +res12 = r.json().get("newbike", "$") +print(res12) # >>> ['["Deimos", { "crashes": 0 }, null]'] + +res13 = r.json().get("newbike", "$[1].crashes") +print(res13) # >>> ['0'] + +res14 = r.json().delete("newbike", "$.[-1]") +print(res14) # >>> [1] + +res15 = r.json().get("newbike", "$") +print(res15) # >>> [['Deimos', {'crashes': 0}]] +# STEP_END + +# REMOVE_START +assert res15 == [["Deimos", {"crashes": 0}]] +# REMOVE_END + +# STEP_START arr2 +res16 = r.json().set("riders", "$", []) +print(res16) # >>> True + +res17 = r.json().arrappend("riders", "$", "Norem") +print(res17) # >>> [1] + +res18 = r.json().get("riders", "$") +print(res18) # >>> [['Norem']] + +res19 = r.json().arrinsert("riders", "$", 1, "Prickett", "Royce", "Castilla") +print(res19) # >>> [4] + +res20 = r.json().get("riders", "$") +print(res20) # >>> [['Norem', 'Prickett', 'Royce', 'Castilla']] + +res21 = r.json().arrtrim("riders", "$", 1, 1) +print(res21) # >>> [1] + +res22 = r.json().get("riders", "$") +print(res22) # >>> [['Prickett']] + +res23 = r.json().arrpop("riders", "$") +print(res23) # >>> ['"Prickett"'] + +res24 = r.json().arrpop("riders", "$") +print(res24) # >>> [None] +# STEP_END + +# REMOVE_START +assert res24 == [None] +# REMOVE_END + +# STEP_START obj +res25 = r.json().set( + "bike:1", "$", {"model": "Deimos", "brand": "Ergonom", "price": 4972} +) +print(res25) # >>> True + +res26 = r.json().objlen("bike:1", "$") +print(res26) # >>> [3] + +res27 = r.json().objkeys("bike:1", "$") +print(res27) # >>> [['model', 'brand', 'price']] +# STEP_END + +# REMOVE_START +assert res27 == [["model", "brand", "price"]] +# REMOVE_END + +# STEP_START set_bikes +# HIDE_START +inventory_json = { + "inventory": { + "mountain_bikes": [ + { + "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.", + "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}, + }, + ], + "commuter_bikes": [ + { + "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.", + "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}, + }, + ], + } +} +# HIDE_END + +res1 = r.json().set("bikes:inventory", "$", inventory_json) +print(res1) # >>> True +# STEP_END + +# 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}}]] +# STEP_END + +# STEP_START get_mtnbikes +res3 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[*].model") +print(res3) # >>> [['Phoebe', 'Quaoar', 'Weywot']] + +res4 = r.json().get("bikes:inventory", '$.inventory["mountain_bikes"][*].model') +print(res4) # >>> [['Phoebe', 'Quaoar', 'Weywot']] + +res5 = r.json().get("bikes:inventory", "$..mountain_bikes[*].model") +print(res5) # >>> [['Phoebe', 'Quaoar', 'Weywot']] +# STEP_END + +# REMOVE_START +assert res3 == ["Phoebe", "Quaoar", "Weywot"] +assert res4 == ["Phoebe", "Quaoar", "Weywot"] +assert res5 == ["Phoebe", "Quaoar", "Weywot"] +# REMOVE_END + +# STEP_START get_models +res6 = r.json().get("bikes:inventory", "$..model") +print(res6) # >>> [['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']] +# STEP_END + +# REMOVE_START +assert res6 == ["Phoebe", "Quaoar", "Weywot", "Salacia", "Mimas"] +# REMOVE_END + +# STEP_START get2mtnbikes +res7 = r.json().get("bikes:inventory", "$..mountain_bikes[0:2].model") +print(res7) # >>> [['Phoebe', 'Quaoar']] +# STEP_END + +# REMOVE_START +assert res7 == ["Phoebe", "Quaoar"] +# REMOVE_END + +# STEP_START filters +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']}] + +# names of bikes made from an alloy +res9 = r.json().get( + "bikes:inventory", "$..[?(@.specs.material == 'alloy')].model" +) +print(res9) # >>> [['Weywot', 'Mimas']] + +res10 = r.json().get( + "bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model" +) +print(res10) # >>> ['Quaoar', 'Weywot', 'Salacia', 'Mimas'] +# STEP_END + +# REMOVE_START +assert res10 == ["Quaoar", "Weywot", "Salacia", "Mimas"] +# REMOVE_END + +# STEP_START update_bikes +res11 = r.json().get("bikes:inventory", "$..price") +print(res11) # >>> [1920, 2072, 3264, 1475, 3941] + +res12 = r.json().numincrby("bikes:inventory", "$..price", -100) +print(res12) # >>> [1820, 1972, 3164, 1375, 3841] + +res13 = r.json().numincrby("bikes:inventory", "$..price", 100) +print(res13) # >>> [1920, 2072, 3264, 1475, 3941] +# STEP_END + +# REMOVE_START +assert res12 == [1820, 1972, 3164, 1375, 3841] +# REMOVE_END + +# STEP_START update_filters +res14 = r.json().arrappend( + "bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink" +) +print(res14) # >>> [3, 3] + +res15 = r.json().get("bikes:inventory", "$..[*].colors") +print( + res15 +) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']] +# STEP_END + +# REMOVE_START +assert res15 == [ + ["black", "silver", "pink"], + ["black", "white"], + ["black", "silver", "pink"], +] +# REMOVE_END diff --git a/doctests/dt_ss.py b/doctests/dt_ss.py new file mode 100644 index 0000000000..a609fec543 --- /dev/null +++ b/doctests/dt_ss.py @@ -0,0 +1,115 @@ +# EXAMPLE: ss_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("racer_scores") +# REMOVE_END + +# STEP_START zadd +res1 = r.zadd("racer_scores", {"Norem": 10}) +print(res1) # >>> 1 + +res2 = r.zadd("racer_scores", {"Castilla": 12}) +print(res2) # >>> 1 + +res3 = r.zadd( + "racer_scores", + {"Sam-Bodden": 8, "Royce": 10, "Ford": 6, "Prickett": 14, "Castilla": 12}, +) +print(res3) # >>> 4 +# STEP_END + +# REMOVE_START +assert r.zcard("racer_scores") == 6 +# REMOVE_END + +# STEP_START zrange +res4 = r.zrange("racer_scores", 0, -1) +print( + res4 +) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett'] + +res5 = r.zrevrange("racer_scores", 0, -1) +print( + res5 +) # >>> ['Prickett', 'Castilla', 'Royce', 'Norem', 'Sam-Bodden', 'Ford'] +# STEP_END + +# STEP_START zrange_withscores +res6 = r.zrange("racer_scores", 0, -1, withscores=True) +print( + res6 +) # >>> [('Ford', 6.0), ('Sam-Bodden', 8.0), ('Norem', 10.0), ('Royce', 10.0), ('Castilla', 12.0), ('Prickett', 14.0)] +# STEP_END + +# STEP_START zrangebyscore +res7 = r.zrangebyscore("racer_scores", "-inf", 10) +print(res7) # >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce'] +# STEP_END + +# STEP_START zremrangebyscore +res8 = r.zrem("racer_scores", "Castilla") +print(res8) # >>> 1 + +res9 = r.zremrangebyscore("racer_scores", "-inf", 9) +print(res9) # >>> 2 + +res10 = r.zrange("racer_scores", 0, -1) +print(res10) # >>> ['Norem', 'Royce', 'Prickett'] +# STEP_END + +# REMOVE_START +assert r.zcard("racer_scores") == 3 +# REMOVE_END + +# STEP_START zrank +res11 = r.zrank("racer_scores", "Norem") +print(res11) # >>> 0 + +res12 = r.zrevrank("racer_scores", "Norem") +print(res12) # >>> 2 +# STEP_END + +# STEP_START zadd_lex +res13 = r.zadd( + "racer_scores", + { + "Norem": 0, + "Sam-Bodden": 0, + "Royce": 0, + "Ford": 0, + "Prickett": 0, + "Castilla": 0, + }, +) +print(res13) # >>> 3 + +res14 = r.zrange("racer_scores", 0, -1) +print( + res14 +) # >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden'] + +res15 = r.zrangebylex("racer_scores", "[A", "[L") +print(res15) # >>> ['Castilla', 'Ford'] +# STEP_END + +# STEP_START leaderboard +res16 = r.zadd("racer_scores", {"Wood": 100}) +print(res16) # >>> 1 + +res17 = r.zadd("racer_scores", {"Henshaw": 100}) +print(res17) # >>> 1 + +res18 = r.zadd("racer_scores", {"Henshaw": 150}) +print(res18) # >>> 0 + +res19 = r.zincrby("racer_scores", 50, "Wood") +print(res19) # >>> 150.0 + +res20 = r.zincrby("racer_scores", 50, "Henshaw") +print(res20) # >>> 200.0 +# STEP_END diff --git a/doctests/dt_stream.py b/doctests/dt_stream.py new file mode 100644 index 0000000000..aa5bbb228f --- /dev/null +++ b/doctests/dt_stream.py @@ -0,0 +1,294 @@ +# EXAMPLE: stream_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END +# REMOVE_START +r.delete("race:france", "race:italy", "race:usa") +# REMOVE_END + +# STEP_START xadd +res1 = r.xadd( + "race:france", + {"rider": "Castilla", "speed": 30.2, "position": 1, "location_id": 1}, +) +print(res1) # >>> 1692629576966-0 + +res2 = r.xadd( + "race:france", + {"rider": "Norem", "speed": 28.8, "position": 3, "location_id": 1}, +) +print(res2) # >>> 1692629594113-0 + +res3 = r.xadd( + "race:france", + {"rider": "Prickett", "speed": 29.7, "position": 2, "location_id": 1}, +) +print(res3) # >>> 1692629613374-0 +# STEP_END + +# REMOVE_START +assert r.xlen("race:france") == 3 +# REMOVE_END + +# STEP_START xrange +res4 = r.xrange("race:france", "1691765278160-0", "+", 2) +print( + res4 +) # >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'})] +# STEP_END + +# STEP_START xread_block +res5 = r.xread(streams={"race:france": 0}, count=100, block=300) +print( + res5 +) # >>> [['race:france', [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}), ('1692629613374-0', {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'})]]] +# STEP_END + +# STEP_START xadd_2 +res6 = r.xadd( + "race:france", + {"rider": "Castilla", "speed": 29.9, "position": 1, "location_id": 2}, +) +print(res6) # >>> 1692629676124-0 +# STEP_END + +# STEP_START xlen +res7 = r.xlen("race:france") +print(res7) # >>> 4 +# STEP_END + + +# STEP_START xadd_id +res8 = r.xadd("race:usa", {"racer": "Castilla"}, id="0-1") +print(res8) # >>> 0-1 + +res9 = r.xadd("race:usa", {"racer": "Norem"}, id="0-2") +print(res9) # >>> 0-2 +# STEP_END + +# STEP_START xadd_bad_id +try: + res10 = r.xadd("race:usa", {"racer": "Prickett"}, id="0-1") + print(res10) # >>> 0-1 +except redis.exceptions.ResponseError as e: + print(e) # >>> WRONGID +# STEP_END + +# STEP_START xrange_all +res11 = r.xrange("race:france", "-", "+") +print( + res11 +) # >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}), ('1692629613374-0', {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'}), ('1692629676124-0', {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'})] +# STEP_END + +# STEP_START xrange_time +res12 = r.xrange("race:france", 1692629576965, 1692629576967) +print( + res12 +) # >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'})] +# STEP_END + +# STEP_START xrange_step_1 +res13 = r.xrange("race:france", "-", "+", 2) +print( + res13 +) # >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'})] +# STEP_END + +# STEP_START xrange_step_2 +res14 = r.xrange("race:france", "(1692629594113-0", "+", 2) +print( + res14 +) # >>> [('1692629613374-0', {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'}), ('1692629676124-0', {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'})] +# STEP_END + +# STEP_START xrange_empty +res15 = r.xrange("race:france", "(1692629676124-0", "+", 2) +print(res15) # >>> [] +# STEP_END + +# STEP_START xrevrange +res16 = r.xrevrange("race:france", "+", "-", 1) +print( + res16 +) # >>> [('1692629676124-0', {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'})] +# STEP_END + +# STEP_START xread +res17 = r.xread(streams={"race:france": 0}, count=2) +print( + res17 +) # >>> [['race:france', [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'})]]] +# STEP_END + +# STEP_START xgroup_create +res18 = r.xgroup_create("race:france", "france_riders", "$") +print(res18) # >>> True +# STEP_END + +# STEP_START xgroup_create_mkstream +res19 = r.xgroup_create("race:italy", "italy_riders", "$", mkstream=True) +print(res19) # >>> True +# STEP_END + +# STEP_START xgroup_read +r.xadd("race:italy", {"rider": "Castilla"}) +r.xadd("race:italy", {"rider": "Royce"}) +r.xadd("race:italy", {"rider": "Sam-Bodden"}) +r.xadd("race:italy", {"rider": "Prickett"}) +r.xadd("race:italy", {"rider": "Norem"}) + +res20 = r.xreadgroup( + streams={"race:italy": ">"}, + consumername="Alice", + groupname="italy_riders", + count=1, +) +print( + res20 +) # >>> [['race:italy', [('1692629925771-0', {'rider': 'Castilla'})]]] +# STEP_END + +# STEP_START xgroup_read_id +res21 = r.xreadgroup( + streams={"race:italy": 0}, + consumername="Alice", + groupname="italy_riders", + count=1, +) +print( + res21 +) # >>> [['race:italy', [('1692629925771-0', {'rider': 'Castilla'})]]] +# STEP_END + +# STEP_START xack +res22 = r.xack("race:italy", "italy_riders", "1692629925771-0") +print(res22) # >>> 1 + +res23 = r.xreadgroup( + streams={"race:italy": 0}, + consumername="Alice", + groupname="italy_riders", + count=1, +) +print(res23) # >>> [['race:italy', []]] +# STEP_END + +# STEP_START xgroup_read_bob +res24 = r.xreadgroup( + streams={"race:italy": ">"}, + consumername="Bob", + groupname="italy_riders", + count=2, +) +print( + res24 +) # >>> [['race:italy', [('1692629925789-0', {'rider': 'Royce'}), ('1692629925790-0', {'rider': 'Sam-Bodden'})]]] +# STEP_END + +# STEP_START xpending +res25 = r.xpending("race:italy", "italy_riders") +print( + res25 +) # >>> {'pending': 2, 'min': '1692629925789-0', 'max': '1692629925790-0', 'consumers': [{'name': 'Bob', 'pending': 2}]} +# STEP_END + +# STEP_START xpending_plus_minus +res26 = r.xpending_range("race:italy", "italy_riders", "-", "+", 10) +print( + res26 +) # >>> [{'message_id': '1692629925789-0', 'consumer': 'Bob', 'time_since_delivered': 31084, 'times_delivered': 1}, {'message_id': '1692629925790-0', 'consumer': 'Bob', 'time_since_delivered': 31084, 'times_delivered': 1}] +# STEP_END + +# STEP_START xrange_pending +res27 = r.xrange("race:italy", "1692629925789-0", "1692629925789-0") +print(res27) # >>> [('1692629925789-0', {'rider': 'Royce'})] +# STEP_END + +# STEP_START xclaim +res28 = r.xclaim( + "race:italy", "italy_riders", "Alice", 60000, ["1692629925789-0"] +) +print(res28) # >>> [('1692629925789-0', {'rider': 'Royce'})] +# STEP_END + +# STEP_START xautoclaim +res29 = r.xautoclaim("race:italy", "italy_riders", "Alice", 1, "0-0", 1) +print( + res29 +) # >>> ['1692629925790-0', [('1692629925789-0', {'rider': 'Royce'})]] +# STEP_END + +# STEP_START xautoclaim_cursor +res30 = r.xautoclaim( + "race:italy", "italy_riders", "Alice", 1, "(1692629925789-0", 1 +) +print(res30) # >>> ['0-0', [('1692629925790-0', {'rider': 'Sam-Bodden'})]] +# STEP_END + +# STEP_START xinfo +res31 = r.xinfo_stream("race:italy") +print( + res31 +) # >>> {'length': 5, 'radix-tree-keys': 1, 'radix-tree-nodes': 2, 'last-generated-id': '1692629926436-0', 'groups': 1, 'first-entry': ('1692629925771-0', {'rider': 'Castilla'}), 'last-entry': ('1692629926436-0', {'rider': 'Norem'})} +# STEP_END + +# STEP_START xinfo_groups +res32 = r.xinfo_groups("race:italy") +print( + res32 +) # >>> [{'name': 'italy_riders', 'consumers': 2, 'pending': 2, 'last-delivered-id': '1692629925790-0'}] +# STEP_END + +# STEP_START xinfo_consumers +res33 = r.xinfo_consumers("race:italy", "italy_riders") +print( + res33 +) # >>> [{'name': 'Alice', 'pending': 2, 'idle': 199332}, {'name': 'Bob', 'pending': 0, 'idle': 489170}] +# STEP_END + +# STEP_START maxlen +r.xadd("race:italy", {"rider": "Jones"}, maxlen=2) +r.xadd("race:italy", {"rider": "Wood"}, maxlen=2) +r.xadd("race:italy", {"rider": "Henshaw"}, maxlen=2) + +res34 = r.xlen("race:italy") +print(res34) # >>> 8 + +res35 = r.xrange("race:italy", "-", "+") +print( + res35 +) # >>> [('1692629925771-0', {'rider': 'Castilla'}), ('1692629925789-0', {'rider': 'Royce'}), ('1692629925790-0', {'rider': 'Sam-Bodden'}), ('1692629925791-0', {'rider': 'Prickett'}), ('1692629926436-0', {'rider': 'Norem'}), ('1692630612602-0', {'rider': 'Jones'}), ('1692630641947-0', {'rider': 'Wood'}), ('1692630648281-0', {'rider': 'Henshaw'})] + +r.xadd("race:italy", {"rider": "Smith"}, maxlen=2, approximate=False) + +res36 = r.xrange("race:italy", "-", "+") +print( + res36 +) # >>> [('1692630648281-0', {'rider': 'Henshaw'}), ('1692631018238-0', {'rider': 'Smith'})] +# STEP_END + +# STEP_START xtrim +res37 = r.xtrim("race:italy", maxlen=10, approximate=False) +print(res37) # >>> 0 +# STEP_END + +# STEP_START xtrim2 +res38 = r.xtrim("race:italy", maxlen=10) +print(res38) # >>> 0 +# STEP_END + +# STEP_START xdel +res39 = r.xrange("race:italy", "-", "+") +print( + res39 +) # >>> [('1692630648281-0', {'rider': 'Henshaw'}), ('1692631018238-0', {'rider': 'Smith'})] + +res40 = r.xdel("race:italy", "1692631018238-0") +print(res40) # >>> 1 + +res41 = r.xrange("race:italy", "-", "+") +print(res41) # >>> [('1692630648281-0', {'rider': 'Henshaw'})] +# STEP_END diff --git a/doctests/dt_tdigest.py b/doctests/dt_tdigest.py new file mode 100644 index 0000000000..a6196b514c --- /dev/null +++ b/doctests/dt_tdigest.py @@ -0,0 +1,76 @@ +# EXAMPLE: tdigest_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("racer_ages") +r.delete("bikes:sales") +# REMOVE_END + +# STEP_START tdig_start +res1 = r.tdigest().create("bikes:sales", 100) +print(res1) # >>> True + +res2 = r.tdigest().add("bikes:sales", [21]) +print(res2) # >>> OK + +res3 = r.tdigest().add("bikes:sales", [150, 95, 75, 34]) +print(res3) # >>> OK +# STEP_END + +# REMOVE_START +assert res1 is True +# REMOVE_END + +# STEP_START tdig_cdf +res4 = r.tdigest().create("racer_ages") +print(res4) # >>> True + +res5 = r.tdigest().add( + "racer_ages", + [ + 45.88, + 44.2, + 58.03, + 19.76, + 39.84, + 69.28, + 50.97, + 25.41, + 19.27, + 85.71, + 42.63, + ], +) +print(res5) # >>> OK + +res6 = r.tdigest().rank("racer_ages", 50) +print(res6) # >>> [7] + +res7 = r.tdigest().rank("racer_ages", 50, 40) +print(res7) # >>> [7, 4] +# STEP_END + +# STEP_START tdig_quant +res8 = r.tdigest().quantile("racer_ages", 0.5) +print(res8) # >>> [44.2] + +res9 = r.tdigest().byrank("racer_ages", 4) +print(res9) # >>> [42.63] +# STEP_END + +# STEP_START tdig_min +res10 = r.tdigest().min("racer_ages") +print(res10) # >>> 19.27 + +res11 = r.tdigest().max("racer_ages") +print(res11) # >>> 85.71 +# STEP_END + +# STEP_START tdig_reset +res12 = r.tdigest().reset("racer_ages") +print(res12) # >>> OK +# STEP_END diff --git a/doctests/dt_topk.py b/doctests/dt_topk.py new file mode 100644 index 0000000000..01ad38a249 --- /dev/null +++ b/doctests/dt_topk.py @@ -0,0 +1,33 @@ +# EXAMPLE: topk_tutorial +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# REMOVE_START +r.delete("bikes:keywords") +# REMOVE_END + +# STEP_START topk +res1 = r.topk().reserve("bikes:keywords", 5, 2000, 7, 0.925) +print(res1) # >>> True + +res2 = r.topk().add( + "bikes:keywords", + "store", + "seat", + "handlebars", + "handles", + "pedals", + "tires", + "store", + "seat", +) +print(res2) # >>> [None, None, None, None, None, 'handlebars', None, None] + +res3 = r.topk().list("bikes:keywords") +print(res3) # >>> ['store', 'seat', 'pedals', 'tires', 'handles'] + +res4 = r.topk().query("bikes:keywords", "store", "handlebars") +print(res4) # >>> [1, 0] From a22c7197c3c5564bd980e655e934fd2cf3a83073 Mon Sep 17 00:00:00 2001 From: Savannah Date: Thu, 12 Oct 2023 14:01:37 -0400 Subject: [PATCH 3/3] Update dt_set.py updating after june 27 breaking change for sismember return type --- doctests/dt_set.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/doctests/dt_set.py b/doctests/dt_set.py index 1a8d9638b0..26f54160c5 100644 --- a/doctests/dt_set.py +++ b/doctests/dt_set.py @@ -36,15 +36,15 @@ r.sadd("bikes:racing:usa", "bike:1", "bike:4") # HIDE_END res5 = r.sismember("bikes:racing:usa", "bike:1") -print(res5) # >>> True +print(res5) # >>> 1 res6 = r.sismember("bikes:racing:usa", "bike:2") -print(res6) # >>> False +print(res6) # >>> 1 # STEP_END # REMOVE_START -assert res5 is True -assert res6 is False +assert res5 == 1 +assert res6 == 0 # REMOVE_END # STEP_START sinter @@ -88,15 +88,15 @@ # STEP_START smismember res11 = r.sismember("bikes:racing:france", "bike:1") -print(res11) # >>> True +print(res11) # >>> 1 res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4") -print(res12) # >>> [True, True, False] +print(res12) # >>> [1, 1, 0] # STEP_END # REMOVE_START -assert res11 is True -assert res12 == [True, True, False] +assert res11 == 1 +assert res12 == [1, 1, 0] # REMOVE_END # STEP_START sdiff