forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0003.py
More file actions
30 lines (22 loc) · 696 Bytes
/
0003.py
File metadata and controls
30 lines (22 loc) · 696 Bytes
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
# -*- coding: utf-8 -*-
# **第 0003 题:**将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
import redis
import random
import string
forSelect = string.ascii_letters + string.digits
def generate_code(count, length):
for x in range(count):
Re = ""
for y in range(length):
Re += random.choice(forSelect)
yield Re
def save_code():
r = redis.Redis(host='127.0.0.1', port='6379', password='linyii')
codes = generate_code(200, 20)
p = r.pipeline()
for code in codes:
p.sadd('code', code)
p.execute()
return r.scard('code')
if __name__ == '__main__':
save_code()