day14

# 一:编写函数,(函数执行时间用time.sleep(n)代表)
def index(x,y):
    time.sleep(1)
    print(x,y)
index(1,2)


# 二:编写装饰器,为函数加上统计时间的功能
import time
def deco1(func1):
    def wrapper1(*args,**kwargs):
        start_time = time.time()
        res1=func1(*args,**kwargs)
        stop_time = time.time()
        print(stop_time - start_time)
        return res1
    return wrapper1
@deco1
def index(x,y):
    time.sleep(1)
    print(x,y)
index(1,2)
# 三:编写装饰器,为函数加上认证的功能
import time
def deco2(func2):
    def wrapper2(*args, **kwargs):
        inp_name = input('username>>>: ').strip()
        inp_pwd = input('password>>>: ').strip()
        if inp_name == "egon" and inp_pwd == "123":
            print('login successful')
            res2 = func2(*args, **kwargs)
            return res2
        else:
            print('username or password error')
    return wrapper2
@deco2
def index(x,y):
    time.sleep(1)
    print(x,y)
index(1,2)
# 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
# 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
def outter(engine = 'file'):
    def deco2(func2):
        def wrapper2(*args,**kwargs):
            inp_name = input('username>>>: ').strip()
            inp_pwd = input('password>>>: ').strip()
            if engine == "file":
                print('基于file的认证')
                with open('a.txt', r, encoding='utf-8') as f:
                    for line in f:
                        f_name, f_pwd = line.strip(' ').split(':')
                        if name == f_name and pwd == f_pwd:
                            print('登录成功')
                        res = func(*args, **kwargs)
                        return res
                        else:
                        print('账户名或密码错误')
        return wrapper

            elif engine == 'mysql':
                print('基于mysql的认证')
            elif engine == 'ldap':
                print('基于ldap的认证')
            else:
                print('未知的engine')
        return wrapper2
    return deco2

@outter(engine='mysql')
def index(x,y):
    print(x,y)
index(1,2)
# 五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
#
# 六:编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
#
# 七:为题目五编写装饰器,实现缓存网页内容的功能:
# 具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中
#
# 扩展功能:用户可以选择缓存介质/缓存引擎,针对不同的url,缓存到不同的文件中
#
# 八:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作
#
# 九 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到日志文件中,日志文件路径可以指定
# 注意:时间格式的获取
# import time
# time.strftime('%Y-%m-%d %X')

你可能感兴趣的:(python)