-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathcmds_hash.py
87 lines (69 loc) · 1.64 KB
/
cmds_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
# EXAMPLE: cmds_hash
# HIDE_START
import redis
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
# HIDE_END
# STEP_START hset
res1 = r.hset("myhash", "field1", "Hello")
print(res1)
# >>> 1
res2 = r.hget("myhash", "field1")
print(res2)
# >>> Hello
res3 = r.hset("myhash", mapping={"field2": "Hi", "field3": "World"})
print(res3)
# >>> 2
res4 = r.hget("myhash", "field2")
print(res4)
# >>> Hi
res5 = r.hget("myhash", "field3")
print(res5)
# >>> World
res6 = r.hgetall("myhash")
print(res6)
# >>> { "field1": "Hello", "field2": "Hi", "field3": "World" }
# REMOVE_START
assert res1 == 1
assert res2 == "Hello"
assert res3 == 2
assert res4 == "Hi"
assert res5 == "World"
assert res6 == { "field1": "Hello", "field2": "Hi", "field3": "World" }
r.delete("myhash")
# REMOVE_END
# STEP_END
# STEP_START hget
res7 = r.hset("myhash", "field1", "foo")
print(res7)
# >>> 1
res8 = r.hget("myhash", "field1")
print(res8)
# >>> foo
res9 = r.hget("myhash", "field2")
print(res9)
# >>> None
# REMOVE_START
assert res7 == 1
assert res8 == "foo"
assert res9 == None
r.delete("myhash")
# REMOVE_END
# STEP_END
# STEP_START hgetall
res10 = r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})
res11 = r.hgetall("myhash")
print(res11) # >>> { "field1": "Hello", "field2": "World" }
# REMOVE_START
assert res11 == { "field1": "Hello", "field2": "World" }
r.delete("myhash")
# REMOVE_END
# STEP_END
# STEP_START hvals
res10 = r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})
res11 = r.hvals("myhash")
print(res11) # >>> [ "Hello", "World" ]
# REMOVE_START
assert res11 == [ "Hello", "World" ]
r.delete("myhash")
# REMOVE_END
# STEP_END