动机

验证数据是否符合规范是很有用的,比如:

  • 用于单元测试
  • 用于验证用户提交的数据是否合法

简介

schema1是一个用来验证python数据结构的库。

可以用来验证诸如:

  • 配置文件
  • 表单
  • 外部服务
  • 命令行解析
  • JSON/YAML转换后的数据

用法

这个库相对于jsonschema2,看起来更加的pythonic,也更加通用。后者使用js形式的字符串来注解,在很多idel里面甚至没有高亮提示。

>>> from schema import Schema, And, Use, Optional

>>> schema = Schema([{'name': And(str, len),
... 'age': And(Use(int), lambda n: 18 <= n <= 99),
... Optional('gender'): And(str, Use(str.lower),
... lambda s: s in ('squid', 'kid'))}]) >>> data = [{'name': 'Sue', 'age': '28', 'gender': 'Squid'},
... {'name': 'Sam', 'age': '42'},
... {'name': 'Sacha', 'age': '20', 'gender': 'KID'}] >>> validated = schema.validate(data) >>> assert validated == [{'name': 'Sue', 'age': 28, 'gender': 'squid'},
... {'name': 'Sam', 'age': 42},
... {'name': 'Sacha', 'age' : 20, 'gender': 'kid'}]

个人评分

类型 评分
实用性 ⭐️⭐️⭐️
易用性 ⭐️⭐️⭐️
有趣性 ⭐️⭐️

【AMAD】schema -- 使用pythonic的方式进行schema验证的更多相关文章

  1. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

  2. onfiguration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...

  3. cvc-elt.1: Cannot find the declaration of element 'beans'Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-3.0.xsd'

    Multiple annotations found at this line: - cvc-elt.1: Cannot find the declaration of element 'beans' ...

  4. webServices接口开发过程中项目启动遇到的错误org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 422; schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-bean

    org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 422; schema_reference.4: Failed to read ...

  5. 【Dubbo&&Zookeeper】3、Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法

    转自:http://blog.csdn.net/gaoshanliushui2009/article/details/50469595 我们公司使了阿里的dubbo,但是阿里的开源网站http://c ...

  6. Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法

    Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法 关于dubbo服务的 ...

  7. Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]

    ERROR - Context initialization failed org.springframework.beans.factory.parsing.BeanDefinitionParsin ...

  8. 整合mybatis时报错:Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...

  9. transaction 用tx事务 测试时 报错:Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc]

    Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/sc ...

随机推荐

  1. TCP/IP 三次握手和四次挥手

    TCP 三次握手 作用:建立TCP连接 1.三次握手是客户端先发起请求到服务器,此时服务器处于LISTEN监听状态,A会先发送一个连接请求的报文---SYN=1,ACK=0,seq=x ,这个包也称为 ...

  2. jquery unbind()方法 语法

    jquery unbind()方法 语法 作用:unbind() 方法移除被选元素的事件处理程序.该方法能够移除所有的或被选的事件处理程序,或者当事件发生时终止指定函数的运行.ubind() 适用于任 ...

  3. 【CUDA 基础】2.3 组织并行线程

    title: [CUDA 基础]2.3 组织并行线程 categories: CUDA Freshman tags: Thread Block Grid toc: true date: 2018-03 ...

  4. 第06课:作用域、JS预解析机制

    从字面上理解----域就是空间.范围.区域,作用就是读.写,所以作用域我们可以简单理解为:在什么样空间或者范围内对数据进行什么样的读或写操作. 看一下代码 alert(a); // 为什么是undef ...

  5. [笔记]C++下的数组声明

    /* 例子来源于<C++ Primer> */ ]; //prts是含有10个整数类型指针的数组 ]; //错误,没有引用的数组 ]; //指向有10个整型元素数组的指针 ]; //引用有 ...

  6. 如何使用getattr运行单个函数

    import sys def foo(): print("哈哈想不到吧") if __name__ == '__main__': getattr(sys.modules[__nam ...

  7. IP输出 之 ip_local_out

    概述 将要从本地发出的数据包,会在构造了ip头之后,调用ip_local_out函数,该函数设置数据包的总长度和校验和,然后经过netfilter的LOCAL_OUT钩子点进行检查过滤,如果通过,则调 ...

  8. Java 实现 2的次幂表示

    问题描述 任何一个正整数都可以用2进制表示,例如:137的2进制表示为10001001. 将这种2进制表示写成2的次幂的和的形式,令次幂高的排在前面,可得到如下表达式:137=2^7+2^3+2^0 ...

  9. Walkthrough: My first WPF desktop application

    Walkthrough: My first WPF desktop application This article shows you how to develop a Windows Presen ...

  10. n个数连接得到最小或最大的多位整数(携程)

    package numCombine; import java.util.Arrays; import java.util.Collections; import java.util.Comparat ...