Skip to content

Commit 2bed636

Browse files
Adds doctest examples for the hash tutorial (#2821)
* Create dt_hash.py creating the python hash data type examples that will be used in conjunction with the hashes.md file in docs to show tabbed examples * Fixes some steps not showing * Fixes instructions for doctests * Making linter happy * Format result as per Igor’s suggestion * Change all the other outputs to Igor’s suggestion * make linter happy --------- Co-authored-by: Elena Kolevska <[email protected]>
1 parent ea3a236 commit 2bed636

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

doctests/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Examples are standalone python scripts, committed to the *doctests* directory. T
1616
```requirements.txt``` and ```dev_requirements.txt``` from this repository have been installed, as per below.
1717

1818
```bash
19-
pip install requirements.txt
20-
pip install dev_requirements.txt
19+
pip install -r requirements.txt
20+
pip install -r dev_requirements.txt
2121
```
2222

2323
Note - the CI process, runs the basic ```black``` and ```isort``` linters against the examples. Assuming

doctests/dt_hash.py

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

0 commit comments

Comments
 (0)