Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8108
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-20 13:38
个人简介

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules.

文章分类

全部博文(8)

文章存档

2015年(8)

我的朋友
最近访客

分类: Python/Ruby

2015-02-09 12:14:29

#!/usr/bin/env python
#-*- coding:utf-8 -*-

__author__ = "richardGaoPy"

import random
import string

COUPON_NUM = 200
COUPON_STR_LENGTH = 8

def mysqlopt(lst):
    try:
        import MySQLdb
        db = MySQLdb.connect("127.0.0.1",'root','bigbird','remy')
        cursor = db.cursor()
        cursor.execute("create table zcmgl(id int primary key,regist_num varchar(8))")
        #i=1
        for i in xrange(0,200):
            cursor.execute("insert into zcmgl values(%s,%s)",(i,lst[i]))
        db.commit()
        cursor.close()
        db.close()
        
    except:
        print "Save data failed!"
    
def redisopt(lst):
    """对redis的操作非常不熟悉、要加强一下;建立redis连接写入数据"""
    try:
        import redis
        r = redis.StrictRedis(host="localhost",port="6379",db=0)
        for x in xrange(0,200):
            r.set(x,lst[x])
    except:
        print "Save data failed to redis."
        
if __name__ == "__main__":
    """随机产生注册码、并存入mysql和redis缓存中"""
    lst = list(string.letters)[26:] + list(string.digits)
    zc_lst = []
    for i in xrange(COUPON_NUM):
        coupon_list = [random.choice(lst) for i in range(COUPON_STR_LENGTH)]
        coupon_str = "".join(coupon_list)
        zc_lst.append(coupon_str)
    mysqlopt(zc_lst)
    redisopt(zc_lst)
阅读(271) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~