码迷,mamicode.com
首页 > 编程语言 > 详细

python16 习题

时间:2020-03-23 22:01:29      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:运行   choice   min   格式   装饰器   ota   pen   sleep   err   

一:编写函数,(函数执行的时间用time.sleep(n)模拟)
二:编写装饰器,为函数加上统计时间的功能
三:编写装饰器,为函数加上认证的功能

四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
注意:从文件中读出字符串形式的字典,可以用eval(‘{"name":"egon","password":"123"}‘)转成字典格式

五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录

六:选做题
思考题(选做),叠加多个装饰器,加载顺序与运行顺序,可以将上述实现的装饰器叠加起来自己验证一下
@deco1 # index=deco1(deco2.wrapper的内存地址)
@deco2 # deco2.wrapper的内存地址=deco2(deco3.wrapper的内存地址)
@deco3 # deco3.wrapper的内存地址=deco3(index)
def index():

import time


# 1
def sum_number(x, y):
    sum = x + y
    time.sleep(3)
    return sum


# 2
def timer(sum_number):
    def wrapper(*args, **kwargs):
        start = time.time()
        sum = sum_number(*args, **kwargs)
        end = time.time()
        total_time = end - start
        print(total_time)
        return sum

    return wrapper


# sum_number=timer(sum_number)
# print(sum_number(3,4))

# 3
def login(sum_number):
    name = input(‘请输入账户:‘)
    pwd = input(‘请输入密码:‘)
    username = ‘zw‘
    userpwd = ‘1993‘
    if name == username and pwd == userpwd:
        def wrapper1(*args, **kwargs):
            start = time.time()
            res = sum_number(*args, **kwargs)
            end = time.time()
            total_time = end - start
            print(total_time)
            return res

        return wrapper1
    else:
        print(‘error‘)


# sum_number = login(sum_number)
# print(sum_number(2, 4))
# 4


def timer1(sum_number1):
    def wrapper(*args, **kwargs):
        start = time.time()
        sum1 = sum_number1(*args, **kwargs)
        end = time.time()
        total_time = end - start
        print(total_time)
        return sum1

    return wrapper


def timer2(sum_number2):
    def wrapper(*args, **kwargs):
        start = time.time()
        sum2 = sum_number2(*args, **kwargs)
        end = time.time()
        total_time = end - start
        print(total_time)
        return sum2

    return wrapper


@timer1
def sum_number1(x, y):
    sum1 = x - y
    time.sleep(3)
    return sum1


@timer2
def sum_number2(x, y):
    sum2 = x * y
    time.sleep(3)
    return sum2

def logout():
    tag1=0
    return tag1


def auth():
    name=input(‘请输入账户:‘)
    pwd=input(‘请输入密码:‘)
    with open(r‘C:\users\administrator\desktop\db.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f1:
        for line in f1:
            username=line.strip().split(‘:‘)[0]
            userpwd=line.strip().split(‘:‘)[1]
            if name==username:
                if pwd==userpwd:
                    print(‘登录成功‘)
                    choice()
                else:
                    print(‘密码错误‘)


def choice():
    dic={
‘1‘:‘运算1‘,
‘2‘:‘运算2‘,
‘0‘:‘退出‘
    }
    print(dic)
    cmd=input (‘请输入命令:‘)
    list = [sum_number1,sum_number2]
    if not cmd in dic:
        print(‘输入有误‘)
    else:
        cmd = int(cmd)
        if cmd==0:
            logout()
        else:
            para1=input(‘请输入第一个参数:‘)
            para2=input(‘请输入第二个参数:‘)
            para1=int(para1)
            para2=int(para2)
            list[cmd](para1,para2)


print(auth())

#5
def auth_time():
    tag=1
    while tag<2:
        name=input(‘请输入账户:‘)
        pwd=input(‘请输入密码:‘)
        with open(r‘C:\users\administrator\desktop\db.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f1:
            for line in f1:
                username=line.strip().split(‘:‘)[0]
                userpwd=line.strip().split(‘:‘)[1]
                if name==username:
                    if pwd==userpwd:
                        print(‘登录成功‘)
                        begin=time.time()
                        tag1=1
                        while tag1:
                            choice()
                            stop=time.time()
                            if stop-begin>20:
                                print(‘登录超时,请重新登录‘)
                                tag=1
                            else:
                                tag+=1
                        else:
                            print(‘密码错误‘)

python16 习题

标签:运行   choice   min   格式   装饰器   ota   pen   sleep   err   

原文地址:https://www.cnblogs.com/Franciszw/p/12555300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!