forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0002.py
More file actions
88 lines (63 loc) · 2.3 KB
/
0002.py
File metadata and controls
88 lines (63 loc) · 2.3 KB
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
#coding=utf-8
import uuid
import MySQLdb
"""
002, 将 0001 题生成的 200 个激活码(或者优惠券)保存到 **MySQL** 关系型数据库中
"""
class ActivationCode(object):
def __init__(self, code_count, database, username, host='localhost', port=3306, password=''):
self._host = host
self._username = username
self._password = password
self._database = database
self._port = port
self.codes = self._generate_activation_code(code_count)
#print self.codes
def _get_mysql_instance(self):
params = {
'host': self._host,
'user': self._username,
'passwd': self._password,
'db': self._database,
'port': self._port,
}
return MySQLdb.connect(**params)
def _generate_activation_code(self, count):
code_list = []
for i in xrange(count):
code = str(uuid.uuid4()).replace('-', '').upper()
if not code in code_list:
code_list.append(code)
return code_list
def store_to_mysql(self):
if self.codes:
conn = self._get_mysql_instance()
try:
cur = conn.cursor()
# clear old datas
cur.execute('delete from code')
# insert mutilple code
for code in self.codes:
cur.execute("insert into code(code) values('%s')" % code)
conn.commit()
cur.close()
conn.close()
return True
except MySQLdb.Error,e:
conn.rollback()
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
return False
def print_activation_code(self):
conn = self._get_mysql_instance()
try:
cur = conn.cursor()
cur.execute('select code from code')
results = cur.fetchall()
for row in results:
print row[0]
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
if __name__ == "__main__":
active_code = ActivationCode(200, database='Test', username='root')
if active_code.store_to_mysql():
active_code.print_activation_code()