Why use an adorner?
Sometimes write a piece of code, over a period of time need to upgrade it, add some new features, but if you want to directly modify the original code will affect other people's call, so you need a not to modify the source code and do not modify the original function of the invocation of things and can add new functionality to the original function of the thing, the adorner is doing this
Two what is an adorner
The utensil of another person can be any callable object, and the adorner can be any callable object. The principle of the adorner is emphasized: 1 does not modify the source code of the adorned object 2 does not modify the object that is being decorated by the adorner's target: Add a new feature to the adorned object, following the premise of 1 and 2
Open closure principle: Closed to modification, open to expansion
use of three adorners
The following is an adorner for a function that adds the ability to calculate its run time:
1 Import Time 2 def timmer (func): 3 def wrapper (*args,**kwargs): 4 start_time=time.time () 5 res=func (*args, **kwargs) 6 stop_time=time.time () 7 print (' Run time is%s '% (stop_time-start_time)) 8 return res 9 return WRAPPER10 @timmer12 def foo (): time.sleep (3) print (' from foo ') + foo ()
Four, the decorator syntax and fixed format
1 def Adorner function name (func): 2 def wrapper (*args,**kwargs): 3 ret = func (*args,**kwargs) 4 return Ret5 return WRAPPER6 7 @ Adorner function name 8 def func (): 9 Pass
Five, homework practice
1 ###################################### #作业练习 ####################################################### 2 # Job: 3 # 1. To write the adorner, for a number of functions with the authentication function (the user's account password from the file), required to log on successfully once, the subsequent functions do not need to enter the user name and password 4 # user_exist = [False] 5 # def auth (func): 6 # def Wrapp ER (*args,**kwargs): 7 # #注册功能 8 # with open (' Db.txt ', ' R ', encoding= ' Utf-8 ') as F:9 # User_dic = eval (F.read ()) # flag = False11 # While not user_exist[0]:12 # username = input (' Please enter your User name: '). Strip () # password = input (' Please enter your password: '). Strip () # if username in user_dic and password = = user_dic[username]:15 # print (' Congratulations, login successful! ') # User_exist[0] = True17 # break18 # else:19 # print (' Account or Password error, please re-enter! ') # ret = func (*args,**kwargs) # return ret22 # return wrapper23 #24 # @auth25 # def func1 (): 26 # Print (' function 1 ') # @auth28 # def FUNC2 (x): # Print (' function 2 ', x) # func1 () 31# FUNC2 (111111) 32 # 2. Write adorners to add a record call function to multiple functions, requiring each call function to write the called function name to the file. # def log (func): # def wrapper (*args,**kwargs): 35 # with open (' Db2.txt ', ' a ', encoding= ' Utf-8 ') as f:36 # f.write ('%s ' function is being called. \ n '%func.__name__) Notoginseng # ret = func (*args,**kwargs) # return ret39 # return Wrapper40 # @log41 # def F Unc1 (): # print (' func1 function was called .... ') # @log44 # def FUNC2 (): # print (' Func2 function was called .... ') #47 # func1 () # Func2 () 49 # Advanced Job (optional): 50 # 1. Write a function to download the content of the Web page, the function is: the user passed in a URL, the function returns the results of the download page from urllib.request Import LOPEN52 # def get_html (URL): Urlopen (URL). Read ()) in #56 # get_html (' http://www.baidu.com ') 57 # 2. Writing decorations for topic 1 To cache the content of the Web page: 58 # Specific: The implementation of the download page is stored in the file, if the file has a value (the file size is not 0), the priority is to read the page content from the file, otherwise, to download, and then save to the file in the Urllib.request import URLOPEN60 # Get_bak (func): # def wrapper (*args,**kwargs): The # with open (' Html.bak ', ' r+ ', encoding= ' u Tf-8 ') as f:64 # if not F.read (): $ # ret = func (*args, **kwargs) # print (ret) # F.write (Ret.decode (' Utf-8 ')) # else:69 # Print (' The following is obtained from the cache ^ ^^ ^^ ') # f.seek (0) # Print (F.read ()) # ret = Fu NC (*args, **kwargs) # return ret74 # return wrapper75 # # @get_bak77 # def get_html (URL): # return Urlopen (URL). Read () # get_html (' http://www.python.org ')
11th Day of Python learning