day12装饰器进阶
生活随笔
收集整理的這篇文章主要介紹了
day12装饰器进阶
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.復習
# 復習 # 講作業 # 裝飾器的進階# functools.wraps# 帶參數的裝飾器# 多個裝飾器裝飾同一個函數 # 周末的作業# 文件操作# 字符串處理# 輸入輸出# 流程控制# 裝飾器 # 開發原則 : 開放封閉原則 # 裝飾器的作用 :在不改變原函數的調用方式的情況下,在函數的前后添加功能 # 裝飾器的本質 : 閉包函數# def wrapper(func): # def inner(*args,**kwargs): # print('在被裝飾的函數執行之前做的事') # ret = func(*args,**kwargs) # print('在被裝飾的函數執行之后做的事') # return ret # return inner # # @wrapper #holiday = wrapper(holiday) # def holiday(day): # print('全體放假%s天'%day) # return '好開心' # # ret = holiday(3) # print(ret)# def outer(*args): # print(args) # print(*args) # def inner(*args): # print('inner : ',args) # inner(*args) # # # outer(1,2,3,4) #==outer(*[1,2,3,4]) #==outer(*(1,2,3,4)) View Code?
2.作業
# 1.編寫裝飾器,為多個函數加上認證的功能(用戶的賬號密碼來源于文件), # 要求登錄成功一次,后續的函數都無需再輸入用戶名和密碼 # FLAG = False # def login(func): # def inner(*args,**kwargs): # global FLAG # '''登錄程序''' # if FLAG: # ret = func(*args, **kwargs) # func是被裝飾的函數 # return ret # else: # username = input('username : ') # password = input('password : ') # if username == 'boss_gold' and password == '22222': # FLAG = True # ret = func(*args,**kwargs) #func是被裝飾的函數 # return ret # else: # print('登錄失敗') # return inner # # @login # def shoplist_add(): # print('增加一件物品') # # @login # def shoplist_del(): # print('刪除一件物品') # # shoplist_add() # shoplist_del()# 2.編寫裝飾器,為多個函數加上記錄調用功能,要求每次調用函數都將被調用的函數名稱寫入文件 # def log(func): # def inner(*args,**kwargs): # with open('log','a',encoding='utf-8') as f: # f.write(func.__name__+'\n') # ret = func(*args,**kwargs) # return ret # return inner # # @log # def shoplist_add(): # print('增加一件物品') # # @log # def shoplist_del(): # print('刪除一件物品')# shoplist_add() # shoplist_del() # shoplist_del() # shoplist_del() # shoplist_del() # shoplist_del()# 進階作業(選做): # 1.編寫下載網頁內容的函數,要求功能是:用戶傳入一個url,函數返回下載頁面的結果 # 2.為題目1編寫裝飾器,實現緩存網頁內容的功能: # 具體:實現下載的頁面存放于文件中,如果文件內有值(文件大小不為0),就優先從文件中讀取網頁內容,否則,就去下載,然后存到文件中 import os from urllib.request import urlopen def cache(func):def inner(*args,**kwargs):if os.path.getsize('web_cache'):with open('web_cache','rb') as f:return f.read()ret = func(*args,**kwargs) #get()with open('web_cache','wb') as f:f.write(b'*********'+ret)return retreturn inner@cache def get(url):code = urlopen(url).read()return code# {'網址':"文件名"} ret = get('http://www.baidu.com') print(ret) ret = get('http://www.baidu.com') print(ret) ret = get('http://www.baidu.com') print(ret) View Code?
3.wraps
from functools import wraps def wrapper(func): #func = holiday @wraps(func)def inner(*args,**kwargs):print('在被裝飾的函數執行之前做的事')ret = func(*args,**kwargs)print('在被裝飾的函數執行之后做的事')return retreturn inner@wrapper #holiday = wrapper(holiday) def holiday(day):'''這是一個放假通知'''print('全體放假%s天'%day)return '好開心'print(holiday.__name__) print(holiday.__doc__) ret = holiday(3) #inner print(ret)# def wahaha(): # ''' # 一個打印娃哈哈的函數 # :return: # ''' # print('娃哈哈')# print(wahaha.__name__) #查看字符串格式的函數名 # print(wahaha.__doc__) #document View Code?
4.裝飾器進階
#帶參數的裝飾器 #500個函數 # import time # FLAGE = False # def timmer_out(flag): # def timmer(func): # def inner(*args,**kwargs): # if flag: # start = time.time() # ret = func(*args,**kwargs) # end = time.time() # print(end-start) # return ret # else: # ret = func(*args, **kwargs) # return ret # return inner # return timmer # # timmer = timmer_out(FLAGE) # @timmer_out(FLAGE) #wahaha = timmer(wahaha) # def wahaha(): # time.sleep(0.1) # print('wahahahahahaha') # # @timmer_out(FLAGE) # def erguotou(): # time.sleep(0.1) # print('erguotoutoutou')# wahaha() # erguotou()#多個裝飾器裝飾一個函數 def wrapper1(func):def inner1():print('wrapper1 ,before func')ret = func()print('wrapper1 ,after func')return retreturn inner1def wrapper2(func):def inner2():print('wrapper2 ,before func')ret = func()print('wrapper2 ,after func')return retreturn inner2def wrapper3(func):def inner3():print('wrapper3 ,before func')ret = func()print('wrapper3 ,after func')return retreturn inner3@wrapper3 @wrapper2 @wrapper1 def f():print('in f')return '哈哈哈'print(f())#記錄用戶的登錄情況 #計算這個函數的執行時間 View Code?
轉載于:https://www.cnblogs.com/xingqisan/p/10712556.html
總結
以上是生活随笔為你收集整理的day12装饰器进阶的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Golang 入门 : 数组
- 下一篇: 使用jenkins实现监控嵌入式设备稳定