zoukankan      html  css  js  c++  java
  • 3.24作业

    # 1、编写课上讲解的有参装饰器准备明天默写
    def auth(db_type):
    def deco(func):
    def wrapper(*args, **kwargs):
    name = input('请输入名字: ').strip()
    pwd = input('请输入密码: ').strip()
    if db_type == 'file':
    if name == 'egon' and 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('index->>%s:%s' % (x, y))

    @auth(db_type='mysql')
    def home(name):
    print('home->>%s' % name)


    @auth(db_type='ldap') # 账号密码的来源是ldap
    def transfer():
    print('transfer')


    # index(1,2)
    # home(111)

    # 2:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明
    # 一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作
    dic={}
    k=0
    def add(key,value):
    def outter(func):
    def wrapper(*args,**kwargs):
    res=func(*args,**kwargs)
    dic[key]=value
    return res
    return wrapper
    return outter

    @add(1,2)
    def index():
    pass

    # print(index())
    # print(dic)
    # 3、 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到
    # 日志文件中,日志文件路径可以指定
    # 注意:时间格式的获取
    # import time
    # time.strftime('%Y-%m-%d %X')


    import time
    def log(func):
    def wrapper(*args,**kwargs):
    res=func(*args,**kwargs)
    with open('log.txt','a',encoding='utf-8')as f:
    f.write(f'2017-07-21 11:12:11 f1 run')
    return res
    return wrapper

    @log
    def f1():
    pass

    # f1()

    # 4、基于迭代器的方式,用while循环迭代取值字符串、列表、元组、字典、集合、文件对象
    # str="abcde"
    # str_iterator=iter(str)
    # while 1:
    # try:
    # print(next(str_iterator))
    # except StopIteration:
    # break


    # l=[1,2,3]
    # l_iterator=iter(l)
    # while 1:
    # try:
    # print(next(l_iterator))
    # except StopIteration:
    # break


    # t=(11,22,33)
    # t_iterator=iter(t)
    # while 1:
    # try:
    # print(next(t_iterator))
    # except StopIteration:
    # break



    # dic={'a':1,'b':2}
    # dic_iterator=iter(dic)
    # while 1:
    # try:
    # print(next(dic_iterator))
    # except StopIteration:
    # break

    # set1={1,2,3}
    # set1_iterator=iter(set1)
    # while 1:
    # try:
    # print(next(set1_iterator))
    # except StopIteration:
    # break


    with open('log.txt','r',encoding='utf-8')as f:
    for line in f:
    f_iterator=iter(line)
    while 1:
    try:
    print(next(f_iterator))
    except StopIteration:
    break



    # 5、自定义迭代器实现range功能
    def my_range(start,stop,step=1):
    while start < stop:
    yield start
    start+=step

    for i in my_range(1,5):
    print(i)
  • 相关阅读:
    java内存区域模型
    Java类加载器(双亲委派模型)(综述)
    2.无重复字符的最长子串
    浅谈Java中的对象和引用
    OSI七层协议大白话解读
    MPLS
    计算机网络为什么要分为五层结构?其依据是什么?
    前端技巧小结
    移动端尺寸新写法-rem
    编写高质量代码:Web前端开发修炼之道(四)
  • 原文地址:https://www.cnblogs.com/chenyoupan/p/12561788.html
Copyright © 2011-2022 走看看