使用简单的python数据结构定义数据的形状。使用这些数据描述来验证应用程序。

schemagic的Python项目详细描述


===
架构/架构.web
==
==
图片::https://img.shields.io/badge/pypi-v0.9.1-blue.svg
:目标:https://pypi.python.org/pypi/schemagic
…图片::https://img.shields.io/badge/readthedocs latest red.svg
:目标:http://schemagic.readthedocs.io/en/latest/schemagic.html
…图片:https://travis-ci.org/mechrophile/schemagic.svg?branch=master
:target:https://travis ci.org/mechrophile/schemagic/
从数据处理中删除猜测工作
==





schemagic.web是程序员讨厌web编程,但又想让他们的程序能够访问web时所做的事情。



代码块::bash

$pip install schemagic

schemagic是什么?
----

此外,输入和静态类型不足以捕获和验证定制的业务类型,
这最终阻碍了团队快速迭代彼此的工作。[1]


schemagic对您,程序员,来说都是三件事:

*数据**描述**最简单的python数据结构和易于扩展的语法
*团队之间的数据**通信**,增强文档,在出现错误时提供反馈。
*数据**验证**基于对已记录和通信的数据的描述。
注释描述在实际的应用程序中,记录数据的形状是不够的。
除非通过编程验证备份文档,否则文档最初会被忽略,
,最终会落后于实际的程序行为。

数据**.



熟悉schemagic
--------


让我们构建一个模式并开始使用它。

……代码块::python

>;>import schemagic
>;
>;>list_of_ints=[int]
>;>schemagic.validate_account_schema(list_of_ints,[1,2,3])
[1,2,3]
>;回溯(最近一次调用):

valueerror:int()的文本无效,基数为10:'hello'

代码块::python

>;>;int("hello")
回溯(最后一次调用):

valueerror:invalid literal for int()with base 10:'hello'

was int()



基本的schemagic用法
----


模式检查非常灵活,所有检查都是递归执行的。让我们再看一些示例:

**映射模板**:
*如果只提供一个具有一个(可调用)键和一个值的架构*

…代码块::python

>;>;string-to-int-map={str:int}
>;>;schemagic.validate-u-account-schema(string-to-int-map,{"hello":5,"friends":6}
{friends:6,"hello":5}

**map-with-specific key**
**

…代码块::python

>>friend_record={"name":str,"age":int}
>;gt;>;schemagic.根据架构验证(friend戋record,{"name":"tyler","age":400})
{"name":"tyler","age":400}

**序列模板**:
*如果提供的序列仅包含一个项作为架构*

代码块::python

>;>;list_ints=[int]
>;>;schemagic.validate_account_schema(list_ints,[1,2,3,4])
[1,2,3,4]

**严格的顺序**:
*如果提供一个包含多个项目的序列作为一个模式*

密码块:python

>gt>gt>gt>gt>python

>gt>gt>gt;schemagic.validate针对schema进行验证(用3个物品和int str和intstrmap进行验证,[1,"你好",5:"朋友",12:"朋友",12:"和",90:"世界"};
[1,"你好",5:"朋友",12:"朋友",12:"朋友",12:"朋友",12:"朋友",12:"朋友",90:"世界">
[1,"你好",5:"朋友",12:"朋友",12:"朋友",90:"朋友",12:"朋友",90:"朋友",12:"朋友",12:"朋友",12:"朋友",12:"朋友",90:"朋友",12世界"}]

**验证功能离子**:
*如果你提供一个作为模式的函数*

…代码块::python

>;>;def null(数据):
…如果数据不是无:
…raise typeerror("需要nonetype,得到{0}。格式(数据))
>;>schemagic.validate_account_schema(null,none)
>;>schemagic.validate_account_schema(null,"hello!")
回溯(最近一次调用):

类型错误:应为nonetype,got hello


**递归地自动组合架构定义**:
*这是实际值所在的位置*

……代码块::python

>;>;def enum(*可能值):
…定义验证器(数据):
…如果不是可能值中的数据:
…提升值错误()
…返回数据
…返回验证器
>;>>事件={
…"事件类型:枚举("生产"、"开发"),
…事件名称:str
…}
>>gt;调度请求{
…"事件":[事件],
…"请求者":str
…}
>>>schemagic.validate\u according\u schema(dispatch\u request,
…{"事件":[{"事件类型":"开发",
…事件名称":"demo_business_process"},
…{"事件类型":"开发",
…事件名称":"demo_other_business_process"}],
…请求方":"tyler tolton"})
{"事件":[{"事件类型":"开发","事件名称":"演示业务流程"},{"事件类型":"开发","事件名称":"演示其他业务流程"}","请求方":"tyler tolton"}


--------

**使用schemagic.validator进行增量基于消息清晰度和控制**:

…代码块::python

>;>;list_ints_validator=schemagic.validator([int],"业务类型:整数列表")
>;>;list_ints_validator([1,"not an int",3])
回溯(最近一次调用):

valueerror:为业务类型提供的错误值:整数列表。-错误:value error:以10为基数的int()的文本无效:"not an int"架构:[<;type'int'>;]值:[1,'not an int',3]

**提供谓词以有条件地阻止/启用验证**:

代码块::python

>>>>gt>gt>gt>>gt;
>>>>>>>>>>>>当\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\int],"整型列表")
>;>;\uuu envev"
>>>>列出单元验证程序([1,"非单元",3])
回溯(最近一次调用):

值错误:为整数列表提供了错误值。-错误:value error:以10为基数的int()的文本无效:"非int"架构:[<;键入"int">;]值:[1,'非int',3]
>;>;\u envu__="prod"
>;>list_ints_验证器([1,"not an int",3])
[1,"not an int",3]



**验证数据时强制数据**:
*注意:根据架构验证将自动执行此操作。请参阅验证器上的文档。*

…代码块::python

>;>;validate_and_comporte=partial(schemagic.validator,comporte_data=true)
>;>;list_of_ints_validator_and_comporte([int],"integer list")
>;>;list_of_ints_validator_only=schemagic.validator([int],"integer_list")
>;>list戋u int s戋u validator戋only(["1","2","3"])
["1","2","3"]
>;>Ints验证器和强制器的t_(["1","2","3"])
[1,2,3]




schemagic.web
----


服务之间、开发人员之间以及开发团队之间进行通信的简单方法。webservice商业世界是schemagic被锻造的熔炉。准备将自己外包出去。

要演示schemagic.web工作流,让我们承担起世界上第一批发现用python计算fibonacci序列的方法的人的角色。

*注意:这段代码都是从peter norvig优秀的计算机p程序<;https://www.udacity.com/course/design of computer programs--cs212>;` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `代码块::python

def memo(fn):
u cache={}
def f(*args):
尝试:
返回缓存[args]
除了keyerror:
u cache[args]=result=fn(*args)
返回结果
excEPT类型错误:
return f n(*args)

1346269

好吧,现在我们当然想以微服务的形式与世界分享这个发现,这样
其他人就不必知道这个复杂而危险的算法的内部工作原理。

让我们来看看如何在flask中设置这个web服务:

代码块::python

from flask import flask,json
from fibonacci import fib;假设我们在fibonnaci.py


app=flask(


@app.route("/fibonacci/<;index>;")中实现了函数
def web fib端点(index):
尝试:
index=int(index)
除了valueerror:
返回响应(
status=400,
response=/fibonacci/的参数必须是整数"

返回响应(
status=200,
响应=json.dumps(fib(index))




记录输入和输出。
让我们使用schemagic.web实用程序查看此代码的改编版本。

。代码块::python

from flask.app import flask
from fibonacci import fib;假设我们在fibonnaci.py
中实现了schemagic.web import service廑registry

app=flask(廑name廑)中的函数
register廑fibonnacci services=service廑registry(app)

注册fibonnacci服务(
dict(rule="/fibonacci",
输入模式=int,
输出模式=int,
fn=fib))


t是交付本文定义的合同的实现。webservices都统一使用post请求来传输数据。提供给端点的数据来自请求的有效负载。

此代码库使用流行的"git flow"http://nvie.com/posts/a-successful-git-branching-model/>;``模型进行版本控制。fork"the repository"并将其作为development的一个分支(最好使用命名约定功能/您的功能)
。当你完成了你的功能后,提出一个拉入开发的请求。一旦你提出请求,就给"维护者"发电子邮件,让我知道!< Br>最后,如果你有任何问题,如何或什么贡献,请随时发送电子邮件!

…_`存储库`:https://github.com/tjtolton/schemagic
。_`维护者`:tjtolton@gmail.com


documentation
==


这个项目使用sphinx自动生成它的文档,并使用readthedocs托管它。它可以在"here<;http://schemagic.readthedocs.io/en/latest/schemagic.html>;""这里"查看。[1]请注意:此描述改编自"prismatic/schema"的精彩介绍,lt;https://github.com/plumatic/schema>;``clojure library此项目基于

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
摆脱Java中的“for循环”   java如何在EJB3中使用Quartz?   多线程应用程序中的java独立日志文件   lambda如何在Java8流中应用groupby>mapping>Reduce   除了稀疏的Java 2D阵列,什么是有内存意识的替代方案?   java在不同的JUnit测试类之间共享数据的最佳方式是什么   如何在spring中重写JndiObjectFactoryBean并在java中设置解密密码   在Java中使用字符串作为IP创建socket和从文件中读取IP之间的区别?   java如何深度复制不规则二维数组   java创建带有独立ImageIcon的多个JToggleButton   java如何每秒拍摄25张屏幕截图   java Mc bukkit heal插件提供/usage而不是命令   java GridView项目与安卓合并   java为什么这个messageListener不工作?   单元测试Java模拟类方法和注入服务