-
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.
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 <elena@kolevska.com>
- Loading branch information
1 parent
ea3a236
commit 2bed636
Showing
2 changed files
with
105 additions
and
2 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
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,103 @@ | ||
# EXAMPLE: hash_tutorial | ||
# HIDE_START | ||
import redis | ||
|
||
r = redis.Redis(decode_responses=True) | ||
# HIDE_END | ||
# STEP_START set_get_all | ||
res1 = r.hset( | ||
"bike:1", | ||
mapping={ | ||
"model": "Deimos", | ||
"brand": "Ergonom", | ||
"type": "Enduro bikes", | ||
"price": 4972, | ||
}, | ||
) | ||
print(res1) | ||
# >>> 4 | ||
|
||
res2 = r.hget("bike:1", "model") | ||
print(res2) | ||
# >>> 'Deimos' | ||
|
||
res3 = r.hget("bike:1", "price") | ||
print(res3) | ||
# >>> '4972' | ||
|
||
res4 = r.hgetall("bike:1") | ||
print(res4) | ||
# >>> {'model': 'Deimos', 'brand': 'Ergonom', 'type': 'Enduro bikes', 'price': '4972'} | ||
|
||
# STEP_END | ||
|
||
# REMOVE_START | ||
assert res1 == 4 | ||
assert res2 == "Deimos" | ||
assert res3 == "4972" | ||
assert res4 == { | ||
"model": "Deimos", | ||
"brand": "Ergonom", | ||
"type": "Enduro bikes", | ||
"price": "4972", | ||
} | ||
# REMOVE_END | ||
|
||
# STEP_START hmget | ||
res5 = r.hmget("bike:1", ["model", "price"]) | ||
print(res5) | ||
# >>> ['Deimos', '4972'] | ||
# STEP_END | ||
|
||
# REMOVE_START | ||
assert res5 == ["Deimos", "4972"] | ||
# REMOVE_END | ||
|
||
# STEP_START hincrby | ||
res6 = r.hincrby("bike:1", "price", 100) | ||
print(res6) | ||
# >>> 5072 | ||
res7 = r.hincrby("bike:1", "price", -100) | ||
print(res7) | ||
# >>> 4972 | ||
# STEP_END | ||
|
||
# REMOVE_START | ||
assert res6 == 5072 | ||
assert res7 == 4972 | ||
# REMOVE_END | ||
|
||
|
||
# STEP_START incrby_get_mget | ||
res11 = r.hincrby("bike:1:stats", "rides", 1) | ||
print(res11) | ||
# >>> 1 | ||
res12 = r.hincrby("bike:1:stats", "rides", 1) | ||
print(res12) | ||
# >>> 2 | ||
res13 = r.hincrby("bike:1:stats", "rides", 1) | ||
print(res13) | ||
# >>> 3 | ||
res14 = r.hincrby("bike:1:stats", "crashes", 1) | ||
print(res14) | ||
# >>> 1 | ||
res15 = r.hincrby("bike:1:stats", "owners", 1) | ||
print(res15) | ||
# >>> 1 | ||
res16 = r.hget("bike:1:stats", "rides") | ||
print(res16) | ||
# >>> 3 | ||
res17 = r.hmget("bike:1:stats", ["crashes", "owners"]) | ||
print(res17) | ||
# >>> ['1', '1'] | ||
# STEP_END | ||
|
||
# REMOVE_START | ||
assert res11 == 1 | ||
assert res12 == 2 | ||
assert res13 == 3 | ||
assert res14 == 1 | ||
assert res15 == 1 | ||
assert res16 == "3" | ||
assert res17 == ["1", "1"] | ||
# REMOVE_END |