Wait for bloom
时光不语,静待花开
posts - 25,comments - 0,views - 1572

1.用户登录认证

  • 有三个界面需要登录认证通过后才能查看
  • 一个登录函数作为装饰器
  • 调用使用@函数名
  • 调用中如果加参数,则装饰器需要再加一层
复制代码
 1 def login(func):
 2     def desc(*args,**kwargs):
 3         user='test'
 4         passwd ='123456'
 5         n=0
 6         if n<=3:
 7             for i in range(3):
 8                 user_input=input('用户名')
 9                 passwd_input=input('密码')
10                 if user_input==user and passwd_input==passwd:
11                     func(*args,**kwargs)
12                     break
13                 else:
14                     print('输入错误,请重新输入')
15                     n+=1
16     return desc
17 
18 @login
19 def home():
20     print('welcome to home')
21 @login
22 def product():
23     print('welcome to product')
24 @login
25 def solution():
26     print('welcome to solution')
27 
28 home()
29 product()
30 solution()
View Code
复制代码

2.日志操作时间

  • 在三个界面记录操作时间
  • 使用logger日志
复制代码
 1 def logger(func):
 2     '''日志装饰器'''
 3     def desc(*args,**kwargs):
 4         start_time=time.time()
 5         func()
 6         end_time=time.time()
 7         print('操作了%s时间'%(end_time-start_time))
 8     return desc
 9 @logger
10 def home(*args,**kwargs):
11     time.sleep(2)
12     print('welcome to home')
13 @logger
14 def product():
15     time.sleep(2)
16     print('welcome to product')
17 @logger
18 def solution():
19     time.sleep(2)
20     print('welcome to solution')
21 
22 home()
23 product()
24 solution()
View Code
复制代码
posted on   Little-Girl  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示