forked from redis/redis-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdt_list.py
323 lines (253 loc) · 6.7 KB
/
dt_list.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# EXAMPLE: list_tutorial
# HIDE_START
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
# HIDE_END
# REMOVE_START
r.delete("bikes:repairs")
r.delete("bikes:finished")
# REMOVE_END
# STEP_START queue
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
# STEP_END
# REMOVE_START
assert res1 == 1
assert res2 == 2
assert res3 == "bike:1"
assert res4 == "bike:2"
# REMOVE_END
# STEP_START stack
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
# STEP_END
# REMOVE_START
assert res5 == 1
assert res6 == 2
assert res7 == "bike:2"
assert res8 == "bike:1"
# REMOVE_END
# STEP_START llen
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
# STEP_END
# REMOVE_START
assert res9 == 0
# REMOVE_END
# STEP_START lmove_lrange
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
# STEP_END
# REMOVE_START
assert res10 == 1
assert res11 == 2
assert res12 == "bike:2"
assert res13 == ["bike:1"]
assert res14 == ["bike:2"]
r.delete("bikes:repairs")
# REMOVE_END
# STEP_START lpush_rpush
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
# STEP_END
# REMOVE_START
assert res15 == 1
assert res16 == 2
assert res17 == 3
assert res18 == ["bike:important_bike", "bike:1", "bike:2"]
r.delete("bikes:repairs")
# REMOVE_END
# STEP_START variadic
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
# STEP_END
# REMOVE_START
assert res19 == 3
assert res20 == 5
assert res21 == [
"bike:very_important_bike",
"bike:important_bike",
"bike:1",
"bike:2",
"bike:3",
]
r.delete("bikes:repairs")
# REMOVE_END
# STEP_START lpop_rpop
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
# STEP_END
# REMOVE_START
assert res22 == 3
assert res23 == "bike:3"
assert res24 == "bike:1"
assert res25 == "bike:2"
assert res26 is None
# REMOVE_END
# STEP_START ltrim
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
# STEP_END
# REMOVE_START
assert res27 == 5
assert res28 is True
assert res29 == ["bike:5", "bike:4", "bike:3"]
r.delete("bikes:repairs")
# REMOVE_END
# STEP_START ltrim_end_of_list
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
# STEP_END
# REMOVE_START
assert res27 == 5
assert res28 is True
assert res29 == ["bike:3", "bike:4", "bike:5"]
r.delete("bikes:repairs")
# REMOVE_END
# STEP_START brpop
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
# STEP_END
# REMOVE_START
assert res31 == 2
assert res32 == ("bikes:repairs", "bike:2")
assert res33 == ("bikes:repairs", "bike:1")
assert res34 is None
r.delete("bikes:repairs")
r.delete("new_bikes")
# REMOVE_END
# STEP_START rule_1
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
# STEP_END
# REMOVE_START
assert res35 == 0
assert res36 == 3
r.delete("new_bikes")
# REMOVE_END
# STEP_START rule_1.1
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
# STEP_END
# REMOVE_START
assert res37 is True
assert res38 == "string"
r.delete("new_bikes")
# REMOVE_END
# STEP_START rule_2
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
# STEP_END
# REMOVE_START
assert res40 == 1
assert res41 == "bike:3"
assert res42 == "bike:2"
assert res43 == "bike:1"
assert res44 == 0
r.delete("bikes:repairs")
# REMOVE_END
# STEP_START rule_3
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
# STEP_END
# REMOVE_START
assert res45 == 0
assert res46 == 0
assert res47 is None
# REMOVE_END
# STEP_START ltrim.1
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
# STEP_END
# REMOVE_START
assert res48 == 5
assert res49 is True
assert res50 == ["bike:5", "bike:4", "bike:3"]
r.delete("bikes:repairs")
# REMOVE_END