动机

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

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

简介

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. MariaDB安装与使用

    下载地址:https://downloads.mariadb.org/ 下载相对应的电脑版本程序 等待下载完成...... 安装教程: 双击运行 设置数据库的密码 等待安装完成.. 这样就完成安装了. ...

  2. 题解 小B的询问

    题面 解析 这就是道莫队模板啊啊!! 因此,似乎并没有什么好讲的. 莫队算法传送门 我们只需要将询问存下来, 离线处理就行了. 还是上代码吧: #include<bits/stdc++.h> ...

  3. table 表格

    标签 <table> 标签定义HTML中的表格 <tr>  标签定义表格中的行. <th>  标签定义表格中表头的每一项.元素内部的文本通常会呈现为居中的粗体文本. ...

  4. 面向对象(oop)特征

    上课时老师不止一次和我们说过,面向对象是Java基础的重中之重!!所以一定要扎实基本功,代码都是呆子活,重要的是思想! 一般来说oop的三大特性是:封装.继承和多态,上次笔者看到有把抽象也归类其中,不 ...

  5. 51 Nod 1101 换零钱(动态规划好题)

    1101 换零钱  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 ...

  6. 文件操作(stat)

    /*** stat.c ***/ #include<stdio.h> #include<string.h> #include<sys/stat.h> #includ ...

  7. Kamil and Making a Stream

    E. Kamil and Making a Stream 参考:Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上 ...

  8. Vue_(组件)实例属性

    Vue实例属性与方法中文文档 传送门   Vue实例属性:vue实例直接调用的属性 Learn 一.vm.$data:获取属性 二.vm.$el:获取实例挂载的元素 三.vm.$options:获取自 ...

  9. linu逻辑分区动态调整大小

    注意: 这个动态调整的方法是有丢数据风险的,要确保调整的源分区没有使用或者使用率很低.源分区中如果有重要的文件最好先备份 在centos 6.5上操作过 lvdisplay 查看已有的分区的大小 lv ...

  10. umount 报错was not found in /proc/mounts

    前段时间在整理服务器时,看到nfs都是通过公网调用的,但是实际这几台服务器都是可以内网互通的,为了更稳定的使用,打算把这些挂载都更改为通过内网ip挂载,什么都设置好之后,操作第一台服务器没有问题,um ...