#一:编写函数,(函数执行时间用time.sleep(n)代表)
#二:编写装饰器,为函数加上统计时间的功能
#三:编写装饰器,为函数加上认证的功能
# import time
#
# def deco1(func1):
# def wrapper1(*args,**kwargs):
# inp_name = input('your name:').strip()
# inp_pwd = input('your password:').strip()
# if inp_name == 'han' and inp_pwd == '123':
# print('login successful')
# res1=func1(*args,**kwargs)
# return res1
# else:
# print('请先登录!')
# return
# return wrapper1
#
#
#
# def deco2(func2):
# def wrapper(*args, **kwargs):
# start_time = time.time()
# func2(*args, **kwargs)
# stop_time = time.time()
# print(stop_time - start_time)
# return wrapper
#
#
# @deco1
# @deco2
# def index(x, y):
# time.sleep(1)
# print('index====>', x, y)
#
# index(1, 2)
# 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
# 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
# import time
#
# def deco1(func1):
# def wrapper1(*args,**kwargs):
# with open('login.txt', 'r', encoding='utf-8') as f:
# login_status = eval(f.readline())
# if login_status['name'] is None:
# inp_name = input('your name:').strip()
# inp_pwd = input('your password:').strip()
# with open('db.txt', 'r', encoding='utf-8') as rf:
# for line in rf:
# name, pwd = line.strip('
').split(':')
# if inp_name == name and inp_pwd == pwd:
# print('login successful')
# with open('login.txt', 'w', encoding='utf-8') as wf:
# res = "{'name':%s,'password':'%s'}
" % (name, pwd)
# wf.write(res)
# break
# else:
# print('请先登录!')
# return
# res1=func1(*args,**kwargs)
# return res1
# else:
# func1(*args,**kwargs)
# return wrapper1
#
#
# @deco1
# def index(x, y):
# time.sleep(1)
# print('index====>', x, y)
#
# index(1, 2)
# 五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
# import time
#
# def deco2(func2):
# def wrapper(*args, **kwargs):
# start_time = time.time()
# func2(*args, **kwargs)
# stop_time = time.time()
# n = stop_time - start_time
# print(n)
# if n > 3:
# with open('login.txt', 'w', encoding='utf-8') as wf:
# res = "{'name':%s,'password':%s}
" % (None, None)
# wf.write(res)
# return wrapper
#
# def deco1(func1):
# def wrapper1(*args,**kwargs):
# with open('login.txt', 'r', encoding='utf-8') as f:
# login_status = eval(f.readline())
# if login_status['name'] is None:
# inp_name = input('your name:').strip()
# inp_pwd = input('your password:').strip()
# with open('db.txt', 'r', encoding='utf-8') as rf:
# for line in rf:
# name, pwd = line.strip('
').split(':')
# if inp_name == name and inp_pwd == pwd:
# print('login successful')
# with open('login.txt', 'w', encoding='utf-8') as wf:
# res = "{'name':'%s','password':'%s'}
" % (name, pwd)
# wf.write(res)
# break
# else:
# print('请先登录!')
# return
# res1=func1(*args,**kwargs)
# return res1
# else:
# func1(*args,**kwargs)
# return wrapper1
#
#
#
# @deco1
# @deco2
# def index(x, y):
# time.sleep(1)
# print('index====>', x, y)
#
# index(1, 2)
#
# @deco1
# @deco2
# def home(x, y):
# time.sleep(4)
# print('index====>', x, y)
#
# home(1, 2)
#六:编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
# import requests
#
# def down():
# keywrod = input('请输入url>>:').strip()
# respone = requests.get('%s'%keywrod,
# headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
# },
# )
# res = respone.status_code
# if res == 200:
# print('download successful')
## print(respone.text)
# else:
# print('url error')
# with open('download.html', 'w', encoding='utf-8') as f:
# f.write(respone.text)
#
# down()
#七:为题目五编写装饰器,实现缓存网页内容的功能:
#具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中
# import os
# import requests
#
# def down():
# b = os.path.getsize("download.html")
# if b != 0:
# with open('download.html','rb') as rf:
# print(rf.read())
# else:
# keywrod = input('请输入url>>:').strip()
# respone = requests.get('%s'%keywrod,
# headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
# },
# )
# res = respone.status_code
# if res == 200:
# print('download successful')
# # print(respone.text)
# else:
# print('url error')
# with open('download.html', 'w', encoding='utf-8') as f:
# f.write(respone.text)
#
# down()
#扩展功能:用户可以选择缓存介质/缓存引擎,针对不同的url,缓存到不同的文件中
#不会
#八:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作
# func_dic={}
#
# def outter(name):
# def wrapper(func):
# func_dic[name] = func
# return wrapper
#
# @outter('login')
# def login():
# print('登录功能......')
#
# @outter('withdraw')
# def withdraw():
# print('提现功能......')
#
# @outter('transfer')
# def transfer():
# print('转账功能......')
#
# @outter('recharge')
# def recharge():
# print('充值功能')
#
# print(func_dic)
#九 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到日志文件中,日志文件路径可以指定
# 注意:时间格式的获取
# import time
# time.strftime('%Y-%m-%d %X')
import time
def outter(func):
def wrapper(*args, **kwargs):
func(*args, **kwargs)
with open('access.log', 'at', encoding='utf-8') as f:
t = time.strftime('%Y-%m-%d %H:%M:%S')
n =f'{t} f1 run
'
f.write(n)
return wrapper
@outter
def index(x, y):
time.sleep(1)
print('index==>',x,y)
index(1, 2)