运行时用于变量类型检查器/验证器/转换器的python库。
pytypeutil的Python项目详细描述
用法
类型检查
>>> frompytypeutil.typeimportInteger>>> Integer(1).is_type()True >>> Integer(1.1).is_type()False
类型验证
>>> frompytypeutil.typeimportInteger>>> Integer(1).validate()>>> try:... Integer(1.1).validate()... exceptTypeErrorase:... print(e)...invalid value type: expected=INTEGER, actual=<type 'float'>
类型转换
转换
>>> frompytypeutil.typeimportInteger>>> frompytypeutilimportTypeConversionError>>> Integer("1").convert()1 >>> try:... Integer(1.1).convert()... exceptTypeConversionErrorase:... print(e)...failed to convert from float to INTEGER
尝试转换
>>> frompytypeutil.typeimportInteger>>> Integer("1").try_convert()1 >>> print(Integer(1.1).try_convert())None
强制转换
>>> frompytypeutil.typeimportInteger>>> Integer("1").force_convert()1 >>> Integer(1.1).force_convert()1
功能
支持的类型如下:
- 布尔
- 日期时间
- dict
- 内景
- 浮动
- 实数
- 无限
- 不是数字
- 无
- str
- 空字符串
类型检查/验证/转换结果将根据^{tt1}决定$ 可以传递给构造函数。API引用可以在文档中找到。
安装
pip install pytypeutil