forked from redis/redis-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdt_hash.py
107 lines (94 loc) · 1.93 KB
/
dt_hash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# EXAMPLE: hash_tutorial
# HIDE_START
"""
Code samples for Hash doc pages:
https://redis.io/docs/latest/develop/data-types/hashes/
"""
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