Automated testing section Tenth---Multithreading, inheritance, List derivation, e-mail

Source: Internet
Author: User

1. List deduction:

Example: Generate an odd list of 100 or less

"If I is a number, the value I get after the For loop processing is passed through to the numbers list"
names=[' Nyy ', ' wxc ', 1234,[' aaa ', ' Danny '
Number=[i for I in range (1,100) if i%2!=0]
Print (number)

2. Inheritance
Class Axfarther (object):
def __init__ (SELF,OP):
Self.op=op
Print (' Parent class: ', op)
def Makemoney (self):
Print ("1000W")
# axf=axfarther (' smoking, drinking ') #打印 "smoking, drinking"

# #重写父类的构造方法
Class Ax (Axfarther):
def __init__ (Self,op,code):
Print (' Subclass: ', code)
Self.code = code
def Makemoney (self): #重写父类的方法
Print (' Subclass method: ', ' 2000W ')
Ax=ax (' Smoking, drinking ', ' python ') #打印 "subclass: Python"
Ax.makemoney () #打印 "Subclass Method: 2000W"

#修改父类构造方法, add a new feature based on the parent class construction method
Class Ax (Axfarther):
def __init__ (Self,op,code):
axfarther.__init__ (SELF,OP) #先把原来的调用一下, there is the function of the parent class, if you want to modify the construction method of the parent class, first call the following construction method of the parent class, the classic class must write this, the new class two kinds of lines
# super (ax,self). __init__ (OP) #作用同上, Super is this class, super will automatically find the parent class construction method
Print (' Subclass: ', code)
Self.code=code
def Makemoney (self): #重写父类的方法
Print (' Subclass method: ', ' 2000W ')
Ax=ax (' Smoking, drinking ', ' Python ') #打印父类: Smoking, Drinking/n sub-category: Python
Ax.makemoney () #打印 "Subclass Method: 2000W"

3. Multithreading
#程序一运行, first there's a main thread
#循环创建10个子线程, let the 10 sub-threads run the AXB function.
#主线程继续走, print ' Game Over '
Import threading,time# Each program has a main thread by default
def AXB (name):
# Time.sleep (1)
Print (' hahaha ', name)
Print (Time.strftime ('%y%m%d%h%m%s ', Time.localtime ())) #同时启动, time consistent
For I in range: #循环启动十个线程, execute function AXB, child thread, threading start are child threads
t = Threading. Thread (TARGET=AXB, args= (i,)) # Instantiates a thread, starts a thread
T.start ()
Print (' Game Over ')

#怎么获取到多线程执行的函数里面的返回值
Import time, requests,threading
Run_times=[]
Objs=[]
def blog (URL):
S_time=time.time ()
R=requests.get (URL). text
E_time=time.time ()
Run_time=e_time-s_time
Run_times.append (Run_time)
Url= ' http://www.nnzhp.cn/archives/527 '
For I in range: #通过多线程指定运行的函数, cannot get the return value of the function
T=threading. Thread (target=blog,args= (URL,))
T.start ()
Objs.append (t)
For obj in Objs: #等待所有子线程执行完
Obj.join ()
Avg=sum (run_times)/len (run_times)
Print ("Average time is:", avg)
/span>
4. Multi-process
From multiprocessing import Process
Import time
def test (i):
Time.sleep (1)
Print (i)
If __name__== ' __main__ ':
For I in range (10):
p = Process (target=test, args= (i,))
P.start ()
5. Daemon Thread
#主线程执行完了, it ends up together, regardless of whether the child thread has finished executing
Import time,threading
def test ():
Time.sleep (2)
Print (' hahaha ')
For I in range (5):
T=threading. Thread (Target=test)
T.setdaemon (True) #设置子线程为守护线程 (the main thread in this program ends after the child thread is established, so nothing is printed)
T.start ()
6. Lock
 # from threading import Lock 
a=0
# lock=lock () #申请一把锁
def Test ():
Global a
# Lock.acquire () #加锁, Python3 can not be manually locked
a+=1
# lock.release () #解锁, Python3 can not be unlocked manually
import Threa Ding
#多线程在处理一个任务的时候 and manipulate a variable
for I in range (+):
T = Threading. Thread (target=test)
T.start ()
Print (a)

7, Example: Save multiple html-multithreading-crawler
Import Requests,threading,time
def write_html (url,name):
r = Requests.get (URL)
With open (name, ' W ', encoding= ' Utf-8 ') as FW:
Fw.write (R.text)
urls=[' www.nnzhp.cn ', ' besttest.cn ', ' www.imdsx.cn ', ' sb.nnzhp.cn ', ' bbs.besttest.cn ']
OBJS = [] #存放每个线程
Start_time = Time.time ()
For URL in URLs:
New_url = ' http//' +url
file_name = url+ '. html ' #www. nnzhp.cn.html
t = Threading. Thread (target=write_html,args= (new_url,file_name))
Objs.append (t)
T.start ()
# T.join () #主线程等待
#.join is the main thread waiting for each sub-thread to finish executing.
#1, start 10 threads and let them run
#2, main thread, and so on.
# for obj in OBJS:
# print (' obj per time ', obj)
# Obj.join ()
# write_html (new_url,file_name)
End_time = Time.time ()
Print (' program runs in total ', end_time-start_time)
# cases
# A.xls B.xls C.xls
8. E-mail-with attachments
Import Smtplib
From Email.mime.text import Mimetext
From Email.mime.multipart import Mimemultipart
# from Email.header Import header
def send_mail (sender,pwd,receiver,content,subject,mailhost= ' smtp.qq.com ', port=465):
msg = Mimemultipart ()
File = ' A.txt '
ATT = mimetext (open (file, encoding= ' UTF8 '). read ()) # Sent Attachment object
att["Content-type"] = ' application/octet-stream '
att["content-disposition"] = ' attachment;filename= "%s" '% file
Msg.attach (ATT) # Add attachments to the message
Msg.attach (content) # message body content added to MSG mail object (mimetext)
msg[' Subject ']=subject
msg[' from ']=sender
msg[' to ']=receiver

Smtp=smtplib. Smtp_ssl (Mailhost,port)
Smtp.login (SENDER,PWD)
Smtp.sendmail (sender,receiver,msg.as_string ())
Smtp.quit ()
Print (' Email send success. ')

Sender = ' XXX ' # sender Account
PWD = ' XXX ' # sender password
Receiver = ' XXX '
Subject = ' Test message header '
Content = ' Here is the message '
Send_mail (Sender,pwd,receiver,content,subject)
    
9, send regular mail
/span>
 import smtplib 
from email.mime.text import mimetext
def send_mail (sender, Pwd,receiver,content,subject,mailhost= ' smtp.qq.com ', port=465):
Mail=mimetext (content)
mail[' subject ']= Subject
mail[' from ']=sender
mail[' to ']=receiver
Smtp=smtplib. Smtp_ssl (Mailhost,port)
Smtp.login (sender,pwd)
Smtp.sendmail (sender,receiver,mail.as_string ())
Smtp.q Uit ()
Print (' Email send success. ')

sender = ' xxx ' # sender account
pwd = ' xxx ' # sender password
receiver = ' xxx '
subject = ' Test message header '
content = ' Here is the message content ' send_mail (sender,pwd,receiver,content,subject)
                 
  

/span>

Automated testing section Tenth---Multithreading, inheritance, List derivation, e-mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.