-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
11 changed files
with
999 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.