We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 23dbc24 commit b4e216eCopy full SHA for b4e216e
darcycool/0003/store_into_redis.py
@@ -0,0 +1,30 @@
1
+# -*- coding: utf-8 -*-
2
+# 第 0003 题: 将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
3
+import random
4
+import string
5
+import redis
6
+
7
8
+def id_generator(size=20, chars=string.ascii_uppercase + string.digits):
9
+ return ''.join(random.choice(chars) for _ in range(size))
10
11
12
+def get_activation_code(size=200, len=20):
13
+ activation_code = set()
14
+ for i in range(size):
15
+ activation_code.add(id_generator(len))
16
+ return activation_code
17
18
19
+def save_codes(codes):
20
+ r = redis.Redis(host='127.0.0.1',
21
+ port=6379,
22
+ db=0)
23
+ pipeline = r.pipeline()
24
+ for c in codes:
25
+ pipeline.sadd('code', c)
26
+ pipeline.execute()
27
+ return r.scard('code')
28
29
30
+save_codes(get_activation_code(200, 20))
0 commit comments