Skip to content

Commit

Permalink
Adds doctest examples for the hash tutorial (#2821)
Browse files Browse the repository at this point in the history
* 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
sav-norem and elena-kolevska committed Jul 4, 2023
1 parent ea3a236 commit 2bed636
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions doctests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Examples are standalone python scripts, committed to the *doctests* directory. T
```requirements.txt``` and ```dev_requirements.txt``` from this repository have been installed, as per below.

```bash
pip install requirements.txt
pip install dev_requirements.txt
pip install -r requirements.txt
pip install -r dev_requirements.txt
```

Note - the CI process, runs the basic ```black``` and ```isort``` linters against the examples. Assuming
Expand Down
103 changes: 103 additions & 0 deletions doctests/dt_hash.py
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

0 comments on commit 2bed636

Please sign in to comment.