python第十二课

#复习
#作业
#装饰器的进阶
#functools.wraps
#带参数的装饰kkd
#多个装饰器装饰同一个函数
#装饰器
#开发原则: 开放封闭原则
#装饰器的作用:在不改变原函数的调用方式的情况下,在函数前后添加功能
#装饰器的本质:闭包函数
# from functools import wraps
# def wrapper(func): #func=holiday
# @wraps(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 '爱你启丽'
#
# print(holiday.__name__)
# print(holiday.__doc__)
# ret=holiday(3) #inner
# print(set)
#
# # def outer(*args,**kwargs):
# # 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))
#
# #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 == '222222':
# 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()

#1.编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
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 ret
return 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)
#生成器函数
# def generator():
# print(1)
# return 'a'
# ret = generator()
# print(ret)

#只要含有yield关键字的函数都是生成器的函数
#yield 不能和return工用且需要写在函数内
# def generator():
# print(1)
# yield 'a'
# #生成器函数:执行之后会得到一个生成器作为返回值
# ret = generator()
# print(ret)
# print(ret.__next__())

# def generator():
# print(1)
# yield 'a'
# print(2)
# yield 'b'
# g=generator()
# for i in g:
# print(i)
# ret=g.__next__()
# print(ret)
# ret=g.__next__()
# print(ret)

#娃哈哈%i
# def wahaha():
# for i in range(100):
# yield '娃哈哈%s'%i
#
# g=wahaha()
# count=0
# for i in g:
# count +=1
# print(i)
# if count>50:
# break
# #print('*******',g.__next__())
# count=0
# for i in g:
# count +=1
# print(i)
# if count>100:
# break

#带参数的装饰器
#500个函数
import time
FLAG=True
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(FLAG)
@timmer_out(FLAG) #wahaha=timmer(wahaha)
def wahaha():
time.sleep(0.1)
print('wahawaha')
@timmer_out(FLAG)
def erguotou():
time.sleep(0.1)
print('ergoutoutoutotu')

wahaha()
erguotou()
def tail(filename):
f=open('file',encoding='utf-8')
while True:
line=f.readline()
if line.strip():
yield line.strip()
g=tail('file')
for i in g:
if '启丽' in i:
print('我喜欢',i)


猜你喜欢

转载自www.cnblogs.com/huangjianfeng/p/11334678.html