本来分开用两个脚本完成,后来发现有很多可多复用的东西,尤其是在python对mysql的操作过程中。然后写可以优化的地方,比如对mysql的操作封装成一个类,就如后面redis的队列类一样。不过懒病犯了,不想动了。简单整合了一下代码,我的心已经飞到办公室外的世界了,准确的是召唤师峡谷。。。
# -*- coding:utf-8 -*-
import MySQLdb
import schedule
import time
import datetime
import random
import string
import redis
# use python to automatic generate the data
# and insert to mysql (db: test; table: test)
class SaveToSql(object):
def __init__(self, conn):
self.conn = conn
def random_str(self, randomlength=random.randint(1,10)):
a = list(string.ascii_letters)
random.shuffle(a)
return ''.join(a[:randomlength])