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

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@Override   使用BuffereImage加载映像时java高ram使用率   java For循环混乱,为什么不是循环?   java Android网格视图字符串对齐问题   java如何将方法与比较类型的附加功能进行比较?   在Java Swing中放置JSepator后的间隙大小   java如何避免并发访问我的网站中的支付链接   java如何从现有的Unix服务器连接到FTP服务器?   Spring中的java用户相关bean定义   带有scribesjava库的wordpress Woocommerce REST API返回消费者密钥参数缺失错误消息   java我可以自动检测特定设备连接的串行端口吗?   Javafx棋盘游戏   java使用JTextPane显示HTML,支持SVG吗?   SpringBoot如何在java中将映射转换为实体对象?   如何使用java代码对xls文件进行密码保护   Java JPA(EclipseLink)如何在持久化实际实体之前接收下一个生成的值?   Javaservlet启动外部进程