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的更多相关文章

  1. python学习 day12 (3月18日)----(装饰器内置函数)

    读时间函数: # import time # def func(): # start_time = time.time() # 代码运行之前的时间 # print('这是一个func函数') # ti ...

  2. 易初大数据 2019年11月10日 spss习题 王庆超

    ◆1.一个数据文件包含下列数据,5个家庭没有汽车(编码为0),20个家庭有一辆汽车(编码唯1),10个家庭拥有两辆汽车(编码为2)指出下列哪种统计量适用于描述该数据并计算出统计量的值.A A拥有汽车数 ...

  3. 【2019年04月10日】股票的滚动市盈率PE最低排名

    仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 新钢股份(SH600782) - 滚动市盈率PE:3.87 - 滚动市净率PB:1.29 - 滚动年化股息收益率:1.31% - 钢铁 ...

  4. 线段树+lazy标记 2019年8月10日计蒜客联盟周赛 C.小A的题

    题目链接:https://nanti.jisuanke.com/t/40852 题意:给定一个01串s,进行m次操作,|s|<=1e6,m<=5e5 操作有两种 l r 0,区间[l,r] ...

  5. 并查集 2019年8月10日计蒜客联盟周赛 K.数组

    题目链接:https://nanti.jisuanke.com/t/40860 题意:给一个长度为n的数组a[],n<1e5,a[i]<1e5 三个操作: 1 x y:把所有值为x的数据改 ...

  6. Ubantu创建热点并共享——2019年5月10日更新

    只需要两步,参考以下两篇文章: ubuntu16.04上安装配置DHCP服务的详细过程 Ubuntu18.04 创建与编辑热点的方法

  7. 【蜕变之路】第20天 UUID和时间戳的生成 (2019年3月10日)

    Hello,大家好!我是程序员阿飞!今天主要学习的内容是:字符串UUID的随机生成和时间戳的随机生成.好了,直接进入正题. 1.UUID的随机生成 /*          * uuid的随机生成方式 ...

  8. AHKManager.ahk AHK管理器 2019年12月15日

    AHKManager.ahk  AHK管理器  2019年12月15日 快捷键   {Alt} + {F1} ///////////////////////////////////////////// ...

  9. 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 语言的接口设计是非侵入式的,接口编写者无须知 ...

随机推荐

  1. ubuntu下安装PyCharm的两种方式

    PyCharm一个是Python集成开发环境,它既提供收费的专业版,也提供免费的社区版本.PyCharm带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Proj ...

  2. iOS UIView 选择性倒角

    有些APP中会有卡券,卡券做成了选择性倒角,例如左上,右上倒角.非常美观.看一下iOS的实现: #import "Masonry.h" @interface WJWDaojiaoV ...

  3. nodejs 使用superagent+cheerio+eventproxy爬取豆瓣帖子

    //cnpm install superagent cheerio eventproxy fs pathvar superagent = require('superagent'); var chee ...

  4. java web 读取数据库数据写入Excel返回浏览器下载

    @RequestMapping(value = "/download", method = RequestMethod.GET) public void downstudents( ...

  5. 快速搭建一个直播Demo

    缘由 最近帮朋友看一个直播网站的源码,发现这份直播源码借助 阿里云 .腾讯云这些大公司提供的SDK 可以非常方便的搭建一个直播网站.下面我们来给大家讲解下如何借助 腾讯云 我们搭建一个简易的 直播示例 ...

  6. 简单的linux使用命令

    1 查看日志异常 tail -f 实时查看日志文件 tail -f 日志文件logtail - 100f 实时查看日志文件 后一百行tail -f -n 100 catalina.out linux查 ...

  7. Applet学习教程(一):applet+dwr 实现

    后台代码 import java.applet.Applet; import java.util.HashMap; import java.util.Map; import netscape.java ...

  8. C# 多线程之Task(任务

      1.简介 为什么MS要推出Task,而不推Thread和ThreadPool,以下是我的见解: (1).Thread的Api并不靠谱,甚至MS自己都不推荐,原因,它将整个Thread类都不开放给W ...

  9. Post请求data参数构造及巧用js脚本显示爬虫进度

    小爬最近随着对python中字符串.json等理解进一步加深,发现先前我随笔中提到的data构造和传参方法略复杂,原本有更简单的方法,Mark如下. 先前小爬我使用的requests.post请求中d ...

  10. linux安装软件时提示找不到镜像的问题:Couldn't resolve host 'mirrorlist.centos.org'

    问题:[root@cddserver2 ~]# yum -y install gcc-*Loaded plugins: fastestmirror, prestoCould not retrieve ...