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

yipeiwu_com6年前Python基础

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

复制代码 代码如下:

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")

相关文章

Python version 2.7 required, which was not found in the registry

Python version 2.7 required, which was not found in the registry

安装PIL库的时候,直接提示:Python version 2.7 required, which was not found in the registry。 如图: 大意是说找不到...

详解Python核心对象类型字符串

Python的字符串的特点 Python与C语言,Java语言都不一样,没有单个字符,只有一个有一个字符的字符串。 字符串对象不可修改,属于不可变类型 字符串和列表,元组都...

Python中的tuple元组详细介绍

Tuple 是不可变 list。 一旦创建了一个 tuple 就不能以任何方式改变它。 Tuple 与 list 的相同之处 定义 tuple 与定义 list 的方式相同, 除了整个元...

python使用wmi模块获取windows下的系统信息 监控系统

Python用WMI模块获取Windows系统的硬件信息:硬盘分区、使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息。 本文实例讲述了python使用...

pytorch 转换矩阵的维数位置方法

例如: preds = to_numpy(preds)#preds是[2985x16x2] preds = preds.transpose(2, 1, 0)#preds[2x16x2...