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

    3.23作业

     
    # 一:编写函数,(函数执行的时间用time.sleep(n)模拟)
    # import time
    # def index(x,y) :
    # time.sleep(3)
    # print('index %s %s' %(x,y))

    # 二:编写装饰器,为函数加上统计时间的功能
    # def timmer(func):
    # def wrapper(*args, **kwargs):
    # start = time.time()
    # res = func(*args, **kwargs)
    # stop = time.time()
    # print(stop - start)
    # return res
    # return wrapper


    # 三:编写装饰器,为函数加上认证的功能
    # def auth(func):
    # def wrapper(*args,**kwargs):
    # name=input('your name>>: ').strip()
    # pwd=input('your password>>: ').strip()
    # if name == 'egon' and pwd == '123':
    # res=func(*args,**kwargs)
    # return res
    # else:
    # print('账号密码错误')
    # return wrapper
    #
    # import time
    # @auth
    # def index(x,y) :
    # time.sleep(3)
    # print('index %s %s' %(x,y))
    #
    # index(1,2)
    # #
    # 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
    # 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式

    # login_user={"name":None}
    # dic={}
    # def auth(func):
    # def wrapper(*args,**kwargs):
    # with open('db.txt','r',encoding='utf-8')as f:
    # for line in f:
    # dic=eval(line.strip())
    # if login_user.get("name") :
    # res = func(*args, **kwargs)
    # return res
    # else:
    # inp_name=input('请输入用户名:')
    # inp_pwd=input('请输入密码:')
    # if inp_name ==dic['name'] and inp_pwd== dic['password']:
    # login_user['name']=inp_name
    # res=func(*args,**kwargs)
    # return res
    # return wrapper
    #
    #
    # import time
    # @auth
    # def index(x,y) :
    # time.sleep(3)
    # print('index %s %s' %(x,y))
    #
    # index(1,2)



    # 五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
    import time
    login_user={"name":None}
    start=time.time()
    dic={}
    def auth(func):
    def wrapper(*args,**kwargs):
    with open('db.txt','r',encoding='utf-8')as f:
    for line in f:
    dic=eval(line.strip())
    if login_user.get("name") :
    res = func(*args, **kwargs)
    return res
    else:
    inp_name=input('请输入用户名:')
    inp_pwd=input('请输入密码:')
    if inp_name ==dic['name'] and inp_pwd== dic['password']:
    login_user['name']=inp_name
    res=func(*args,**kwargs)
    stop=time.time()
    lock_time=stop-start
    if lock_time >30:
    inp_name = input('请输入用户名:')
    inp_pwd = input('请输入密码:')
    if inp_name == dic['name'] and inp_pwd == dic['password']:
    res = func(*args, **kwargs)
    return res
    else:
    return res
    return wrapper


    import time
    @auth
    def index(x,y) :
    time.sleep(3)
    print('index %s %s' %(x,y))

    index(1,2)
  • 相关阅读:
    codevs 2632 非常好友
    codevs 1213 解的个数
    codevs 2751 军训分批
    codevs 1519 过路费
    codevs 1503 愚蠢的宠物
    codevs 2639 约会计划
    codevs 3369 膜拜
    codevs 3135 River Hopscotch
    数论模板
    JXOJ 9.7 NOIP 放松模拟赛 总结
  • 原文地址:https://www.cnblogs.com/haliluyafeng/p/12556391.html
Copyright © 2011-2022 走看看