西天取经python之路 DAY(十)

装饰器进阶

第一个程序

装饰器使用之验证功能-decorator_verification

# encoding: utf-8
'''
# @Author  : ccq
# @File    : decorator_verification.py
# @Time    : 2019/6/11 12:05
'''

# 装饰器验证用户名密码
username = 'ccq'
password = '123'


def authorv(function):
    def verification(*args, **kwargs):
        uname = input("username:").strip()
        pswd = input("password:").strip()
        if uname == username and pswd == password:
            print("hello %s , welcome!" % (username))
            # 返回值
            res = function(*args, **kwargs)
            return res
        else:
            exit("invalid username or password")

    return verification


def index():
    print("welcome to index page")


@authorv
def home():
    print("welcome to home page")
    return "return value"


def bbs():
    print("welcome to bbs page")


index()
home()
# print(home())

#在昨天的装饰器上进行了改进

#昨天的代码有一个小问题,如果home有返回值的话,那么将无法返回。

#于是在verification函数里加入了返回值。

#但是如果遇到不同的验证方式,比如home是本地验证,bbs是要服务器验证,怎么办呢?

第二个程序

装饰器终极版-finaldecorator

# encoding: utf-8
'''
# @Author  : ccq
# @File    : decorator_verification.py
# @Time    : 2019/6/11 12:05
'''

# 装饰器验证用户名密码
username = 'ccq'
password = '123'


def authorv(authorv_type):
    def out_virification(function):
        def verification(*args, **kwargs):
            if authorv_type == "local":
                uname = input("username:").strip()
                pswd = input("password:").strip()
                if uname == username and pswd == password:
                    print("hello %s , welcome!" % (username))
                    # 返回值
                    res = function(*args, **kwargs)
                    return res
                else:
                    exit("invalid username or password")
            elif authorv_type == "service":
                print("I have no idea about service")

        return verification

    return out_virification


def index():
    print("welcome to index page")


@authorv(authorv_type="local")
def home():
    print("welcome to home page")
    return "return value"


@authorv(authorv_type="service")
def bbs():
    print("welcome to bbs page")


index()
home()
bbs()

#又加了一层嵌套函数,用来传递参数。这个参数是验证方式.并且在函数中加入判断,如果验证方式是local那么采用之前的验证,如果方式是service,那么print。这样就完美解决了。

#如果再遇到新的情况,比如本地验证有好几种方式,那么就再加一个参数就行。判断的时候要把本地验证的方式也加上。因为要判断本地还是服务器验证,以及具体哪一种方式。代码如下:

# encoding: utf-8
'''
# @Author  : ccq
# @File    : decorator_verification.py
# @Time    : 2019/6/11 12:05
'''

# 装饰器验证用户名密码
username = 'ccq'
password = '123'


def authorv(authorv_type, typenum):  # 装饰器
    def out_virification(function):  # 外层验证
        def verification(*args, **kwargs):  # 验证函数
            if authorv_type == "local" and typenum == 1:  # 判断验证方式(具体为本地验证,采用1方式验证)
                uname = input("username:").strip()  # 输入username 并格式化
                pswd = input("password:").strip()  # 输入password 并格式化
                if uname == username and pswd == password:  # 验证是不是该用户
                    print("hello %s , welcome!" % (username))
                    # 返回值
                    res = function(*args, **kwargs)
                    return res
                else:  # 验证失败就退出并提示
                    exit("invalid username or password")
            elif authorv_type == "service" and typenum == 1:  # 判断验证方式(具体为服务器验证,用1方式验证)
                print("I have no idea about service")
            elif authorv_type == "local" and typenum == 2:  # 判断验证方式(具体为本地验证,采用2方式验证)
                print("this is local type 2")

        return verification

    return out_virification


def index():
    print("welcome to index page")


@authorv(authorv_type="local", typenum=1)
def home():
    print("welcome to home page")
    return "return value"


@authorv(authorv_type="local", typenum=2)
def home2():
    print("welcome to home page")
    return "return value"


@authorv(authorv_type="service", typenum=1)
def bbs():
    print("welcome to bbs page")


index()
home()
bbs()
home2()

#逻辑挺复杂的,加上断点调试,一步一步过,过几遍就清晰了。


第三个程序

列表生成器-createlist

# encoding: utf-8
'''
# @Author  : ccq
# @File    : createlist.py
# @Time    : 2019/6/11 15:17
'''
# 列表生成器

# 简单列表生成
a = [i * 2 for i in range(10)]
print(a)


# 定义一个+1的函数
def func(num):
    num = num + 1
    return num


# 在生成器中使用函数
b = [func(i) for i in range(1, 10)]
print(b)
print(b[8])

# 调用的时候才会生成 生成器
c = (z * 2 for z in range(10))
print(c)
for i in c:
    print(i,end=" ")

#[i*2 for i in range(10)]这种是生成列表的一种特殊写法,包括把函数加进去也是。看起来比较酷。

#真正的生成器是最后一个。不可以通过类似这样的代码:print(b[8]) 来使用。


第四个程序

斐波那契数列-fib

# encoding: utf-8
'''
# @Author  : ccq
# @File    : fib.py
# @Time    : 2019/6/11 16:53
'''


# 斐波那契数列
def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        # print(b)
        yield b  # 如果用左边这样的写法,就变成了生成器
        a, b = b, a + b
        n = n + 1

    # return 'done'


f = fib(20)
for i in f:
    print(i)
    if i == 3:
        print("OK")
        break

#生成器可以节省大量内存。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值