python类型提示模块包_Python checktypes包_程序模块 - PyPI - Python中文网

checktypes软件包

用于创建实用程序类的库,为类型提供了良好的抽象

检查和数据验证。

基本示例

创建

面向对象的api

在要继承的CheckType旁边选择一个基类并定义一个predicate()staticmethod或classmethod>>>fromchecktypesimportCheckType>>>classPositiveInteger(int,CheckType):..."'int' > 0"...@staticmethod...defpredicate(n):...returnn>0...>>>classHumanAge(int,CheckType):..."'int' between 0 and 125"...minval=0...maxval=125...@classmethod...defpredicate(cls,val):...returncls.minval<=val<=cls.maxval...

或者,也可以使用lambdas来定义较短的类。>>>classPositiveInteger(int,CheckType):..."'int' > 0"...predicate=lambdan:n>0...>>>classHumanAge(int,CheckType):..."'int' between 0 and 125"...minval=0...maxval=125...predicate=classmethod(lambdacls,n:cls.minval<=n<=cls.maxval)...

功能api

另一种方法是使用checktype()工厂。>>>fromchecktypesimportchecktype>>>PositiveInteger=checktype('PositiveInteger',int,lambdan:n>0,"'int' > 0")>>>HumanAge=checktype(...'HumanAge',int,doc="'int' between 0 and 125",minval=0,maxval=125,...predicate=classmethod(lambdacls,n:cls.minval<=n

用法

isinstance()超载>>>isinstance(1,PositiveInteger)True>>>isinstance(-1,PositiveInteger)False>>>isinstance('a',PositiveInteger)False

validate()类方法>>>PositiveInteger.validate(1)# No output => the value is a valid one>>>PositiveInteger.validate(-1)Traceback(mostrecentcalllast):...ValueError:expected'PositiveInteger'('int'>0)butgot-1>>>PositiveInteger.validate('a')Traceback(mostrecentcalllast):...TypeError:expected'PositiveInteger'('int'>0)butgot'str'

register()类方法>>>isinstance(0,PositiveInteger)False>>>PositiveInteger.validate(0)Traceback(mostrecentcalllast):...ValueError:expected'PositiveInteger'('int'>0)butgot0>>>PositiveInteger.register(0)# Now let pass 0>>>isinstance(0,PositiveInteger)True>>>PositiveInteger.validate(0)

as_descriptor()类方法>>>classCircle:...radius=PositiveInteger.as_descriptor()...>>>c=Circle()>>>c.radius=1>>>c.radius=-1Traceback(mostrecentcalllast):...ValueError:expected'PositiveInteger'('int'>0)butgot-1for'radius'attributeof'Circle'object>>>c.radius='a'Traceback(mostrecentcalllast):...TypeError:expected'PositiveInteger'('int'>0)butgot'str'for'radius'attributeof'Circle'object

checktyped带有类型提示(3.6+样式)的decorator>>>fromchecktypesimportchecktyped>>>@checktyped...classPoint2D:...x:float...y:float...>>>p=Point2D()>>>p.x=0.0>>>p.y=1.0>>>p.x='a'Traceback(mostrecentcalllast):...TypeError:expected'float'butgot'str'for'x'attributeof'Point2D'object

实例化

根据概念CheckTypes最初并不打算被实例化。但既然这是一项普通的任务

为了将一个值转换成另一个类型,添加了支持以使构造函数返回一个值

与python中的标准类具有相同的规则,但有三项除外:

1-返回的值永远不是类的实例,而是一个类的实例。>>>PositiveInteger(1)1>>>n=PositiveInteger(1)>>>print(n,type(n),sep=': ')1:

2-如果该值不满足isinstance()检查,则会提高ValueError。>>>PositiveInteger(-1)Traceback(mostrecentcalllast):...ValueError:-1cannotbeinterpretedasa'PositiveInteger'('int'>0)

3-__init__()和__new__()被忽略。>>>classMyInt(int,CheckType):...def__new__(cls,x):...return'unexpected thing'...def__init__(self,x):...self.my_attr='some value'...>>>x=MyInt(1)>>>x1>>>x.my_attrTraceback(mostrecentcalllast):...AttributeError:'int'objecthasnoattribute'my_attr'

仍然可以提供两个类属性来添加对更好实例化的支持:

1-default

如果不带参数调用类,则它提供要返回的值。

它的优点之一是:它解决了默认值不合适的问题。>>>classNegativeInteger(int,CheckType):...default=-1...predicate=lambdan:n<0...>>>NegativeInteger()-1>>>delNegativeInteger.default>>>NegativeInteger()# int() -> 0Traceback(mostrecentcalllast):...ValueError:0cannotbeinterpretedasa'NegativeInteger'

2-factory()

它是一个可调用的,负责返回新对象。

从ABC继承时特别有用。>>>fromcollections.abcimportSized>>>classThreePlace(Sized,CheckType):...factory=tuple...predicate=lambdas:len(s)==3...>>>ThreePlace(range(1,4))(1,2,3)>>>ThreePlace([4,5,6])(4,5,6)>>>ThreePlace('789')('7','8','9')

请注意,返回值仍将被检查。>>>defbadfactory(*args,**kwarg):...return'bad value'...>>>ThreePlace.factory=badfactory>>>ThreePlace((1,2,3))Traceback(mostrecentcalllast):...ValueError:'bad value'cannotbeinterpretedasa'ThreePlace'>>>delThreePlace.factory>>>ThreePlace.default=0>>>ThreePlace()Traceback(mostrecentcalllast):...TypeError:'int'objectcannotbeinterpretedasa'ThreePlace'

有关其他示例,请参见Recipes.md。

欢迎加入QQ群-->: 979659372

推荐PyPI第三方库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值