Python牛刀小试(四)--代码解析(邮件发送功能)

1.工具类python

点击(此处)折叠或打开

  1. # -*-coding=utf-8-*-
  2. __author__ = 'zhangshengdong'
  3. '''
  4. 个人技术博客:http://blog.chinaunix.net/uid/26446098.html
  5. 联系方式: sdzhang@cashq.ac.cn
  6. '''
  7. class Toolkit():                          #工具类,后面会调用,用于分割例3的配置文件。

  8.     @staticmethod
  9.     def getUserData(cfg_file):
  10.         f=open(cfg_file,'r')
  11.         account={}
  12.         for i in f.readlines():
  13.             ctype,passwd=i.split('=')
  14.             #print ctype
  15.             #print passwd
  16.             account[ctype.strip()]=passwd.strip()
  17.         return account

2.价格比较策略代码

点击(此处)折叠或打开

  1. #-*-coding=utf-8-*-
  2. __author__ = 'zhangshengdong'
  3. '''
  4. 个人技术博客:http://blog.chinaunix.net/uid/26446098.html
  5. 联系方式: sdzhang@cashq.ac.cn
  6. '''
  7. import os
  8. import sys
  9. import smtplib
  10. import tushare as ts
  11. from Ztoolkit import Toolkit as TK
  12. from email.mime.text import MIMEText
  13. from email import Encoders, Utils

  14. reload(sys)
  15. sys.setdefaultencoding('utf8')

  16. class MailSend():
  17.     def __init__(self, smtp_server, from_mail, password, to_mail):   #邮箱初始化配置的函数
  18.         self.server = smtp_server
  19.         self.username = from_mail.split("@")[0]
  20.         self.from_mail = from_mail
  21.         self.password = password
  22.         self.to_mail = to_mail

  23.     def send_txt(self, name, real_price,price, percent, status):    #信息内容排版的函数
  24.         if 'up' == status:
  25. # content = 'stock name:%s current price: %.2f higher than price up:%.2f , ratio:%.2f' % (name, real_price,price, percent)
  26.              content = '证券名称:%s 当前价格: %.2f 高于 定价模型上限价格:%.2f , 可卖出 预计盈利率:%.2f' %(name, real_price,price, percent+15)
  27.         if 'down' == status:
  28. # content = 'stock name:%s current price: %.2f lower than price down:%.2f , 盈利率:%.2f' % (name, real_price,price, percent)
  29.              content = '证券名称:%s 当前价格: %.2f 低于 定价模型下限价格:%.2f , 可买入 预计盈利率:%.2f' %(name, real_price,price, percent+15)
  30.         content = content + '%'
  31.         print content
  32.         subject = '%s' % name
  33.         self.msg = MIMEText(content, 'plain', 'utf-8')
  34.         self.msg['to'] = self.to_mail
  35.         self.msg['from'] = self.from_mail
  36.         self.msg['Subject'] = subject
  37.         self.msg['Date'] = Utils.formatdate(localtime=1)
  38.         try:

  39.             self.smtp = smtplib.SMTP_SSL(port=465)
  40.             self.smtp.connect(self.server)
  41.             self.smtp.login(self.username, self.password)
  42.             self.smtp.sendmail(self.msg['from'], self.msg['to'], self.msg.as_string())
  43.             self.smtp.quit()
  44.             print "sent"
  45.         except smtplib.SMTPException, e:
  46.             print e
  47.             return 0

  48. def push_msg(name, real_price,price, percent, status):            #信息推送的函数
  49.     cfg = TK.getUserData('data.cfg')
  50.     from_mail = cfg['from_mail']
  51.     password = cfg['password']
  52.     to_mail = cfg['to_mail']
  53.     obj = MailSend('smtp.qq.com', from_mail, password, to_mail)   ##这里调用了上述MailSend类
  54.     obj.send_txt(name,real_price, price, percent, status)

  55. def read_stock(name):
  56.     f = open(name)
  57.     stock_list = []

  58.     for s in f.readlines():
  59.         s = s.strip()
  60.         row = s.split(';')
  61.          #print row
  62.         print "code :",row[0]
  63.         print "price_down :",row[1]
  64.         print "price_up :",row[2]
  65.         stock_list.append(row)
  66.     return stock_list

  67. def compare_price(code, price_down, price_up):
  68.     try:
  69.         df = ts.get_realtime_quotes(code)
  70.     except Exception, e:
  71.         print e
  72.         time.sleep(5)
  73.         return 0
  74.     real_price = df['price'].values[0]
  75.     name = df['name'].values[0]
  76.     print "stock name :",name
  77.     real_price = float(real_price)
  78.     print "current price :",real_price
  79.     pre_close = float(df['pre_close'].values[0])
  80.     print "close price :",pre_close
  81.     if real_price >= price_up:
  82.         percent_up = (real_price - price_up) / price_up * 100
  83.         print 'percent : %.2f\n' % (percent_up),
  84.         print '%s real_price %.2f higher than price_up %.2f , %.2f' % (name, real_price, price_up,percent_up),
  85.         print '%'
  86.         push_msg(name, real_price,price_up, percent_up, 'up')
  87.         return 1
  88.     if real_price = price_down:
  89.         percent_down = (price_down - real_price) / price_down * 100
  90.         print '%s real_price %.2f lower than price_down %.2f , %.2f' % (name, real_price, price_down,percent_down),
  91.         print '%'
  92.         push_msg(name, real_price,price_down, percent_down, 'down')
  93.         return 1

  94. def main():
  95.     #read_stock('price.txt')
  96.     # choice = input("Input your choice:\n")

  97.     # if str(choice) == '1':
  98.         stock_lists_price = read_stock('price.txt')
  99.         while 1:
  100.             t = 0
  101.             for each_stock in stock_lists_price:
  102.                 code = each_stock[0]
  103.                 price_down = float(each_stock[1])
  104.                 price_up = float(each_stock[2])
  105.                 t = compare_price(code, price_down, price_up)
  106.                 if t:
  107.                     stock_lists_price.remove(each_stock)

  108. if __name__ == '__main__':
  109.     path=os.path.join(os.getcwd(),'data')
  110.     if os.path.exists(path)==False:
  111.          os.mkdir(path)
  112.     os.chdir(path)

  113.     main()
3.邮箱配置文件

点击(此处)折叠或打开

  1. from_mail=xxxxxxx@qq.com
  2. password=xxxxxxxx
  3. to_mail=xxxxxxx@qq.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值