用于数据验证的python类型库。
typed的Python项目详细描述
此库允许类型测试和转换,并可用于数据加载、存储和验证。
用法
简单类型检查:
import typed t1 = typed.int | typed.string assert t1.test('abc') assert t1.test(u'xyz') assert t1.test(30) assert not t1.test(1.2) assert not t1.test(True) assert not t1.test(None) t2 = typed.list(typed.int) assert t1.test([1, 2, 3]) assert t1.test([]) assert not t1.test(['a', 'b', 'c']) t3 = typed.list(typed.int | typed.string) assert t3.test([1, 'a', 2, 'b']) t4 = typed.list(typed.int) | typed.list(typed.string) assert t4.test([1, 2, 3]) assert t4.test(['a', 'b', 'c']) assert not t4.test([1, 'a', 2, 'b']) t5 = typed.set(1, 2, 3) assert t5.test(2) assert not t5.test(4) t6 = typed.dict({ 'a': typed.int, 'b': typed.bool.optional, 'c': (typed.int | typed.none).optional, })