Home > Backend Development > Python Tutorial > 使用python装饰器验证配置文件示例

使用python装饰器验证配置文件示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-16 08:45:08
Original
1160 people have browsed it

根据不同配置文件调用不同的验证函数检查输入。可以根据需求更改验证函数的逻辑。

复制代码 代码如下:

def VerifyData(func):

    def VerifyInt(data):
        assert(int(data) > 0)

    def VerifyString(data):
        assert(len(data) > 10)

    def inner(*args, **kvargs):

        print args
        print kvargs

        assert(len(args) > 1)
        if args[1] == "int.txt":
            VerifyInt(args[0])
        elif args[1] == "string.txt":
            VerifyString(args[0])

        func(*args, **kvargs)

    return inner

@VerifyData
def WriteData(text, filepath):
    print "WriteData"
    print text
    print filepath

WriteData("1234567890", "int.txt")

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template