python-装饰器作业day12

#第一题:
flag=False
def login(func):
    def inner(*args,**kwargs):
        global flag
        if flag:
            ret=func()
            return ret
        else:
            username=input("username:")
            password=input("password:")
            if username=="orange" and password=='123':
                flag=True
                ret=func()
                return ret
            else:
                print("登陆失败")
    return inner
@login
def shop_add():
    print("增加一个产品")
@login
def shop_del():
    print("减少一个产品")
shop_add()
shop_del()

#第二题
import datetime
def log(func):
    def inner(*args,**kwargs):
        now_time=datetime.datetime.now() #获取当前时间
        with open('log','a',encoding="utf-8") as f:
            f.write(str(now_time)+':'+func.__name__+'\n')
        ret =func()
        return ret
    return inner
@log
def shop_add():
    print("增加一个产品")
@log
def shop_del():
    print("减少一个产品")
shop_add()
shop_del()

#第三题
from urllib.request import urlopen
def get(url):
    code=urlopen(url).read
    return code
ret= get("http://www.baidu.com")
print(ret)

 

上一篇:Java学习总结day12(对象流 & 容器)


下一篇:npm install报错 npm ERR! 的四种解决办法