這個Python腳本,讓工作自動向你彙報進度!

前言

本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。

作者:讀芯術

PS:如有需要Python學習資料的小夥伴可以加點擊下方鏈接自行獲取

http://t.cn/A6Zvjdun

筆者經常編寫Python腳本來進行數據處理、數據傳輸和模型訓練。隨着數據量和數據複雜性的增加,運行腳本可能需要一些時間。在等待數據處理完成時可以同時做一些其他工作。

爲了達到這個目的,筆者編寫了一組用於解決這個問題的Python腳本。使用這些腳本向手機發送流程更新、可視化和完成通知。當偶爾擁有這些自由的時刻,你可以享受而不是擔心模型的進度。

需要什麼

第一個問題是,需要知道什麼?這取決於你正在做的工作。對於筆者來說主要有有三個可能會佔用時間的處理任務:

  • 模型訓練
  • 數據處理和/或傳輸
  • 金融模型

我們需要對於每一種情況具體分析。

模型訓練

更新每n個epoch,必須包括關鍵指標。例如,訓練和驗證集的損失和準確性。接着完成通知,包括:

  • 訓練期間關鍵指標的可視化(同樣,訓練和驗證集的損失和準確性)
  • 其他不太重要但仍然有用的信息,如本地模型目錄、訓練時間、模型架構等
  • 預測輸出,對於文本生成來說輸出生成的文本(或它的一個樣本);對於圖像生成來說,輸出結果是一個(希望)很酷的可視化

以訓練一個神經網絡來重現給定的藝術風格爲例。我們需要重點從模型中生成的圖像,損失和精度圖,當前的訓練時間,和一個模型的名稱。

import notify
             START= datetime.now()  # this line would be placed before modeltraining begins
      MODELNAME="SynthwaveGAN"  # giving us ourmodel name
      NOTIFY=100  # so we send an update notification every100 epochs
             # for each epoch e,we would include the following code
      if e % notify_epoch ==0and e !=0:
          # here we createthe email body message
         txt = (f"{MODELNAME} update as of "
                f"{datetime.now().strftime( %H:%M:%S )}.")
                 # we build theMIME message object with notify.message
         msg = notify.message(
             subject= Synthwave GAN ,
             text=txt,
             img=[
                 f ../visuals/{MODELNAME}/epoch_{e}_loss.png ,
                 f ../visuals/{MODELNAME}/epoch_{e}_iter_{i}.png 
             ]
         )  # note that weattach two images here, the loss plot and
          #    ...a generated image output from our model
                    notify.send(msg)  # we then send the message

每隔100個epoch,就會發送一封包含上述所有內容的電子郵件。以下是其中一封郵件:
在這裏插入圖片描述
數據處理和傳輸

這點不是很有趣,但在時間消耗方面,它排第一名。

以使用Python將批量數據上傳到SQLServer爲例(對於沒有BULK INSERT的人)。在上傳腳本的最後,會有一個簡單的消息通知上傳完成。

import os
      import notify
      from data importSql  # seehttps://jamescalam.github.io/pysqlplus/lib/data/sql.html
             dt =Sql( database123 ,  server001 )  # setup theconnection to SQL Server
             for i, file inenumerate(os.listdir( ../data/new )):
          dt.push_raw(f ../data/new/{file} )  # push a file to SQL Server
             # once the upload is complete, send a notification
      # first we create the message
      msg = notify.message(
          subject= SQL Data Upload ,
          text=f Data upload complete, {i} filesuploaded. ,
      )
             # send the message
      notify.send(msg)

如果偶爾拋出錯誤,還可以添加一個try-except語句來捕獲錯誤,並將其添加到一個列表中,以包含在更新和/或完成電子郵件中。

金融模型

金融建模中運行的所有東西實際上都非常快,所以此處只能提供一個示例。

以現金流動模型工具爲例。現實中,這個過程只需要10-20秒,但現在假設你是華爾街炙手可熱的定量分析師,正在處理幾百萬筆貸款。在這封電子郵件中,可能想要包含一個高級概要分析的投資組合。可以隨機選擇一些貸款,並在給定的時間段內可視化關鍵值——給定一個小樣本來交叉檢驗模型的性能。

end = datetime.datetime.now()  # get the ending datetime
             # get the total runtime in hours:minutes:seconds
                      hours,rem =divmod((end - start).seconds, 3600)
                      mins,secs =divmod(rem, 60)
                      runtime= {:02d}:{:02d}:{:02d} .format(hours, mins,secs)
             # now built our message
                      notify.msg(
                          subject="Cashflow Model Completion",
                          text=(f {len(model.output)} loansprocessed. 
                                f Total runtime:{runtime} ),
                          img=[
                               ../vis/loan01_amortisation.png ,
                               ../vis/loan07_amortisation.png ,
                               ../vis/loan01_profit_and_loss.png ,
                               ../vis/loan07_profit_and_loss.png 
                          ]
                      )
             notify.send(msg)  # and send it

代碼

上面的所有功能節選自一個名爲notify.py的腳本。在示例代碼中將使用Outlook。這裏需要兩個Python庫,email和smtplib:

  • email:用於管理電子郵件消息。有了這個庫就可以設置電子郵件消息本身,包括主題、正文和附件。
  • smtplib:處理SMTP連接。簡單郵件傳輸協議(SMTP)是大多數電子郵件系統使用的協議,允許郵件通過互聯網發送。
    在這裏插入圖片描述

MIME

消息本身是使用來自email模塊的MIMEMultipart對象構建的。還需要使用三個MIME子類,並將它們附加到MIMEMultipart對象上:

  • mimetext:這將包含電子郵件“有效負載”,即電子郵件正文中的文本。
  • mimeimage :這是用於在電子郵件中包含圖像。
  • mimeapplication:用於MIME消息應用程序對象。也就是文件附件。

除了這些子類之外,還有其他參數,比如MimeMultipart中的Subject值。所有這些加在一起就形成了下面的結構。

把它們都放在一起會發生什麼:
在這裏插入圖片描述

import os
      from email.mime.text importMIMEText
      from email.mime.image importMIMEImage
      from email.mime.application importMIMEApplication
      from email.mime.multipart importMIMEMultipart
             defmessage(subject="PythonNotification", text="", img=None, attachment=None):
          # build messagecontents
          msg =MIMEMultipart()
          msg[ Subject ] = subject  # add in thesubject
          msg.attach(MIMEText(text))  # add text contents
                 # check if wehave anything given in the img parameter
          if img isnotNone:
              # if we do, wewant to iterate through the images, so let s check that
              # what we haveis actually a list
              iftype(img) isnot list:
                  img = [img]  # if it isn t alist, make it one
              # now iteratethrough our list
              for one_img in img:
                  img_data =open(one_img,  rb ).read()  # read the imagebinary data
                  # attach theimage data to MIMEMultipart using MIMEImage, we add
                  # the givenfilename use os.basename
                  msg.attach(MIMEImage(img_data, name=os.path.basename(one_img)))
                 # we do the samefor attachments as we did for images
          if attachment isnotNone:
              iftype(attachment) isnot list:
                  attachment = [attachment]  # if it isn t a list, make it one
                          for one_attachment in attachment:
                  withopen(one_attachment, rb ) as f:
                      # read in theattachment using MIMEApplication
                      file =MIMEApplication(
                          f.read(),
                          name=os.path.basename(one_attachment)
                      )
                  # here we editthe attached file metadata
                  file[ Content-Disposition ] =f attachment; filename="{os.path.basename(one_attachment)}" 
                  msg.attach(file)  # finally, addthe attachment to our message object
          return msg

這個腳本相當簡單。在頂部,有導入(這是以前介紹過的MIME部分)以及Python的os庫。

接下來定義一個名爲message的函數。這允許使用不同的參數調用函數並輕鬆地構建一個電子郵件消息對象。例如,可以這樣寫一封帶有多張圖片和附件的郵件:


email_msg= message(
    text="Model processing complete,please see attached data.",
    img=[ accuracy.png ,  loss.png ],
    attachments=[ data_in.csv ,  data_out.csv ]
)

首先,初始化MIMEMultipart對象,並將其分配給msg;然後,使用“subject”鍵設置電子郵件主題。attach方法向MIMEMultipart對象添加不同的MIME子類。你可以使用MIMEText子類添加電子郵件正文。

對於圖像img和attachment附件,可以什麼都不傳遞,只傳遞一個文件路徑,或者傳遞一組文件路徑。我們通過首先檢查參數是否爲None來處理它,如果參數爲None,則傳遞;否則,檢查給定的數據類型,不是一個list,就創建一個,這就使得可以用下面的for循環來遍歷項。

接着,使用MIMEImage和MIMEApplication子類分別附加圖像和文件。使用os.basename從給定的文件路徑中獲取文件名,附件名包括該文件名。

SMTP

現在已經構建好電子郵件消息對象,下一步就是發送它。這就是smtplib模塊發揮作用的地方。代碼同樣非常簡單,只有一處例外。

由於直接處理不同的電子郵件供應商和他們各自的服務器,需要不同的SMTP地址。在谷歌中輸入“outlook smtp”。不需要單擊頁面,你就可以得到服務器地址smtp-mail.outlook.com和端口號587。

當使用smtplib.SMTP初始化SMTP對象時,這兩種方法都需要用。SMTP -接近開始的send函數:

import smtplib
       import socket
             defsend(server= smtp-mail.outlook.com , port= 587 , msg):
           # contain followingin try-except in case of momentary network errors
           try:
               # initialiseconnection to email server, the default is Outlook
               smtp = smtplib.SMTP(server, port)
               # this is the Extended Hello  command, essentially greeting our SMTP or ESMTP server
               smtp.ehlo()
               # this is the Start Transport Layer Security  command, tells the server we will
               # becommunicating with TLS encryption
               smtp.starttls()
                          # read email andpassword from file
               withopen( ../data/email.txt ,  r ) as fp:
                   email = fp.read()
               withopen( ../data/password.txt ,  r ) as fp:
                   pwd = fp.read()
                        # login tooutlook server
               smtp.login(email, pwd)
               # sendnotification to self
               smtp.sendmail(email, email, msg.as_string())
               # disconnectfrom the server
               smtp.quit()
           except socket.gaierror:
               print("Network connection error, email notsent.")

smtp.ehlo()和smtp.starttls()都是SMTP命令。ehlo(擴展Hello)本質上是向服務器打招呼。starttls通知服務器,將使用加密傳輸級別安全(TLS)連接進行通信。

在此之後,只需從文件中讀取電子郵件和密碼,分別存儲在email和pwd中。然後,使用smtp.login登錄到SMTP服務器。登錄並使用smtp.sendmail發送電子郵件。

筆者總是給自己發送通知,但在自動報告的情況下,可能希望將電子郵件發送到其他地方。爲此,我會更改destination_address: smtp.sendmail(email、destination_address、 msg.as_string)。

最後,終止會話並關閉與smtp.quit的連接。

將所有這些都放在try-except語句中。在瞬間網絡連接丟失的情況下,將無法連接到服務器。導致socket.gaierror。使用try-except語句可以防止程序在網絡連接失敗的情況下崩潰。

筆者將其用於ML模型培訓更新和數據傳輸完成。如果郵件沒有被髮送,那也沒有關係。這種簡單、被動地處理連接丟失是合適的。

現在已經寫了代碼的兩部分,我們就可以發送電子郵件了:

 # builda message object
msg = message(text="See attached!", img= important.png ,
             attachment= data.csv )send(msg)  #send the email (defaults to Outlook)

這是所有的電子郵件通知和/或使用Python的自動化的全過程。感謝email和smptlib庫,設置過程變得非常容易。
在這裏插入圖片描述
還有一點請注意,公共電子郵件提供程序服務器地址和TLS端口。

對於任何耗費大量時間的處理或訓練任務,進度更新和完成通知通常纔是真正的解放。Python,讓工作更美好!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章