python 装饰器实现_Python学习之路:装饰器实现终极版
网站实现验证功能装饰器:
import time
user,passwd='alex','abc123'
def auth(func):
def wrapper(*args,**kwargs):
print("wraper func args:",*args,**kwargs)
username=input("Username:").strip()
password=input("Password:").strip()
if user==username and passwd==password:
print("\033[32;1mUser has passed authentication\033[0m")
func(*args,**kwargs)#
#print("---after authentication---")#保留要装饰函数home的输出结果
#return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
return wrapper
def index():
print("welcome to index page")
@auth
def home():
print("welcome to home page ")
return "from home"
@auth
def bbs():
print("welcome to bbs page")
index()
home()
print(home())#执行结果为空,调用home相当于调用wraper
bbs()
保留要装饰函数的返回结果:
import time
user,passwd='alex','abc123'
def auth(func):
def wrapper(*args,**kwargs):
print("wraper func args:",*args,**kwargs)
username=input("Username:").strip()
password=input("Password:").strip()
if user==username and passwd==password:
print("\033[32;1mUser has passed authentication\033[0m")
res=func(*args,**kwargs)#
print("---after authentication---")#保留要装饰函数home的输出结果
return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
return wrapper
def index():
print("welcome to index page")
@auth
def home():
print("welcome to home page ")
return "from home"
@auth
def bbs():
print("welcome to bbs page")
index()
home()
print(home())#执行结果为空,调用home相当于调用wraper
bbs()
不同网页不同验证方式的装饰器:
import time
user,passwd='alex','abc123'
def auth(auth_type):
print("auth func:",auth_type)
def outer_auth(func):
def wrapper(*args,**kwargs):
print("wraper func args:",*args,**kwargs)
if auth_type=="local":
username=input("Username:").strip()
password=input("Password:").strip()
if user==username and passwd==password:
print("\033[32;1mUser has passed authentication\033[0m")
res=func(*args,**kwargs)#
print("---after authentication---")#保留要装饰函数home的输出结果
return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
elif auth_type=="ldap":
print("搞毛线ldap,不会。。。。")
return wrapper
return outer_auth
def index():
print("welcome to index page")
@auth(auth_type="local")
def home():
print("welcome to home page ")
return "from home"
@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page")
index()
home()
print(home())#执行结果为空,调用home相当于调用wraper
bbs()
python 装饰器实现_Python学习之路:装饰器实现终极版相关推荐
- python正则表达式生成器_Python学习之路-装饰器生成器正则表达式
装饰器 通俗的讲,装饰器就是在不改变源代码基础上,给源代码增加新功能. 不改变函数的源代码.调用方式.返回值等,给函数增加新功能. 经典案例:登录装饰器,def login_decorator(fun ...
- python写前端代码_python学习之路前端-JavaScript
JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚本是通过嵌入在HTML中来实现 ...
- python簇状柱形图_python学习之路--可视化利器matplotlib(上)
前几期已经把读写数据.数据预处理等介绍完了,今天我们接着介绍一个可视化的库matplotlib,虽说现在已经有了更为高级的可视化库,如seaborn,ploty,pyecharts等,但是matplo ...
- python可视化双向箭头_python学习之路--可视化利器matplotlib(下)
上一期我们介绍了matplotlib作图的基本思维,也对相关参数以及一些简单的图表进行示例演示,今天继续用matplotlib绘制一些既实用又有趣的图表.另外还有朋友问到能不能介绍一下pyechart ...
- python文件下载速度 装饰器_python学习笔记之---装饰器
# -*- coding:utf-8 -*- '''示例1: 最简单的函数,表示调用了两次''' def myfunc(): print ("myfunc() called.") ...
- python常用的装饰器库_Python中的各种装饰器详解
Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...
- python na不显示 占位_Python学习之路—Python基础(一)
前言 在Python学习之路-初识Python一文中,简单的介绍了Python的基本信息,搭建了Python的基础运行环境,这篇文档将主要展现一些Python的入门必备内容,如有错误或遗漏的地方,欢迎 ...
- python从入门到_Python学习路线从入门到上手,如何快速Python学习?
因为清晰易读的风格,广泛的适用性,Python已经成为最受欢迎的编程语言之一.在TIOBE 排行榜中位居第四,是名副其实的人工智能第一语言. 风靡的另一个原因是,Python有非常多的第三方库.比如用 ...
- 退出所有循环_Python学习之路9—循环的总结
距离上一次的学习,我们学习了whlie循环与whlie循环嵌套(点击蓝色的字就能查看对应的文章). 今天继续学习循环. 一.while...else 语法是: while 条件: 条件成立重复执行el ...
- python官方手册笔记_Python学习手册笔记
Python学习手册(原书第4版)Mark Lutz 著 第一部分 使用入门 >> orthogonal >> 交互提示模式下编写代码 >> "%&quo ...
最新文章
- 使用代码生成建立可扩展序列化器(上)
- 152.信道划分及其典型应用
- ync 小技巧-14-为用户启用统一的联系人存储库-Lync-无联系人
- 八皇后java_经典八皇后问题:Java语言
- sikuli 搜索例子
- Centos7.3 格式化和挂载数据盘
- GEO-Slope产品
- 如何将数据库中的表导入到PowerDesigner
- 高分屏更改Adobe Premier CC UI界面字体大小
- css3参考手册在线和下载
- air dots 配对_小米AirDots蓝牙耳机如何连接iPhone?小米Air连接苹果手机的方法
- 【经典详解】<T> T 和 T的用法和区别,public <T> List<T> f(T a){}的详解
- 双拼输入法中,例如“哦”“欧”“饿”“而”等,只有韵母的字怎样打?
- js 中实现百分比计算
- 微信公众平台开发(54)翻板抽奖
- flask python 上传图片或头像
- GlassFish 任意文件读取
- 学计算机电脑厚度23mm,轻薄机身兼具生产力!适合专业人士的轻薄笔记本电脑,了解下?...
- 安装mysql 配置环境变量
- 股票自选股基本函数大全-6