day-11

#coding = Unicode
'''
装饰器:
定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
原则:1、不能修改被装饰的函数的源代码
      2、不能修改被装饰的函数的调用方式
实现装饰器知识储备:
1、函数即“变量”
2、高阶函数
        a:把一个函数名当做实参传给另外一个函数(不修改被装饰函数源代码情况下为其添加功能)
        b:返回值中包含函数名(不修改函数的调用方式)
3、嵌套函数


高价函数+嵌套函数=》装饰器
'''


#import time
##装饰器范例
#def timmer(func):
#    def warpper(*args,**kwargs):
#        start_time = time.time()
#        func()
#        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()


#def bar():
#    print('in the bar')
#def foo():
#    print('in the foo')
#    bar()
#foo()


#def foo():
#    print('in the foo')
#    bar()
#foo()
#def bar():
#    print('in the bar')
#    bar()


#高阶函数
#def bar():
#    print('in the bar')


#def test1(func):
#    print(func)
#    start_time = time.time()
#    func()
#    stop_time = time.time()
#    print("the func run time is %s" %(stop_time-start_time))
#test1(bar)
#func = bar
#func()


#def bar():
#    time.sleep(3)
#    print('in the bar')
#def test2(func):
#    print(func)
#    return func
##print(test2(bar))
#test2(bar())
#bar = test2(bar)
#bar()       #run bar


#def foo():
#    print('in the foo')
#    def bar():
#        print('in the bar')
#    bar()
#foo()


#x=0
#def grandpa():
#    # x=1
#    def dad():
#        x=2
#        def son():
#            x=3
#            print(x)
#        son()
#    dad()
#grandpa()


#完整装饰器
#import time
#def timer(func):
#    def deco(*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 deco
#@timer  #test1 = timer(test1)
#def test1():
#    time.sleep(3)
#    print('in the test1')
#@timer
#def test2(name,age):
#    time.sleep(3)
#    print("test2:",name,age)
#test1()
#test2("tom",22)
#@timer
#def test2():
#    time.sleep(3)
#    print('in the test2')
#deco(test1)
#test1 = deco(test1)
#test1()
#deco(test2)
#test2 = deco(test2)
#test2
#test1 = timer(test1)
#import time
#user,passwd = 'tom','123'
#def auth(auth_type):
#    print("auth func:",auth_type)
#    def outer_wrapper(func):
#        def wrapper(*args,**kwargs):
#            print("auth func:",*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 authentiation\033[0m")
#                #res = func(*args,**kwargs)
#                print("-----Wlecome----")
#                #return res
#            else:
#                exit("\033[31;1mInvalid username or password\033[0m")
#        elif auth_type == "Idap":
#            print("exit")
#        return wrapper
#    return outer_wrapper
#def index():
#    print("welcome to index page")
#@auth(auth_type="local")
#def home():
#    print("welcome to home page")
#@auth(auth_type="Idap")
#def bbs():
#    print("welcome to bbs page")
#index()
#print(home())
#bbs()


'''
生成器 只有在调用时才会生成相应的数据,只记录当前位置
只有一个__next__()方法
'''
#斐波拉契数列
#def fib(max):
#    n, a, b = 0, 0, 1
#    while n < max:
#        print(b)
#        a, b = b, a + b
#        n = n + 1
#    return 'done'


#单线程并行效果(协成)
#import time
#def consumer(name):
#    print("%s 准备吃包子啦!" %name)
#    while True:
#       baozi = yield


#       print("包子[%s]来了,被[%s]吃了!" %(baozi,name))
##c = consumer("kali")
##c.__next__()


##b1 = "韭菜馅"
##c.send(b1)
##c.__next__()


#def producer(name):
#    c = consumer('A')
#    c2 = consumer('B')
#    c.__next__()
#    c2.__next__()
#    print("我开始准备做包子啦!")
#    for i in range(10):
#        time.sleep(1)
#        print("做了2个包子!")
#        c.send(i)
#        c2.send(i)


#producer("alex")


'''
迭代器
可以使用isinstance判断是否为可迭代对象Iterable
可以被next()函数调用并不断返回下一个值的对象称为迭代器Iterator,它们表示一个惰性计算的序列
集合数据类型如list、dict、str等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。
'''


##内置方法
#详细说明:https://docs.python.org/3/library/functions.html?highlight=built#ascii


#匿名函数
#def sayhi(n):
#    print(n)
    
#sayhi(3)


#(lambda n:print(n))(5)
#calc = lambda n:print(n)
#calc(6)


#字典排序
#a = {6:2,8:0,1:-9,-1:4}
#print(sorted(a.items()))
#print(sorted(a.items(), key=lambda x:x[1]))
#print(a)


#两个列表集合
#a = [1,2,3,4,5,6]
#b = ['a','b','c','d','e']
#for i in zip(a,b):
#    print(i)
    #print(i[1])


'''
json序列化
'''
#import json
#def sayhi(name):
#    print("Hello,",name)
#info = {
#    'name':'tom',
#    'age':12,
#    'func':sayhi
#}
#f = open("test.text","w")
#f.write(json.dumps(info))
#f.close()


#import pickle
#def sayhi(name):
#    print("Hello,",name)
#info = {
#    'name':'tom',
#    'age':12,
#    'func':sayhi
#}
#f = open("test.text","wb")
##f.write( pickle.dumps(info))
#pickle.dump(info,f)
#f.close()






'''
json反序列化
'''
#import json
#f = open("test.text","r")


#data = json.loads(f.read())
#print(data["age"])
#f.close()


#import  pickle
#f = open("test.text","rb")


##data =  pickle.loads(f.read())
#data =  pickle.load(f)


##print(data)
#f.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值