python学习之有参装饰器与迭代器作业
1、编写课上讲解的有参装饰器准备明天默写
def auth(db_type):
def deco(func):
def wrapper(*args,**kwargs):
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
if db_type == 'file':
print('基于文件的验证')
if inp_name == 'egon' and inp_pwd == '123':
res = func(*args,**kwargs)
return res
else:
print('账号或密码错误。')
elif db_type == 'mysql':
print('基于mysql的验证')
elif db_type == 'ldap':
print('基于ldap的验证')
else:
print('不支持该db_type')
return wrapper
return deco
@auth(db_type='file')
def index(x,y):
print('from index->> {} and {}'.format(x,y))
@auth(db_type='mysql')
def home(name):
print('from home->> {}'.format(name))
@auth(db_type='ldap')
def transfer():
print('from transfer')
index(1,2)
home('vincent')
transfer()
2:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作
cmd_dic = {'0':['退出',exit]}
def func_dic(cmd_dic,cmd_num,usage):
def deco(func):
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
return res
cmd_dic[cmd_num] = [usage,wrapper]
return wrapper
return deco
@func_dic(cmd_dic,'1','注册')
def register():
print('注册功能。')
@func_dic(cmd_dic,'2','登录')
def login():
print('登录功能。')
@func_dic(cmd_dic,'3','查询余额')
def check_balance():
print('查询余额功能。')
@func_dic(cmd_dic,'4','提现')
def withdraw():
print('提现功能。')
@func_dic(cmd_dic,'5','转账')
def transfer():
print('转账功能。')
while True:
for k,v in cmd_dic.items():
print('{:>4}: {}'.format(k,v[0]))
cmd = input('请输入命令编号:').strip()
if not cmd.isdigit():
print('必须输入数字。')
continue
if cmd in cmd_dic:
cmd_dic[cmd][1]()
else:
print('你输入的命令不存在,请重新输入。')
3、 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到日志文件中,日志文件路径可以指定
注意:时间格式的获取
import time
time.strftime('%Y-%m-%d %X')
import time
def run_log(log_path):
def deco(func):
def wrapper(*args,**kwargs):
run_time = time.strftime('%Y-%m-%d %X')
res = func(*args,**kwargs)
with open(r'{}'.format(log_path),'at',encoding='utf8') as f:
f.write('{} {} run\n'.format(run_time,str(func).split()[1]))
return res
return wrapper
return deco
@run_log(log_path='run_time.log')
def f1(x,y):
time.sleep(3)
print('from f1—>>> {} and {}'.format(x,y))
f1('AA','BB')
4、基于迭代器的方式,用while循环迭代取值字符串、列表、元组、字典、集合、文件对象
1)字符串
str1 = 'addesdels'
str1_iterator = iter(str1)
while True:
try:
print(next(str1_iterator))
except StopIteration:
break
2)列表
list1 = [2,3,4,5,6,7]
list1_iterator = iter(list1)
while True:
try:
print(next(list1_iterator))
except StopIteration:
break
3)元组
tup1 = (11,22,33,44,55,66)
tup1_iterator = iter(tup1)
while True:
try:
print(next(tup1_iterator))
except StopIteration:
break
4)字典
dic1 = {'name':'egon','age':18,'gender':'male'}
dic1_iterator = iter(dic1)
while True:
try:
print(next(dic1_iterator))
except StopIteration:
break
5)集合
s1 = {111,222,333,444,555,777}
s1_iterator = iter(s1)
while True:
try:
print(next(s1_iterator))
except StopIteration:
break
6)文件对象
with open('db.txt','rt',encoding='utf8') as f:
while True:
try:
print(next(f),end='')
except StopIteration:
break
5、自定义迭代器实现range功能
def my_range(start,stop,step=1):
while start<stop:
yield start
start += step
for i in my_range(2,10):
print(i)