1)python,現在用過的,後面推出每日練習文法與深度挖掘應用

來源:互聯網
上載者:User

標籤:文檔   串連   uila   mysq   text   推出   基本   items   var   

python1, Mac 系統內建的python路徑/System/Library/Frameworks/Python.framework/Version裡面存放多個版本可通過:啟動pythonimport sysprint sys.path 查看路徑/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip  2, mac 裡面可以啟動idle建立.py 檔案,然後開啟cmd用Mac內建的 執行 python 路徑/.py檔案windows裡面可以用notepad++ 編寫,然後 ctrl + f5配置一下,作為run 3,mac 不支援漢字支援https://www.jianshu.com/p/8333f08ec87d 4,自動發送郵件 http://www.runoob.com/python/python-email.html#! /usr/bin/env python#! /usr/bin/env python# coding=utf-8import sysimport osimport urllibimport urllib2import unittestimport jsonimport time reload(sys)sys.setdefaultencoding("utf-8") from email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport smtplib def sendmail():msg = MIMEMultipart()#att = MIMEText(open(r‘C:\Users\alice02.yang\Desktop\brandid.txt‘, ‘rb‘).read(), ‘base64‘, ‘gb2312‘)#att["Content-Type"] = ‘application/octet-stream‘#att["Content-Disposition"] = ‘attachment; filename="brandid.txt"‘#msg.attach(att) strTo = (‘[email protected]‘,‘[email protected]‘)msg[‘to‘] = ‘;‘.join(strTo)msg[‘from‘] = ‘[email protected]‘msg[‘subject‘] = ‘brandid‘ try:server = smtplib.SMTP()server.connect(‘smtp.163.com‘)server.login(‘[email protected]‘,‘yzh1990a1a1‘)server.sendmail(msg[‘from‘], strTo,msg.as_string())server.quit()print ‘finish‘except Exception, e:print str(e) if __name__ == ‘__main__‘:sendmail()  linux --> cmd 使用crontab -e然後 編寫 下午六點20發送 20 18 * * * cd /Users/vip/pythonTestScript; python testAutoSendMail.py 退出  對被反垃圾郵件網關隔離郵件允許存取的操作步驟:http://wiki.corp.vipshop.com/pages/viewpage.action?pageId=35029549&src=search  開始→程式→附件→系統工具→計劃任務→開啟新增工作計劃。進入任務計劃嚮導。 利用“任務計劃”,可以將任何指令碼、程式或文檔安排在某個最方便的時間運行。“任務計劃”在每次啟動 Windows XP 的時候啟動並在後台運行。 5,串連資料庫操作linux 要安裝MySQLdb外掛程式 import MySQLdb def conncur():conn=MySQLdb.connect(host=‘10.198.184.184‘,user=‘root‘,passwd=‘admin‘,port=3306,charset=‘GBK‘)cur=conn.cursor()return conn,cur def conSql():try:conn,cur=conncur()cur.execute(‘drop database if exists vip_test_pythonMysql‘)cur.execute(‘create database if not exists vip_test_pythonMysql‘)conn.select_db(‘vip_test_pythonMysql‘)#sqlfile=open(r‘D:\vipd.sql‘)#sql=sqlfile.readlines() #sqlfile.close() #for line in sql:#cur.execute(line)#conn.commit()cur.close()conn.close()except MySQLdb.Error,e:print "Mysql Error %d: %s" % (e.args[0], e.args[1]) if __name__ == ‘__main__‘:conSql() 6,多線程# coding:gbkimport timeimport urllibimport urllib2import threadingfrom Queue import Queuefrom time import sleep    #THREAD_NUM = 10#ONE_WORKER_NUM = 500#LOOP_SLEEP = 0.01  THREAD_NUM = 100ONE_WORKER_NUM = 10LOOP_SLEEP = 0.5  PERF_TEST_URL=‘‘ ERROR_NUM = 0 def get(url,para):url_value = urllib.urlencode(para)fullurl=url+‘?‘+url_valueprint fullurldata=urllib2.urlopen(fullurl,timeout=20)json=data.read()return json    def doWork(index):t = threading.currentThread()#print "["+t.name+" "+str(index)+"] "+PERF_TEST_URL try:url=‘http://10.199.144.188/vips-mobile-operation-center-api/cms/content/list/get_rule_resp‘para= build_para()json = get(url,para)print jsonexcept urllib2.URLError, e:print "["+t.name+" "+str(index)+"] "print eglobal ERROR_NUMERROR_NUM += 1  def working():t = threading.currentThread()print "["+t.name+"] Sub Thread Begin" i = 0while i < ONE_WORKER_NUM:i += 1doWork(i)sleep(LOOP_SLEEP) print "["+t.name+"] Sub Thread End" def build_para():para={}para[‘mid‘]=‘123456789‘para[‘function‘]=‘testfunction‘para[‘appname‘]=‘testapp‘para[‘warehouse‘]=‘VIP_SH‘para[‘tag‘]=‘C10‘para[‘client‘]=‘ipad‘para[‘areaid‘]=‘103101‘para[‘mchannel‘]=‘10‘return para  def main():t1 = time.time()Threads = [] for i in range(THREAD_NUM):t = threading.Thread(target=working, name="T"+str(i))t.setDaemon(True)Threads.append(t)for t in Threads:t.start()for t in Threads:t.join() print "main thread end"t2 = time.time()print "========================================"print "URL:", PERF_TEST_URLprint "task num:", THREAD_NUM, "*", ONE_WORKER_NUM, "=", THREAD_NUM*ONE_WORKER_NUMprint "total time cost:", t2-t1print "per req time cost( sec):", (t2-t1) / (THREAD_NUM*ONE_WORKER_NUM)print "req per sec:", 1 / ((t2-t1) / (THREAD_NUM*ONE_WORKER_NUM))print "error num:", ERROR_NUM  if __name__ == "__main__":main() 7,單元測試操作52944782 寫好TestCase,然後由TestLoader載入TestCase到TestSuite,然後由TextTestRunner來運行TestSuite,啟動並執行結果儲存在TextTestResult中,我們通過命令列或者unittest.main()執行時,main會調用TextTestRunner中的run來執行,或者我們可以直接通過TextTestRunner來執行用例。import unittestclass TestCdi(unittest.TestCase):def setUp(self):conSql() #------------------------across hit one-----------------------------#------------------------hit Challenger-----------------------------def testbroute_2(self): if __name__ == ‘__main__‘:unittest.main() 每個測試方法均以  開頭,否則是不被unittest識別的 8,post/get請求操作 #! /usr/bin/env python# coding=utf-8import urllib2import urllibimport httplibimport jsonurl=‘http://10.199.144.226/vips-infra-cdi-rule-engine-service/cdi_rule_engine_service/get_cdi_resp/‘para={}para={}var={}f1=open(r"C:\Users\alice02.yang\Desktop\my.txt",‘w‘)para["function"]="Recommend"para["appName"]="ADS"#var["mid"]="123456789"var["warehouse"]="VIP_NH"#var["tag"]="B"var["mid"]="11111111111"#var["client"]="iphone"#var["areaid"]="103107"#var["mchannel"]="1"var["user_id"]="12345"#var["version"]="2.11"para["variables"]=varurl_v = urllib.urlencode(para)url_value=url_v.replace("%27",‘%22‘)fullurl=url+‘?‘+url_valueheaders = {"Content-Type":"application/x-www-form-urlencoded","Connection":"Keep-Alive","Referer":"http://10.199.144.226/vips-infra-cdi-rule-engine-service/cdi_rule_engine_service/get_cdi_resp/"}print fullurlconn = httplib.HTTPConnection("10.199.144.226");conn.request("POST","/vips-infra-cdi-rule-engine-service/cdi_rule_engine_service/get_cdi_resp/",url_value,headers);response = conn.getresponse() print response.read()f1.close()conn.close()  import hashlibimport jsonimport unittestimport urllib2 def getmd5code(src):#print srcm1 = hashlib.md5()m1.update(src)return m1.hexdigest()    9,串連redis叢集操作 #! /usr/bin/env python# coding=utf-8 import sysimport osimport jsonimport getoptimport urllib2from urllib import urlopenfrom rediscluster import StrictRedisCluster  reload(sys)sys.setdefaultencoding( "utf-8" ) # URL = "http://rank-admin.vip.vip.com/rank/category/findCategoryListInfoByTreeId"# DATAURL = "http://rank-admin.vip.vip.com/rank/redis/findBigDataFormRedisCluster"# ticket = "ST-328962-SqdDyIf25geXGIlfdxhj-cas.oa.vipshop.com"# DELETE_DATA_URL = "".join([URL, "?ticket=", ticket, "&treeId=160"])# INFO_DATA_URL = "".join([DATAURL, "?ticket=", ticket, "&key="]) URL = "http://rank-admin.vip.vip.com/rank/category/findCategoryListInfoByTreeId"DATAURL = "http://rank-admin.vip.vip.com/rank/redis/findBigDataFormRedisCluster"opener = urllib2.build_opener()opener.addheaders.append((‘Cookie‘,‘_const_cas_assertion_=ST-454039-HGKO3KBGkZiVpAgLCb2x-cas.oa.vipshop.com‘))DELETE_DATA_URL = "".join([URL, "?treeId=117"])INFO_DATA_URL = "".join([DATAURL, "?key="]) f1=open(r‘C:\Users\alice02.yang\Desktop\ss\117mappingId.txt‘,‘w‘) def redis_cluster(key,value):print keyprint valueredis_nodes = [{‘host‘:‘10.198.184.184‘,‘port‘:6379},{‘host‘:‘10.198.184.184‘,‘port‘:6380},{‘host‘:‘10.198.184.185‘,‘port‘:6379},{‘host‘:‘10.198.184.185‘,‘port‘:6380},{‘host‘:‘10.198.184.186‘,‘port‘:6379},{‘host‘:‘10.198.184.186‘,‘port‘:6380}]try:redisconn = StrictRedisCluster(startup_nodes=redis_nodes)#rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)except Exception,e:print "Connect Error!"sys.exit(1)try:redisconn.hmset(key,value)except:passprint "sssss"print redisconn.hgetall(key)  def rank():catIdList=[]#print DELETE_DATA_URL#content = urlopen(DELETE_DATA_URL).read()content = opener.open(DELETE_DATA_URL).read()print contentcontent = json.loads(content)datas = content["data"];#print datasfor data in datas:for i in data[‘nodes‘]:try:for j in i[‘nodes‘][0][‘mappingCatIds‘]:catIdList.append(str(j))print i[‘nodes‘][0][‘mappingCatIds‘]except:passreturn catIdList   def getBigDataInfo(cat):adict = {}for i in cat:warehouse=["VIP_SH", "VIP_CD", "VIP_HZ", "VIP_BJ", "VIP_NH"]#warehouse=["VIP_ZZ", "VIP_ZZKG", "VIP_GZNS", "VIP_JC", "HT_GZZY", "HT_GZFLXY", "VIP_NBJCBS", "HT_NBYC"]for wareh in warehouse:key = str(i) + ":" + str(wareh) + ":3105"print key#print INFO_DATA_URL + keybigDataInfo = urlopen(INFO_DATA_URL + key).read()bigDataInfo = json.loads(bigDataInfo)bigdatas = bigDataInfo["data"];try:print bigdatas["value"]adict[key]=bigdatas["value"]except:passreturn adict#for k in bigdatas["value"]:#print k#redis_cluster(key,k,bigdatas["value"][k])  def main():cat = rank()#cat=[7494,1018]print catprint >>f1,"\n".join(cat)#adict = getBigDataInfo(cat)#for key,value in adict.items():#print key#print value#redis_cluster(key,value) if __name__ == "__main__":main() 10,excel表格操作 import xlrdimport jsondata = xlrd.open_workbook(r‘C:\Users\alice02.yang\Desktop\tt.xlsx‘)table = data.sheet_by_name(u‘Sheet1‘)a=table.col_values(6)b=table.col_values(7)dics={}result=[]for i in range(len(a)):dics={}print a[i]if len(a[i])==0:breakdics[‘name‘]=a[i]dics[‘value‘]=b[i]print dicsresult.append(dics)print resultdict_res=json.dumps(result,ensure_ascii=False)print dict_res.decode(‘utf-8‘).encode(‘gbk‘) 11,基本操作http://www.runoob.com/python/python-json.html 異常:=(,"w" fhwrite"這是一個測試檔案,用於測試異常!!" :print"Error: 沒有找到檔案或讀取檔案失敗"else fhclose import osimport timeimport datetimeoverdueName=list()oneDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 1))oneDayAgoTimeStamp = int(time.mktime(oneDayAgo.timetuple()))def del_files(path):for root , dirs, files in os.walk(path): for name in files:if name.endswith(".xls"):timeStr = name.split(".xls")[0].split("cdi_rule_")[1];timeStrToInt = int(time.mktime(time.strptime(timeStr,‘%Y-%m-%d‘)))if timeStrToInt <= oneDayAgoTimeStamp:overdueName.append(name)if len(overdueName)>=10:for temp in overdueName:os.remove(os.path.join(path, temp))print ("Delete File: " + os.path.join(path, temp)) if __name__ == "__main__":#path = ‘/tmp‘path = ‘E:‘del_files(path)

1)python,現在用過的,後面推出每日練習文法與深度挖掘應用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.