###装饰器
#Author:walter
##装饰器:
#定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
#原则:1,不能修改被装饰函数的源代码
#2,不能修改被装饰函数的调用方式
#实现装饰器知识储备:
# 1.函数即“变量”
# 2.高阶函数:a,把一个函数名当做实参,传给另外一函数
# (在不修改被装饰函数源代码的情况下为其添加功能)
#b,返回值中包含函数名(不修改函数的调用方式)
# 3.嵌套函数
# 高阶函数+嵌套函数=》装饰器
'''
import time
def timmer(func): #timmer(test1) func=test1
def deco():
start_time = time.time()
func() #run test1()
stop_time = time.time()
print("the func run time is %s"%(stop_time-start_time))
return deco
@timmer #tese1=timmer(test1)
def test1():
time.sleep(3)
print('in the test1')
test1()
import time
def timmer(func): ##装饰函数
def warpper(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs)
stop_time = time.time()
print('the func run time is %s'%(stop_time-start_time))
return warpper
@timmer
def test1():
time.sleep(3)
print('in the test1')
test1()
'''
import time
user,passwd = 'lzq','123456'
def auth(auth_type):
print("auth func:",auth_type)
def outer_wrapper(func):
def wrapper(*args,**kwargs):
print("wrapper 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 authentition\033[0m")
res = func(*args,**kwargs)
print("-----after authentication")
return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
elif auth_type == "ldap":
print("无效ladp")
return wrapper
return outer_wrapper
def index():
print("welcome to index page")
@auth(auth_type = "local")
def home():
print("welcome to home page")
return "form home"
@auth(auth_type = "ldap")
def bbs():
print("welcome to bbs page")
index()
print(home()) #wrapper()
bbs()
#Author:walter
code = '''
print(all([0,-5,3])) #判断可迭代对象是否为真,有一个假便假
print(any([0,-5,3])) #判断可迭代对象是否为真,有一个真便真
print(ascii([1,2,"我是某某某"])) #变为可打印的字符串形式
print(bin(10)) #把数字转化为二进制格式
print(bool()) #判断真假
'''
#bytearray("abcdefg") #打开可修改的字符串
print(callable([])) # 判断是否可以调用
print(chr(98)) #返回ASCII对应表的值
print(ord('b')) #返回值所对应的ASCII数 字
#compile() #可以操作字符串''' '''里的代码
#py_obj = compile(code,"err.log","exec")
#exec(py_obj)
exec(code) #等价于上面三句
dict() =={} #字典
dir() #调用所有可用函数
print(divmod(5,2)) #相除并返回商
res = filter(lambda n :n >5,range(10))
#res=map(lambda n: n*2,range(10))
#import functools
#res=functools.reduce((lambda x,y: x*y,range(1,10))) 接乘
for i in res:
print(i)
#import functools.reduce() , filter(), map() 配合lambda应用
a = frozenset([1,2,3,45,75,65]) #不可变集合,冻结
print(globals()) #判断变量是存在语法
print(hash(0)) #确定一个值不变
print(hex(10) ) #转成16进制
print(oct(10)) #转八进制
print(pow(2,8)) #多少次方
#print(repr()) #转成字符串对象
#reversed() #翻转
print(round(1.3365,2)) #四舍五入,保留两位
print(slice(2,5,None)) #切片》
a = {6:2,8:0,4:-5,99:11,7:85}
print(sorted(a.items())) #排序 按第一个数字大小排序
print(sorted(a.items(),key = lambda x:x[1])) #排序 按第二个数字大小排序
b1 = [1,2,3,4,5,6]
b2 = ['a','b','c','d']
for i in zip(b1,b2): #拼接形成数组
print(i)
__import__('decorator') #调用字符串,以后常用
###列表生成器
#Author:walter
def fib(max):
n,a,b = 0, 0, 1
while n < max:
#print(b)
yield b #当吧print换成yield后变成生成器
a,b = b, a+b
n += 1
return 'done' #当出现异常时出现的消息
g=fib(6)
while True: #处理报错
try:
x = next(g)
print('g:',x)
except StopIteration as e:
print('Generator return value:',e.value)
break
# f=fib(8)
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# fib(15)
#for i in f:
# print(i) 循环输出
'''
a=[]
for i in range(10):
a.append(i*2)
print(a)
#列表生成式
b = [i*2 for i in range(10)]
print(b)
#生成器:只有在调用时才会生成相应的数据
#只记录当前位置,只有一个__next__()方法
c=(i*2 for i in range(10))
print(c.__next__())
print(c.__next__())
'''
##列表并行
#Author:walter
import time
def consumer(name):
print("%s 准备吃包子啦!" %name)
while True:
baozi= yield
print("包子[%s]来啦,被[%s]吃啦!"%(baozi,name))
c = consumer("lzq")
c.__next__()
# c.__next__()
# b1 = "韭菜馅的"
# c.send(b1)
def producer(name):
c=consumer('A')
c1=consumer('B')
c.__next__()
c1.__next__()
print("真的开始准备吃包子啦!")
for i in range(10):
time.sleep(1)
print("做了2个包子!")
c.send(i)
c1.send(i)
producer("walter")