Skip to content

Commit

Permalink
the tabbed example code for python (#3002)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sav-norem authored Nov 15, 2023
1 parent 4042bf8 commit 24f0adc
Show file tree
Hide file tree
Showing 12 changed files with 1,170 additions and 0 deletions.
30 changes: 30 additions & 0 deletions doctests/dt_bitfield.py
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
36 changes: 36 additions & 0 deletions doctests/dt_bitmap.py
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
37 changes: 37 additions & 0 deletions doctests/dt_bloom.py
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
28 changes: 28 additions & 0 deletions doctests/dt_cms.py
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
32 changes: 32 additions & 0 deletions doctests/dt_cuckoo.py
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
30 changes: 30 additions & 0 deletions doctests/dt_hll.py
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
Loading

0 comments on commit 24f0adc

Please sign in to comment.