Skip to content

Commit 24f0adc

Browse files
authored
the tabbed example code for python (#3002)
* Create dt_set.py creating the python code for the tabbed example on the set data type page * 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 * Update dt_set.py updating after june 27 breaking change for sismember return type
1 parent 4042bf8 commit 24f0adc

File tree

12 files changed

+1170
-0
lines changed

12 files changed

+1170
-0
lines changed

doctests/dt_bitfield.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# EXAMPLE: bitfield_tutorial
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# REMOVE_START
9+
r.delete("bike:1:stats")
10+
# REMOVE_END
11+
12+
# STEP_START bf
13+
bf = r.bitfield("bike:1:stats")
14+
res1 = bf.set("u32", "#0", 1000).execute()
15+
print(res1) # >>> [0]
16+
17+
res2 = bf.incrby("u32", "#0", -50).incrby("u32", "#1", 1).execute()
18+
print(res2) # >>> [950, 1]
19+
20+
res3 = bf.incrby("u32", "#0", 500).incrby("u32", "#1", 1).execute()
21+
print(res3) # >>> [1450, 2]
22+
23+
res4 = bf.get("u32", "#0").get("u32", "#1").execute()
24+
print(res4) # >>> [1450, 2]
25+
# STEP_END
26+
27+
# REMOVE_START
28+
assert res1 == [0]
29+
assert res4 == [1450, 2]
30+
# REMOVE_END

doctests/dt_bitmap.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# EXAMPLE: bitmap_tutorial
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# REMOVE_START
9+
r.delete("pings:2024-01-01-00:00")
10+
# REMOVE_END
11+
12+
# STEP_START ping
13+
res1 = r.setbit("pings:2024-01-01-00:00", 123, 1)
14+
print(res1) # >>> 0
15+
16+
res2 = r.getbit("pings:2024-01-01-00:00", 123)
17+
print(res2) # >>> 1
18+
19+
res3 = r.getbit("pings:2024-01-01-00:00", 456)
20+
print(res3) # >>> 0
21+
# STEP_END
22+
23+
# REMOVE_START
24+
assert res1 == 0
25+
# REMOVE_END
26+
27+
# STEP_START bitcount
28+
# HIDE_START
29+
r.setbit("pings:2024-01-01-00:00", 123, 1)
30+
# HIDE_END
31+
res4 = r.bitcount("pings:2024-01-01-00:00")
32+
print(res4) # >>> 1
33+
# STEP_END
34+
# REMOVE_START
35+
assert res4 == 1
36+
# REMOVE_END

doctests/dt_bloom.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# EXAMPLE: bf_tutorial
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# STEP_START bloom
9+
res1 = r.bf().reserve("bikes:models", 0.01, 1000)
10+
print(res1) # >>> True
11+
12+
res2 = r.bf().add("bikes:models", "Smoky Mountain Striker")
13+
print(res2) # >>> True
14+
15+
res3 = r.bf().exists("bikes:models", "Smoky Mountain Striker")
16+
print(res3) # >>> True
17+
18+
res4 = r.bf().madd(
19+
"bikes:models",
20+
"Rocky Mountain Racer",
21+
"Cloudy City Cruiser",
22+
"Windy City Wippet",
23+
)
24+
print(res4) # >>> [True, True, True]
25+
26+
res5 = r.bf().mexists(
27+
"bikes:models",
28+
"Rocky Mountain Racer",
29+
"Cloudy City Cruiser",
30+
"Windy City Wippet",
31+
)
32+
print(res5) # >>> [True, True, True]
33+
# STEP_END
34+
35+
# REMOVE_START
36+
assert res1 is True
37+
# REMOVE_END

doctests/dt_cms.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# EXAMPLE: cms_tutorial
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
# REMOVE_START
8+
r.delete("bikes:profit")
9+
# REMOVE_END
10+
11+
# STEP_START cms
12+
res1 = r.cms().initbyprob("bikes:profit", 0.001, 0.002)
13+
print(res1) # >>> True
14+
15+
res2 = r.cms().incrby("bikes:profit", ["Smoky Mountain Striker"], [100])
16+
print(res2) # >>> [100]
17+
18+
res3 = r.cms().incrby(
19+
"bikes:profit", ["Rocky Mountain Racer", "Cloudy City Cruiser"], [200, 150]
20+
)
21+
print(res3) # >>> [200, 150]
22+
23+
res4 = r.cms().query("bikes:profit", "Smoky Mountain Striker")
24+
print(res4) # >>> [100]
25+
26+
res5 = r.cms().info("bikes:profit")
27+
print(res5.width, res5.depth, res5.count) # >>> 2000 9 450
28+
# STEP_END

doctests/dt_cuckoo.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# EXAMPLE: cuckoo_tutorial
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# REMOVE_START
9+
r.delete("bikes:models")
10+
# REMOVE_END
11+
12+
# STEP_START cuckoo
13+
res1 = r.cf().reserve("bikes:models", 1000000)
14+
print(res1) # >>> True
15+
16+
res2 = r.cf().add("bikes:models", "Smoky Mountain Striker")
17+
print(res2) # >>> 1
18+
19+
res3 = r.cf().exists("bikes:models", "Smoky Mountain Striker")
20+
print(res3) # >>> 1
21+
22+
res4 = r.cf().exists("bikes:models", "Terrible Bike Name")
23+
print(res4) # >>> 0
24+
25+
res5 = r.cf().delete("bikes:models", "Smoky Mountain Striker")
26+
print(res5) # >>> 1
27+
# STEP_END
28+
29+
# REMOVE_START
30+
assert res1 is True
31+
assert res5 == 1
32+
# REMOVE_END

doctests/dt_hll.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import redis
2+
3+
# HIDE_START
4+
r = redis.Redis(decode_responses=True)
5+
# HIDE_END
6+
7+
# REMOVE_START
8+
r.delete("bikes", "commuter_bikes", "all_bikes")
9+
# REMOVE_END
10+
11+
# STEP_START pfadd
12+
res1 = r.pfadd("bikes", "Hyperion", "Deimos", "Phoebe", "Quaoar")
13+
print(res1) # >>> 1
14+
15+
res2 = r.pfcount("bikes")
16+
print(res2) # >>> 4
17+
18+
res3 = r.pfadd("commuter_bikes", "Salacia", "Mimas", "Quaoar")
19+
print(res3) # >>> 1
20+
21+
res4 = r.pfmerge("all_bikes", "bikes", "commuter_bikes")
22+
print(res4) # >>> True
23+
24+
res5 = r.pfcount("all_bikes")
25+
print(res5) # >>> 6
26+
# STEP_END
27+
28+
# REMOVE_START
29+
assert res4 == True
30+
# REMOVE_END

0 commit comments

Comments
 (0)