forked from redis/redis-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds_sorted_set.py
89 lines (77 loc) · 1.72 KB
/
cmds_sorted_set.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
# EXAMPLE: cmds_sorted_set
# HIDE_START
import redis
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
# HIDE_END
# STEP_START zadd
res = r.zadd("myzset", {"one": 1})
print(res)
# >>> 1
# REMOVE_START
assert res == 1
# REMOVE_END
res = r.zadd("myzset", {"uno": 1})
print(res)
# >>> 1
# REMOVE_START
assert res == 1
# REMOVE_END
res = r.zadd("myzset", {"two": 2, "three": 3})
print(res)
# >>> 2
# REMOVE_START
assert res == 2
# REMOVE_END
res = r.zrange("myzset", 0, -1, withscores=True)
# >>> [('one', 1.0), ('uno', 1.0), ('two', 2.0), ('three', 3.0)]
# REMOVE_START
assert res == [('one', 1.0), ('uno', 1.0), ('two', 2.0), ('three', 3.0)]
# REMOVE_END
# REMOVE_START
r.delete("myzset")
# REMOVE_END
# STEP_END
# STEP_START zrange1
res = r.zadd("myzset", {"one": 1, "two":2, "three":3})
print(res)
# >>> 3
res = r.zrange("myzset", 0, -1)
print(res)
# >>> ['one', 'two', 'three']
# REMOVE_START
assert res == ['one', 'two', 'three']
# REMOVE_END
res = r.zrange("myzset", 2, 3)
print(res)
# >>> ['three']
# REMOVE_START
assert res == ['three']
# REMOVE_END
res = r.zrange("myzset", -2, -1)
print(res)
# >>> ['two', 'three']
# REMOVE_START
assert res == ['two', 'three']
r.delete("myzset")
# REMOVE_END
# STEP_END
# STEP_START zrange2
res = r.zadd("myzset", {"one": 1, "two":2, "three":3})
res = r.zrange("myzset", 0, 1, withscores=True)
print(res)
# >>> [('one', 1.0), ('two', 2.0)]
# REMOVE_START
assert res == [('one', 1.0), ('two', 2.0)]
r.delete("myzset")
# REMOVE_END
# STEP_END
# STEP_START zrange3
res = r.zadd("myzset", {"one": 1, "two":2, "three":3})
res = r.zrange("myzset", 2, 3, byscore=True, offset=1, num=1)
print(res)
# >>> ['three']
# REMOVE_START
assert res == ['three']
r.delete("myzset")
# REMOVE_END
# STEP_END