python-验证功能的装饰器示例
user_list=[
{'name':"alex","pwd":''},
{'name':'tang','pwd':''},
{'name':'sb','pwd':''}
]
current_dict={'username':None,'login':False}
def auth(auth_type):
def auth_func(func):
def wrapper(*args,**kwargs):
print("认证类型", auth_type) if auth_type == 'filedb': if current_dict['username'] and current_dict['login']:#判断用户的登陆状态
res = func(*args, **kwargs)
return res
username=input("用户名").strip()#strip()函数去除字符串两边的空格
password=input('密码').strip() for item in user_list:
if username==item['name'] and password==item['pwd']:
current_dict['username']=username#改变用户的登陆状态,改变后就不需要每次都进行输入
current_dict['login']=True
res=func(*args,**kwargs)
return res
else:
print("用户名或密码错误")
elif auth_type=="ldap":
print("hahhaha") return wrapper return auth_func @auth(auth_type="filedb")#auth_func=auth(auth_type="filedb")--->@auth_func 附加了一个auth_type---->index=auth_func(index)
def index():
print("欢迎来到京东") @auth(auth_type='')
def home(name):
print("我的个人中心%s"%(name)) @auth(auth_type='filedb')
def shop_car(name):
print("%s我的购物车"%(name)) print("after---->",current_dict)
index()
print('after---->',current_dict)
home("alex")
shop_car("产品经理") # dict_user={"username":None,"login":False}
#
# def auth_func(func):
# def wrapper(*args,**kwargs):
# if dict_user['username'] and dict_user['login']:
# res = func(*args, **kwargs)
# return res
# username=input("用户名").strip()#strip()函数去除字符串两边的空格
# password=input('密码').strip()
# if username=="sb" and password=="123":
# dict_user['username']=username
# dict_user['login']=True
# res=func(*args,**kwargs)
# return res
# else:
# print("用户名或密码错误")
#
# return wrapper
#
#
#
#
#
#
# @auth_func
# def index():
# print("欢迎来到京东")
#
# @auth_func
# def home(name):
# print("我的个人中心%s"%(name))
#
# @auth_func
# def shop_car(name):
# print("%s我的购物车"%(name))
#
# index()
# home("alex")
# shop_car("产品经理")
#
# # a="123"
# b='1'
# v=b.join(a)##b join a
# print(v)
python-验证功能的装饰器示例的更多相关文章
- Python中利用函数装饰器实现备忘功能
Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下 " ...
- python 各种装饰器示例(python3)
参考网址: Python中的各种装饰器详解_python_脚本之家http://www.jb51.net/article/63892.htm 一.函数式装饰器: 1.装饰器无参数,被装饰对象无参数 d ...
- python函数式编程之装饰器(一)
1.开放封闭原则 简单来说,就是对扩展开放,对修改封闭 在面向对象的编程方式中,经常会定义各种函数. 一个函数的使用分为定义阶段和使用阶段,一个函数定义完成以后,可能会在很多位置被调用 这意味着如果函 ...
- Python之迭代器、装饰器、软件开发规范
本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...
- python之闭包与装饰器
python闭包与装饰器 闭包 在函数内部定义的函数包含对外部的作用域,而不是全局作用域名字的引用,这样的函数叫做闭包函数. 示例: #-------------------------------- ...
- python基础-函数之装饰器、迭代器与生成器
1. 函数嵌套 1.1 函数嵌套调用 函数的嵌套调用:在调用一个函数的过程中,又调用了其他函数 def bar(): print("from in the bar.") def f ...
- python基础——8(装饰器)
一.nonlocal关键字 def outer(): num = 0 def inner(): # 如果想在被嵌套的函数中修改外部函数变量(名字)的值 nonlocal num # 将 L 与 E(E ...
- Python函数编程——闭包和装饰器
Python函数编程--闭包和装饰器 一.闭包 关于闭包,即函数定义和函数表达式位于另一个函数的函数体内(嵌套函数).而且,这些内部函数可以访问它们所在的外部函数中声明的所有局部变量.参数.当其中一个 ...
- Python函数篇:装饰器
装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理. ...
随机推荐
- 实用工具/API
实用工具/API PNG图片无损压缩 在线给图片加水印 随机密码生成 随机头像生成 微博一键清理工具 CSS压缩 在线工具 免费虚拟主机 技术摘要 https://github.com/biezhi/ ...
- [BZOJ3527][ZJOI2014]力:FFT
分析 整理得下式: \[E_i=\sum_{j<i}{\frac{q_i}{(i-j)^2}}-\sum_{j>i}{\frac{q_i}{(i-j)^2}}\] 假设\(n=5\),考虑 ...
- Redis实现存取数据+数据存取
添加依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> ...
- cookie格式化
#coding=utf- import requests url = 'http://www.baidu.com' f=open(r'cookies.txt','r') cookies={} for ...
- r hive
w r只能处理有限量的数据 pdf 467
- 架构-层-Model:Model
ylbtech-架构-层-Model:Model 1.返回顶部 1. Model,意思是模特儿,模特儿是英文“model”的音译.模特一般来说要五官端正,身材良好,有气质,展示能力强,另外身高要具备一 ...
- Node - 模块加载与 lerna 提升
从node_modules 加载模块的过程 如果要加载的模块非核心模块,并且路径不是'/'. '../'和'./'开头,这个模块就会从当前文件夹递归向上在node_modules文件夹中寻找这个模块. ...
- Vue知识整理2:Vue生命周期方法
在vue执行过程中,可以分为beforeCreate.created.BeforeMount.mounted .BeforeUpdate.updated 等常用的方法,如下图所示. 除此之外,通过查 ...
- Oracle数据备份与恢复
为了保证数据库的高可用性,Oracle数据库提供了备份和恢复机制,以便在数据库发生故障时完成对数据库的恢复操作,避免损失重要的数据资源 丢失数据分为:物理丢失:操作系统的数据库主键(数据文件.控机文件 ...
- 测开之路九十三:css之文字样式和段落对齐
引用css 字体族 字体类型和尺寸 em:字体按倍数缩放 font-weight:调整文字的粗细 段落对齐:text-align 左对齐:left 右对齐:right 居中对齐:center 两边对齐 ...