grpc.io的异步包装器
pychecktype的Python项目详细描述
可以处理递归类型和数据的类型检查器
文件:http://pychecktype.readthedocs.io/en/latest/
安装
pip install pychecktype
基本用法
frompychecktypeimportcheck_typecheck_type({"abc":[1,2,3],"def":{"test":"abc"}},{"abc":[int],"def":{"test":[str]}})# Returns: {"abc": [1,2,3], "def": {"test": ["abc"]}}
突出显示
这个实现最有趣的地方是 支持递归类型和数据,例如:
frompychecktypeimportcheck_typemy_type=[]my_type.append((int,my_type))# my_type accepts: recursive lists with only sub-list and integers with any depth - even infinitecheck_type([],my_type)# []check_type([1,2,3,[1,2],[1,2,[3,4]]],my_type)# [1,2,3,[1,2],[1,2,[3,4]]]check_type([1,2,3,[1,2],[1,2,["3",4]]],my_type)# failedmy_obj=[]my_obj.append(my_obj)my_obj.append(2)check_type(my_obj,my_type)# [[...], 2]
规则
这个类型检查器有一些适合yaml的特殊规则。为了 例如,此类型检查器接受列表类型的单个值, 并将该值转换为[value]。
这个类型检查器使用一个稍微简单易读的dsl规则 与其他库(如typing和trafaret)相比,大多数库都是 python内置对象。
check_type方法不仅检查值是否与 给定的类型;它返回该对象的已更正的版本。
一般情况下:
- python类型匹配该类型中的任何对象(例如str、int),除了:
- str和unicode始终匹配str和unicode对象 在Python2和Python3中
- int和long中的int和long对象始终匹配 python 2和python 3
- bool对象从不与int或long匹配,它们是 仅与bool匹配(尽管bool是int的子类)
特别是,object匹配任何值,包括None。帮手 提供的类NoMatch与任何实例都不匹配,它可以是 嵌入到其他类型中以创建断言。
- None只匹配None(相当于NoneType)
- 元组类型:
- ()匹配任何对象,除了None
- 多个有效类型的元组(type1, type2, ...)尝试 从左到右将对象与每个子类型匹配。例如, (str, int)匹配str对象或int对象;(str, None) 匹配str对象或不匹配
- 按类型列出:
[]匹配任何列表,或将对象转换为包含 对象
[type]匹配所有与内部类型匹配的项的列表,或者 将与内部类型匹配的对象转换为列表 包含它
默认情况下,列表类型同时匹配list对象和tuple 对象,并将它们转换为列表。例如,[int]匹配 (1,2,3)并返回[1,2,3]。使用list_factory方法 创建只接受 明确规定。您也可以使用它来接受更适合的类型,例如。 set
默认情况下,列表类型可以将非列表对象转换为列表 仅包含该对象,例如1到[1],{"a":1}到 [{"a":1}]。当对象本身 是列表/元组,例如[list]不能与[1]匹配,因为它是 不允许转换为[[1]]。
您可以通过创建自定义列表类型来禁用转换 使用list_factory方法和strict=True
列表类型返回输入列表的浅层副本。
- 按类型分布:
{}匹配任何dict
当dict包含键值对时,它们将成为 输入指令:
1)。钥匙从'!'是必需的密钥,相应的 值是一种类型。输入dict中指定键的值 必须与类型dict中指定的类型匹配。
(第二章)。钥匙以'?'是可选密钥,不需要 出现在输入指令中,但如果它们出现,则必须匹配 使用dict类型中的值。
(第三章)。以“~”开头的键是正则表达式。所有的钥匙 与正则表达式匹配的输入dict,后跟 “~”,相应的值必须与指定的类型匹配。 正则表达式只匹配不需要的键或 可选钥匙。
(四)。其他密钥被视为必需的密钥(好像它们是 准备好了!')
5条)。输入dict中的额外键不会影响匹配。你可以用 '~': NoMatch禁用额外的键。
示例:
`{"abc": int}` matches `{"abc": 1}` and `{"abc": 1, "d": 2}` but not `{"d": 2}` `{"!abc": int}` matches `{"abc": 1}` and `{"abc": 1, "d": 2}` but not `{"d": 2}` `{"?abc": int}` matches `{"abc": 1}`, `{"abc": 1, "d": 2}` and `{"d": 1}`, but not `{"abc": "a"}` `{"~a.b": int}` matches `{"acb": 1}` but not `{"facbg": "a"}` because "facbg" is matched by 'a.b' `{"~a.b": int, "adb": str}` matches `{"adb": "abc"}` but not `{"adb": 1}`
- tuple_((type1, type2, type3, ...))创建自定义类型 (元组类型)匹配任何元组/列表,该元组/列表正好包含 相同数量的项,每个项都匹配相应的子类型。
- map_(key_type, value_type)创建自定义类型(映射类型) 它匹配任何dict,其中每个键都匹配key_type, 每个值都匹配value_类型
- extra_和class_是高级自定义类型,它们是 输入对象的自定义附加检查,例如检查 正则表达式等。
有关详细信息,请参见pychecktype.py中的docstring。
python 3注释检查
您可以使用pychecktype.checked.checked
decorator检查函数的输入参数和返回值
frompychecktype.checkedimportchecked@checkeddeff(a:str,b:int)->str:""" check `a` is str, `b` is int, and returns str """returna+str(b)@checkeddeff2(a,b:int):""" You may check only part of the parameters. """returnstr(a)+str(b)@checkedasyncdeff3(a:str,*args:[int],**kwargs:{'?join':bool})->str:""" Async functions are decorated to async functions *args , keyword-only arguments and **kwargs can also be checked """ifkwargs.get('join'):returna.join(str(v)forvinargs)else:returna+str(sum(args))fromfunctoolsimportwrapsdeftestdecorator(f):@wrapsdef_f(*args,**kwargs):print("Wrapped")returnf(*args,**kwargs)@checked@testdecoratordeff4(a:int):""" Works well with decorators that are correctly using `functools.wraps` and not modifying the argument list """returna+1