One: Write the function (the time of the function execution is random)
Import Random
Def t ():
Time.sleep (Random.randrange (1,3))
print (' hello ')
Two: Write the adorner, add the function of statistic time
Import time
import Random
def Timebe (func):
Start_time=time.time ()
res = func (*args, **kwargs)
End_time= Time.time ()
print ( "Run time is:%s '% ( End_time-start_time)
return res
return wrapper
@timebe
def t ():
Time.sleep (Random.randrange (2, 3))
print ( ' hello ')
Three: Writing adorners, adding authentication functions to functions
Def ident (func):
def wrapper (*args,**kwargs):
Name=Input' Name: ')
Pwd=Input if name== " Zuo ' and pwd== " 123 ':
print ( ' correct ')
Span style= "color: #000080; Font-weight:bold ">return func (*args, **kwargs)
else:
print ( ' wrong ')
return wrapper
@ident
def t ():
print ( ' hello ')
Four: To write the adorner, for a number of functions and authentication function (the user's account password from the file), required to log on successfully once, subsequent functions do not need to enter the user name and password
Note: Reading a dictionary from a file as a string can be converted to a dictionary format using eval (' {' name ': ' Egon ', ' Password ': ' 123 '} ')
Five: 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
Six: For the title five to write adorners, to achieve the function of caching Web content:
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
Seven: Remember we use the concept of function object, make a function dictionary operation, come, we have a bigger approach, declare an empty dictionary at the beginning of the file, and then add the adorner before each function,
Complete the auto-add to dictionary operation
Python's closure function decorator job