2019年3月10日 装饰器进阶-模拟session
ser_dic={'username':None,'login':False}#用户字典,反应登入状态,用字典做全局变量
def idf(func):#验证登入信息是否正确
def wrapper(*args,**kwargs):
if user_dic['username'] and user_dic['login']:#验证登入状态,如果正确登入,则直接执行
print("验证通过")
res = func(*args, **kwargs)
return res
else:
username=input('please input username: '.strip())#.strip指的是移除前后空格或者制表符
password=input('pleasr input password: ')
if username=='sxj' and password=='abc':
user_dic['username']=username#更新用户字典的登入状态
user_dic['login']=True#更新用户字典的登入状态
print("验证通过")
res = func(*args, **kwargs)
return res
else:
print('I am sorry')
return wrapper
@idf
def index():
print('welcome to index')
@idf
def home(name):
print('%s,welcome to home'%name)
@idf
def shopping_car():
print('this is shopping car,you have %s,%s,%s'%('奶茶','妹妹','sxj'))
home('sxj')
shopping_car()
>>>>
please input username:sxj
pleasr input password: abc
验证通过
sxj,welcome to home
验证通过
this is shopping car,you have 奶茶,妹妹,sxj
难度增加:
user_list=[#定义密码库
{'name':'sxj','password':''},
{'name':'abc','password':''},
{'name':'def','password':''},#注意这里的222是字符串类型
{'name':'ghw','password':333}#注意这里的333是int 类型
]
current_dic={'username':None,'login':False}#用户字典,反应登入状态,用字典做全局变量 def idf(func):#验证登入信息是否正确
def wrapper(*args,**kwargs):
if current_dic['username'] and current_dic['login']:#验证登入状态,如果正确登入,则直接执行
print("验证通过")
res = func(*args, **kwargs)
return res
else:
user_name=input('please input username: '.strip())#.strip指的是移除前后空格或者制表符
pass_word=input('please input password: '.strip())#注意这里输入的是字符串 与上面的333会因为类型不同导致密码错误
for u_dic in user_list:
if u_dic['name'] == user_name and str(u_dic['password'])== pass_word: #为了防止发生类型不同,所以用str强制转化为字符串类型
current_dic['username']=user_name#更新用户字典的登入状态
current_dic['login']=True#更新用户字典的登入状态
print("验证通过")
res = func(*args, **kwargs)
return res
else:
print('I am sorry')
return wrapper @idf
def index():
print('welcome to index') @idf
def home(name2):
print('%s,welcome to home'%name2) @idf
def shopping_car():
print('this is shopping car,you have %s,%s,%s'%('奶茶','妹妹','sxj')) print('First>',current_dic)
home('sxj')
print('Last>',current_dic)
shopping_car()
》》》》
First> {'username': None, 'login': False}
please input username:ghw
please input password:333
验证通过
sxj,welcome to home
Last> {'username': 'ghw', 'login': True}
验证通过
this is shopping car,you have 奶茶,妹妹,sxj
2019年3月10日 装饰器进阶-模拟session的更多相关文章
- python学习 day12 (3月18日)----(装饰器内置函数)
读时间函数: # import time # def func(): # start_time = time.time() # 代码运行之前的时间 # print('这是一个func函数') # ti ...
- 易初大数据 2019年11月10日 spss习题 王庆超
◆1.一个数据文件包含下列数据,5个家庭没有汽车(编码为0),20个家庭有一辆汽车(编码唯1),10个家庭拥有两辆汽车(编码为2)指出下列哪种统计量适用于描述该数据并计算出统计量的值.A A拥有汽车数 ...
- 【2019年04月10日】股票的滚动市盈率PE最低排名
仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 新钢股份(SH600782) - 滚动市盈率PE:3.87 - 滚动市净率PB:1.29 - 滚动年化股息收益率:1.31% - 钢铁 ...
- 线段树+lazy标记 2019年8月10日计蒜客联盟周赛 C.小A的题
题目链接:https://nanti.jisuanke.com/t/40852 题意:给定一个01串s,进行m次操作,|s|<=1e6,m<=5e5 操作有两种 l r 0,区间[l,r] ...
- 并查集 2019年8月10日计蒜客联盟周赛 K.数组
题目链接:https://nanti.jisuanke.com/t/40860 题意:给一个长度为n的数组a[],n<1e5,a[i]<1e5 三个操作: 1 x y:把所有值为x的数据改 ...
- Ubantu创建热点并共享——2019年5月10日更新
只需要两步,参考以下两篇文章: ubuntu16.04上安装配置DHCP服务的详细过程 Ubuntu18.04 创建与编辑热点的方法
- 【蜕变之路】第20天 UUID和时间戳的生成 (2019年3月10日)
Hello,大家好!我是程序员阿飞!今天主要学习的内容是:字符串UUID的随机生成和时间戳的随机生成.好了,直接进入正题. 1.UUID的随机生成 /* * uuid的随机生成方式 ...
- AHKManager.ahk AHK管理器 2019年12月15日
AHKManager.ahk AHK管理器 2019年12月15日 快捷键 {Alt} + {F1} ///////////////////////////////////////////// ...
- 19.go语言基础学习(下)——2019年12月16日
2019年12月16日16:57:04 5.接口 2019年11月01日15:56:09 5.1 duck typing 1. 2. 接口 3.介绍 Go 语言的接口设计是非侵入式的,接口编写者无须知 ...
随机推荐
- django的闪现和增、删、改、查
使用 messages 闪现在views.py中导入 from django.contrib import messages 在html中 {% if messages %} {% for mess ...
- express 随笔
#express 1.使用Express 应用生成器 npm install express-generator -g 2.创建一个命名为 myapp 的应用 express myapp 3.安装所有 ...
- Docker入门级简单的操作命令
在理解 Docker 之前,首先得先区分清楚两个概念,容器和虚拟机. 虚拟机都需要有自己的操作系统,虚拟机一旦被开启,预分配给它的资源将全部被占用. 容器技术是和我们的宿主机共享硬件资源及操作系统,可 ...
- 使用pushstate,指定回退地址
history.pushState(null,"testname", window.location.href); window.addEventListener('popstat ...
- ES6语法 Promise Iterator
类和对象 基本定义: class Parent{ constructor(name='lmx'){ //name= 默认值 this.name=name } } let v_parent = new ...
- Linux下Netty实现高性能UDP服务(SO_REUSEPORT)
参考: https://www.jianshu.com/p/61df929aa98b SO_REUSEPORT学习笔记:http://www.blogjava.net/yongboy/archive/ ...
- python类与对象-如何为创建大量实例节省内存
如何为创建大量实例节省内存 问题举例 在网络游戏中,定义玩家类Player(id, name, level...), 每个玩家在线将创建一个Player实例,当在线人数很多时,将产生大量实例, 如何降 ...
- Tomcat配置Web默认页面
1.在web.xml中配置要加载的首页 <welcome-file-list> <welcome-file>Tiring-room/index.jsp</welcome- ...
- 常见的四种文本自动分词详解及IK Analyze的代码实现
以下解释来源于网络-百度百科 1.word分词器 word分词 [1] 是一个Java实现的分布式的中文分词组件,提供了多种基于词典的分词算法,并利用ngram模型来消除歧义.能准确识别英文.数字, ...
- linux下chromedriver的安装
很多时候,发现phantomjs被一些网站屏蔽导致我们无法达到想要的结果,一方面phantomjs也停止维护,这时候们可以使用chromedriver(谷歌)或者firefoxdriver(火狐)来代 ...