import time
current_user={'user':None}
def outter(func):
def wrapper(*args,**kwargs):
if current_user['user'] is not None:
res=func(*args,**kwargs)
return res
user=input('username>>>: ').strip()
pwd=input('password>>>: ').strip()
if user == 'egon' and pwd == '123':
print('login successfull')
current_user['user']=user
res=func(*args,**kwargs)
return res
else:
print('user or password error')
return wrapper
@outter
def index():
time.sleep(1)
print('from index')
@outter
def home(name):
print('welcome %s' %name)
index()
home('egon')
import time
current_user={'user':None}
def auth(engine='file'):
def outter(func):
def wrapper(*args,**kwargs):
if current_user['user'] is not None:
res=func(*args,**kwargs)
return res
user=input('username>>>: ').strip()
pwd=input('password>>>: ').strip()
if engine == 'file':
# 基于文件的认证
if user == 'egon' and pwd == '123':
print('login successfull')
current_user['user']=user
res=func(*args,**kwargs)
return res
else:
print('user or password error')
elif engine == 'mysql':
# 基于mysql的认证
print('基于mysql的认证')
elif engine == 'ldap':
# 基于ldap的认证
print('基于ldap的认证')
else:
print('不知道engine')
return wrapper
return outter
@auth('ldap') #@outter #index=outter(index) # index=wrapper
def index():
time.sleep(1)
print('from index')
@auth('mysql') #@outter # home=outter(home) #home=wrapper
def home(name):
print('welcome %s' %name)
index()
home('egon')
import time
def outter(func):
# func=index
def wrapper(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs)
stop=time.time()
print(stop-start)
return res
return wrapper
from functools import wraps #wraps 为与index内的文档返回相同
def outter(func):
@wraps(func) #所以加上 @wraps
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
return res
# wrapper.__name__ = func.__name__
# wrapper.__doc__ = func.__doc__
return wrapper
@outter #index=outter(index) #index=wrapper
def index():
"""
这是index函数....
:return:
"""
print('from index')
print(index.__name__) #print(wrapper.__name__)
print(index.__doc__)
叠加多个装饰器
1. 加载顺序(outter函数的调用顺序):自下而上
2. 执行顺序(wrapper函数的执行顺序):自上而下
def outter1(func1): #func1=wrapper2的内存地址
print('加载了outter1')
def wrapper1(*args,**kwargs):
print('执行了wrapper1')
res1=func1(*args,**kwargs)
return res1
return wrapper1
def outter2(func2): #func2=wrapper3的内存地址
print('加载了outter2')
def wrapper2(*args,**kwargs):
print('执行了wrapper2')
res2=func2(*args,**kwargs)
return res2
return wrapper2
def outter3(func3): # func3=最原始的那个index的内存地址
print('加载了outter3')
def wrapper3(*args,**kwargs):
print('执行了wrapper3')
res3=func3(*args,**kwargs)
return res3
return wrapper3
@outter1 # outter1(wrapper2的内存地址)======>index=wrapper1的内存地址
@outter2 # outter2(wrapper3的内存地址)======>wrapper2的内存地址
@outter3 # outter3(最原始的那个index的内存地址)===>wrapper3的内存地址
def index():
print('from index')
print('======================================================')
index()
'''import time
def timmer(func):
def aaa(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs) #最原始那个index的内存地址()
stop=time.time()
print('run time is %s' %(stop -start))
return res
return aaa
def auth(func):
def bbb(*args,**kwargs):
name=input('name>>>: ').strip()
pwd=input('pwd>>>: ').strip()
if name == 'egon' and pwd == '123':
print('login successfull')
res=func(*args,**kwargs)
return res
else:
print('user or pwd error')
return bbb
@timmer
@auth
def index():
time.sleep(1)
print('from index')
index()
三元表达式
def max2(x,y):
if x > y:
return x
else:
return y
res=max2(10,20)
x=10
y=20
res=x if x > y else y
print(res)
res='OK' if False else 'No'
print(res)
生成式
列表生成式
l=[]
for i in range(10):
if i > 4:
l.append(i**2)
l=[i**2 for i in range(10) if i > 4]
print(l)
names=['egon','alex_sb','kevin_sb','hxx_sb','cxx_sb']
sbs=[]
for name in names:
if name.endswith('sb'):
sbs.append(name)
sbs=[name.upper() for name in names if name.endswith('sb')]
print([name.upper() for name in names])
print([name for name in names if name.endswith('sb')])
字典生成式
res={i:i**2 for i in range(10) if i > 3}
print(res)
print({i for i in 'hello'})
匿名函数
def func():
print('from func')
func()
func()
func()
匿名函数就是只定义了一个函数的内存地址,主要用于临时使用一次的场景
func=lambda x,y:x+y
print(func)
print(func(1,2))
res=(lambda x,y:x+y)(1,2)
print(res)
print(max([10,11,-3,23]))
salaries={
'egon':3000,
'alex':100000000,
'wupeiqi':10000,
'yuanhao':2000
}
def func(k):
return salaries[k]
print(max(salaries,key=lambda k:salaries[k]))
print(min(salaries,key=func))
for循环的结果 比较依据
'egon' 3000
'alex' 100000000
'wupeiqi' 10000
'yuanhao' 2000
l=[4,2,3]
l_new=sorted(l,reverse=True)
print(l_new)
print(sorted(salaries,key=lambda k:salaries[k],reverse=True))
max: 取最大值
min: 取最小值
sorted: 排序 从小到大 ,加reverse=True 从大到小
map: 把一个列表按照我们自定义的映射成一个新列表
filter: 从一个列表中过滤出符合我们过滤规则的值
reduce: 把多个值合并成一个结果
eval: 把文件中像字典的字符串,取出打印出时是一个字典