扩展其他类以通过注释进行类型检查

TypedClass的Python项目详细描述


类型类

一个python类,用于扩展其他类,让它们对带注释的属性进行类型检查,以及其他一些好的东西,如必需的属性、不可变的属性、属性的验证函数、属性的选择

示例用法

类型类

fromTypedClassimportTypedClass,TypeDefclassExampleTypedClass(TypedClass):simple:intusing_type_def:TypeDef(typeof=int)simple_with_default:float=1.01type_hint_with_default:TypeDef(typeof=int)=22all_options:TypeDef(typeof=int,required=True,immutable=True,choices=[21,22,23],validate_fn=lambdax:x>20)=23def__init__(self,simple:int,using_type_def:int,simple_with_default:float=1.01,type_hint_with_default:int=22,all_options:int=23,):super().__init__(simple=simple,using_type_def=using_type_def,simple_with_default=simple_with_default,type_hint_with_default=type_hint_with_default,all_options=all_options)

类型dclassstrict

与上面一样,默认情况下,只有所有内容都设置为必需和不可变。

类型DClassJSON

fromTypedClassimportTypedClassJsonclassExampleJSONValidationUsageObj(TypedClassJson):name:strvalue:strvalid:boolclassExampleJSONValidationUsageWithHelperClass(TypedClassJson):_id:intsender:strkind:strnested_obj:ExampleJSONValidationUsageObjnested_obj_with_help:ExampleJSONValidationUsageObjexample_json={'_id':1,'sender':'nic','kind':'message','nested_obj':{'name':'nested_obj','value':'cool','valid':True},'nested_obj_with_help':{'name':'nested_obj_with_help','value':'very cool','valid':True}}# ...importunittestjson_output_example=ExampleJSONValidationUsageWithHelperClass(example_json)tc=unittest.TestCase('__init__')tc.assertEqual(example_json,json_output_example.dict)

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

推荐PyPI第三方库


热门话题
Java例外。Lang.Stringindexoutofboundsexception索引超出范围(0)   java Spring引导Freemarker从2.2.0升级失败   重构Java反模式名称?包含对象的对象包含。。。等   用java处理JDBC可能出现的死锁的最佳方法   java无法访问主线程上的数据库,因为它可能会在很长一段时间内锁定UI   java如何将固定大小的画布包装在边框窗格中的滚动窗格居中?   java解析xsd文件后得到空结果   在html页面中表示XML文件的java   socketjava对象流   sql Java越界异常数据库   JavaJBoss7.1.1不会在Mavericks上启动   Twincat ADS事件驱动读取在一段时间后停止工作(Java)   java MyBatis使用生成的ID插入所有   Mojave上缺少MacOS Java控制面板   JavaGuice:如果多次注入相同的依赖项,是否注入了该依赖项的相同实例?