Skip to content

Commit fff4c12

Browse files
committed
03/26
1 parent 938ec9b commit fff4c12

6 files changed

Lines changed: 170 additions & 0 deletions

File tree

jimchenhub/0001/coupon_code.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
#-*- encoding:utf-8 -*-
3+
'''
4+
生成长度为10的唯一优惠码,由数字和字母组成
5+
id + L + 随机码
6+
'''
7+
import random
8+
import string
9+
10+
def generate_code(id, length=10):
11+
prefix = hex(int(id))[2:] + "L"
12+
length = length - len(prefix)
13+
chars=string.ascii_letters+string.digits
14+
return prefix + ''.join([random.choice(chars) for i in range(length)])
15+
def main():
16+
for i in range(20):
17+
code = generate_code(i)
18+
print code
19+
20+
if __name__=="__main__":
21+
main()

jimchenhub/0002/coupon.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
0L2IFj2oMm
2+
1LwfzIE95r
3+
2LfT3vChTl
4+
3L5YkbNQMw
5+
4LPHA8kegU
6+
5LN68DBVlX
7+
6LLwRFIxrM
8+
7L6a71Sd1h
9+
8LGBuAjL4D
10+
9Lk7yYjFok
11+
aLcpxO3DtU
12+
bLynaJ8QOE
13+
cLsFI8pD3a
14+
dLcmdyFKle
15+
eLr45vcdKl
16+
fLLCUwWOqf
17+
10LYxg37Rr
18+
11LHlWDPBB
19+
12L7NJ3y8Z
20+
13LcD7JDzN
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
#-*- encoding:utf-8 -*-
3+
'''
4+
save coupon code into mysql database
5+
'''
6+
import random
7+
import string
8+
import MySQLdb
9+
10+
config = {
11+
"host":'localhost',
12+
"port" : 3306,
13+
"user":'root',
14+
"passwd":'jim',
15+
"db" :'python'
16+
}
17+
18+
class Save_to_mysql:
19+
def __init__(self, code_file, config):
20+
self.code_file = code_file
21+
self.config = config
22+
23+
def connect_db(self):
24+
try:
25+
conn= MySQLdb.connect(host=self.config["host"], port=self.config["port"], user=self.config["user"], passwd=self.config["passwd"], db=self.config["db"])
26+
except MySQLdb.Error, e:
27+
try:
28+
sqlError = "Error %d:%s" % (e.args[0], e.args[1])
29+
print sqlError
30+
except IndexError:
31+
print "MySQL Error:%s" % str(e)
32+
self.conn = conn
33+
self.cur = conn.cursor()
34+
35+
def save_code_into_db(self):
36+
f = open("./coupon.txt")
37+
for code in f:
38+
print code
39+
sql = "insert into coupon_code(code) values('"+code+"')"
40+
print sql
41+
try:
42+
self.cur.execute(sql)
43+
except MySQLdb.Error, e:
44+
try:
45+
sqlError = "Error %d:%s" % (e.args[0], e.args[1])
46+
print sqlError
47+
except IndexError:
48+
print "MySQL Error:%s" % str(e)
49+
f.close()
50+
self.cur.close()
51+
self.conn.commit()
52+
def close_all(self):
53+
self.conn.close()
54+
55+
def main():
56+
code_file = "./coupon.txt"
57+
mysql = Save_to_mysql(code_file, config)
58+
mysql.connect_db()
59+
mysql.save_code_into_db()
60+
mysql.close_all()
61+
62+
if __name__=="__main__":
63+
main()

jimchenhub/0003/coupon.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
0L2IFj2oMm
2+
1LwfzIE95r
3+
2LfT3vChTl
4+
3L5YkbNQMw
5+
4LPHA8kegU
6+
5LN68DBVlX
7+
6LLwRFIxrM
8+
7L6a71Sd1h
9+
8LGBuAjL4D
10+
9Lk7yYjFok
11+
aLcpxO3DtU
12+
bLynaJ8QOE
13+
cLsFI8pD3a
14+
dLcmdyFKle
15+
eLr45vcdKl
16+
fLLCUwWOqf
17+
10LYxg37Rr
18+
11LHlWDPBB
19+
12L7NJ3y8Z
20+
13LcD7JDzN
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
#-*- encoding:utf-8 -*-
3+
'''
4+
save coupon code into redis
5+
'''
6+
import redis
7+
8+
config = {
9+
"host":'localhost',
10+
"port":6379,
11+
"db":0
12+
}
13+
14+
class Save_to_redis:
15+
def __init__(self, code_file, config):
16+
self.code_file = code_file
17+
self.config = config
18+
19+
def connect_db(self):
20+
self.r = redis.Redis(host=self.config["host"], port = self.config["port"], db=self.config["db"])
21+
22+
def save_code_to_redis(self):
23+
f = open("./coupon.txt")
24+
for code in f:
25+
code = code.strip('\n')
26+
self.r.rpush("coupon_list", code)
27+
f.close()
28+
29+
def show_code_list(self):
30+
print self.r.lrange("coupon_list", 0, -1)
31+
32+
def cleanup(self):
33+
while self.r.llen("coupon_list") :
34+
self.r.lpop("coupon_list")
35+
36+
def main():
37+
code_file = "./coupon.txt"
38+
redis = Save_to_redis(code_file, config)
39+
redis.connect_db()
40+
redis.save_code_to_redis()
41+
redis.show_code_list()
42+
redis.cleanup()
43+
44+
if __name__=="__main__":
45+
main()

jimchenhub/0003/dump.rdb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
REDIS0006�ܳC�Z��V

0 commit comments

Comments
 (0)