forked from redis/redis-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds_hash.py
63 lines (51 loc) · 1.1 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
# 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