Skip to content

Commit 4935e99

Browse files
andy-stark-redisvladvildanov
authored andcommitted
Move code samples to master branch (#3271)
Move the code samples used for documentation into the master branch, so they sit next to the code they document.
1 parent c7ccba9 commit 4935e99

23 files changed

+2809
-0
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Move doctests (doc code examples) to main branch
12
* Update `ResponseT` type hint
23
* Allow to control the minimum SSL version
34
* Add an optional lock_name attribute to LockError.

doctests/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Command examples for redis.io
2+
3+
## How to add an example
4+
5+
Create regular python file in the current folder with meaningful name. It makes sense prefix example files with
6+
command category (e.g. string, set, list, hash, etc) to make navigation in the folder easier. Files ending in *.py*
7+
are automatically run by the test suite.
8+
9+
### Special markup
10+
11+
See https://github.com/redis-stack/redis-stack-website#readme for more details.
12+
13+
## How to test examples
14+
15+
Examples are standalone python scripts, committed to the *doctests* directory. These scripts assume that the
16+
```requirements.txt``` and ```dev_requirements.txt``` from this repository have been installed, as per below.
17+
18+
```bash
19+
pip install -r requirements.txt
20+
pip install -r dev_requirements.txt
21+
pip install -r doctests/requirements.txt
22+
```
23+
24+
Note - the CI process, runs the basic ```black``` and ```isort``` linters against the examples. Assuming
25+
the requirements above have been installed you can run ```black yourfile.py``` and ```isort yourfile.py```
26+
locally to validate the linting, prior to CI.
27+
28+
Just include necessary assertions in the example file and run
29+
```bash
30+
sh doctests/run_examples.sh
31+
```
32+
to test all examples in the current folder.

doctests/dt_bitfield.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# EXAMPLE: bitfield_tutorial
2+
# HIDE_START
3+
"""
4+
Code samples for Bitfield doc pages:
5+
https://redis.io/docs/latest/develop/data-types/bitfields/
6+
"""
7+
import redis
8+
9+
r = redis.Redis(decode_responses=True)
10+
# HIDE_END
11+
12+
# REMOVE_START
13+
r.delete("bike:1:stats")
14+
# REMOVE_END
15+
16+
# STEP_START bf
17+
bf = r.bitfield("bike:1:stats")
18+
res1 = bf.set("u32", "#0", 1000).execute()
19+
print(res1) # >>> [0]
20+
21+
res2 = bf.incrby("u32", "#0", -50).incrby("u32", "#1", 1).execute()
22+
print(res2) # >>> [950, 1]
23+
24+
res3 = bf.incrby("u32", "#0", 500).incrby("u32", "#1", 1).execute()
25+
print(res3) # >>> [1450, 2]
26+
27+
res4 = bf.get("u32", "#0").get("u32", "#1").execute()
28+
print(res4) # >>> [1450, 2]
29+
# STEP_END
30+
31+
# REMOVE_START
32+
assert res1 == [0]
33+
assert res4 == [1450, 2]
34+
# REMOVE_END

doctests/dt_bitmap.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# EXAMPLE: bitmap_tutorial
2+
# HIDE_START
3+
"""
4+
Code samples for Bitmap doc pages:
5+
https://redis.io/docs/latest/develop/data-types/bitmaps/
6+
"""
7+
import redis
8+
9+
r = redis.Redis(decode_responses=True)
10+
# HIDE_END
11+
12+
# REMOVE_START
13+
r.delete("pings:2024-01-01-00:00")
14+
# REMOVE_END
15+
16+
# STEP_START ping
17+
res1 = r.setbit("pings:2024-01-01-00:00", 123, 1)
18+
print(res1) # >>> 0
19+
20+
res2 = r.getbit("pings:2024-01-01-00:00", 123)
21+
print(res2) # >>> 1
22+
23+
res3 = r.getbit("pings:2024-01-01-00:00", 456)
24+
print(res3) # >>> 0
25+
# STEP_END
26+
27+
# REMOVE_START
28+
assert res1 == 0
29+
# REMOVE_END
30+
31+
# STEP_START bitcount
32+
# HIDE_START
33+
r.setbit("pings:2024-01-01-00:00", 123, 1)
34+
# HIDE_END
35+
res4 = r.bitcount("pings:2024-01-01-00:00")
36+
print(res4) # >>> 1
37+
# STEP_END
38+
# REMOVE_START
39+
assert res4 == 1
40+
# REMOVE_END

doctests/dt_bloom.py

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

doctests/dt_cms.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# EXAMPLE: cms_tutorial
2+
# HIDE_START
3+
"""
4+
Code samples for Count-min sketch doc pages:
5+
https://redis.io/docs/latest/develop/data-types/probabilistic/count-min-sketch/
6+
"""
7+
import redis
8+
9+
r = redis.Redis(decode_responses=True)
10+
# HIDE_END
11+
# REMOVE_START
12+
r.delete("bikes:profit")
13+
# REMOVE_END
14+
15+
# STEP_START cms
16+
res1 = r.cms().initbyprob("bikes:profit", 0.001, 0.002)
17+
print(res1) # >>> True
18+
19+
res2 = r.cms().incrby("bikes:profit", ["Smoky Mountain Striker"], [100])
20+
print(res2) # >>> [100]
21+
22+
res3 = r.cms().incrby(
23+
"bikes:profit", ["Rocky Mountain Racer", "Cloudy City Cruiser"], [200, 150]
24+
)
25+
print(res3) # >>> [200, 150]
26+
27+
res4 = r.cms().query("bikes:profit", "Smoky Mountain Striker")
28+
print(res4) # >>> [100]
29+
30+
res5 = r.cms().info("bikes:profit")
31+
print(res5.width, res5.depth, res5.count) # >>> 2000 9 450
32+
# STEP_END

doctests/dt_cuckoo.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# EXAMPLE: cuckoo_tutorial
2+
# HIDE_START
3+
"""
4+
Code samples for Cuckoo filter doc pages:
5+
https://redis.io/docs/latest/develop/data-types/probabilistic/cuckoo-filter/
6+
"""
7+
import redis
8+
9+
r = redis.Redis(decode_responses=True)
10+
# HIDE_END
11+
12+
# REMOVE_START
13+
r.delete("bikes:models")
14+
# REMOVE_END
15+
16+
# STEP_START cuckoo
17+
res1 = r.cf().reserve("bikes:models", 1000000)
18+
print(res1) # >>> True
19+
20+
res2 = r.cf().add("bikes:models", "Smoky Mountain Striker")
21+
print(res2) # >>> 1
22+
23+
res3 = r.cf().exists("bikes:models", "Smoky Mountain Striker")
24+
print(res3) # >>> 1
25+
26+
res4 = r.cf().exists("bikes:models", "Terrible Bike Name")
27+
print(res4) # >>> 0
28+
29+
res5 = r.cf().delete("bikes:models", "Smoky Mountain Striker")
30+
print(res5) # >>> 1
31+
# STEP_END
32+
33+
# REMOVE_START
34+
assert res1 is True
35+
assert res5 == 1
36+
# REMOVE_END

doctests/dt_geo.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# EXAMPLE: geo_tutorial
2+
# HIDE_START
3+
"""
4+
Code samples for Geospatial doc pages:
5+
https://redis.io/docs/latest/develop/data-types/geospatial/
6+
"""
7+
import redis
8+
9+
r = redis.Redis(decode_responses=True)
10+
# HIDE_END
11+
# REMOVE_START
12+
r.delete("bikes:rentable")
13+
# REMOVE_END
14+
15+
# STEP_START geoadd
16+
res1 = r.geoadd("bikes:rentable", [-122.27652, 37.805186, "station:1"])
17+
print(res1) # >>> 1
18+
19+
res2 = r.geoadd("bikes:rentable", [-122.2674626, 37.8062344, "station:2"])
20+
print(res2) # >>> 1
21+
22+
res3 = r.geoadd("bikes:rentable", [-122.2469854, 37.8104049, "station:3"])
23+
print(res3) # >>> 1
24+
# STEP_END
25+
26+
# REMOVE_START
27+
assert res1 == 1
28+
assert res2 == 1
29+
assert res3 == 1
30+
# REMOVE_END
31+
32+
# STEP_START geosearch
33+
res4 = r.geosearch(
34+
"bikes:rentable",
35+
longitude=-122.27652,
36+
latitude=37.805186,
37+
radius=5,
38+
unit="km",
39+
)
40+
print(res4) # >>> ['station:1', 'station:2', 'station:3']
41+
# STEP_END
42+
43+
# REMOVE_START
44+
assert res4 == ["station:1", "station:2", "station:3"]
45+
# REMOVE_END

doctests/dt_hash.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# EXAMPLE: hash_tutorial
2+
# HIDE_START
3+
"""
4+
Code samples for Hash doc pages:
5+
https://redis.io/docs/latest/develop/data-types/hashes/
6+
"""
7+
import redis
8+
9+
r = redis.Redis(decode_responses=True)
10+
# HIDE_END
11+
# STEP_START set_get_all
12+
res1 = r.hset(
13+
"bike:1",
14+
mapping={
15+
"model": "Deimos",
16+
"brand": "Ergonom",
17+
"type": "Enduro bikes",
18+
"price": 4972,
19+
},
20+
)
21+
print(res1)
22+
# >>> 4
23+
24+
res2 = r.hget("bike:1", "model")
25+
print(res2)
26+
# >>> 'Deimos'
27+
28+
res3 = r.hget("bike:1", "price")
29+
print(res3)
30+
# >>> '4972'
31+
32+
res4 = r.hgetall("bike:1")
33+
print(res4)
34+
# >>> {'model': 'Deimos', 'brand': 'Ergonom', 'type': 'Enduro bikes', 'price': '4972'}
35+
36+
# STEP_END
37+
38+
# REMOVE_START
39+
assert res1 == 4
40+
assert res2 == "Deimos"
41+
assert res3 == "4972"
42+
assert res4 == {
43+
"model": "Deimos",
44+
"brand": "Ergonom",
45+
"type": "Enduro bikes",
46+
"price": "4972",
47+
}
48+
# REMOVE_END
49+
50+
# STEP_START hmget
51+
res5 = r.hmget("bike:1", ["model", "price"])
52+
print(res5)
53+
# >>> ['Deimos', '4972']
54+
# STEP_END
55+
56+
# REMOVE_START
57+
assert res5 == ["Deimos", "4972"]
58+
# REMOVE_END
59+
60+
# STEP_START hincrby
61+
res6 = r.hincrby("bike:1", "price", 100)
62+
print(res6)
63+
# >>> 5072
64+
res7 = r.hincrby("bike:1", "price", -100)
65+
print(res7)
66+
# >>> 4972
67+
# STEP_END
68+
69+
# REMOVE_START
70+
assert res6 == 5072
71+
assert res7 == 4972
72+
# REMOVE_END
73+
74+
75+
# STEP_START incrby_get_mget
76+
res11 = r.hincrby("bike:1:stats", "rides", 1)
77+
print(res11)
78+
# >>> 1
79+
res12 = r.hincrby("bike:1:stats", "rides", 1)
80+
print(res12)
81+
# >>> 2
82+
res13 = r.hincrby("bike:1:stats", "rides", 1)
83+
print(res13)
84+
# >>> 3
85+
res14 = r.hincrby("bike:1:stats", "crashes", 1)
86+
print(res14)
87+
# >>> 1
88+
res15 = r.hincrby("bike:1:stats", "owners", 1)
89+
print(res15)
90+
# >>> 1
91+
res16 = r.hget("bike:1:stats", "rides")
92+
print(res16)
93+
# >>> 3
94+
res17 = r.hmget("bike:1:stats", ["crashes", "owners"])
95+
print(res17)
96+
# >>> ['1', '1']
97+
# STEP_END
98+
99+
# REMOVE_START
100+
assert res11 == 1
101+
assert res12 == 2
102+
assert res13 == 3
103+
assert res14 == 1
104+
assert res15 == 1
105+
assert res16 == "3"
106+
assert res17 == ["1", "1"]
107+
# REMOVE_END

0 commit comments

Comments
 (0)