Python's closure function decorator job

Source: Internet
Author: User
Tags closure wrapper

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

Related Article

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.