python对象的数值特性

numprops的Python项目详细描述


Travis Build StatusCoverage Status

请注意:此包是实验性的,可能仍会看到一些 对api的更改。如果你对改进api有什么建议, 请打开一个问题!

关于

这个简单的模块定义了一个描述符类,可以用来定义 类和上的数值性质(标量和n维数组) 提供一种验证方法。因此,与其写东西 例如:

classSphere(object):@propertydefradius(self):returnself._radius@radius.setterdefradius(self,value):ifvalue<=0:raiseValueError("Value should be strictly positive")ifnotnp.isscalar(value):raiseTypeError("Value should be a scalar")ifnotnp.isreal(value):raiseTypeError("Value should be numerical")self._value=value

对于要定义的每个属性,您只需执行以下操作:

fromnumpropsimportNumericalPropertyclassSphere(object):radius=NumericalProperty('radius',domain='strictly-positive',ndim=0)

还包括用于检查尺寸和形状的支架 数组(包括将元组和列表转换为数组 以及检查 astropy.unitspint,和 quantities单元框架。

安装

此包与Python2.6、2.7和3.3及更高版本兼容,并且 需要numpy。如果你有兴趣 进行单元验证时,还需要 astropypint,或 quantities,取决于 你通常使用哪个单元框架。

要安装,您可以执行以下操作:

pip install numprops

如果您想的话,还可以将numprops.py捆绑到包中 避免使用外部依赖项,但请确保 文件的版权和许可证。

使用

若要在类上创建自验证的数值属性,请使用 NumericalProperty类:

fromnumpropsimportNumericalPropertyclassSphere(object):radius=NumericalProperty('radius',domain='strictly-positive',ndim=0)position=NumericalProperty('position',shape=(3,))

设置属性时,将验证它:

>>>s=Sphere()>>>s.radius=1.>>>s.radius=-3...ValueError:radiusshouldbestrictlypositive>>>s.radius=[1,2]...TypeError:radiusshouldbeascalarvalue>>>s.position=(1,2,3)>>>s.position=3...TypeError:positionshouldbea1-dsequence>>>s.position=(1,2,3,4)...ValueError:positionhasincorrectlength(expected3butfound4)

NumericalProperty的以下参数可用(在 添加到属性名):

  • ndim:将值限制为具有此维数的数组
  • shape:将值限制为具有此形状的数组。如果 指定的,ndim不需要给定。
  • domain:将值限制为特定域-可以是一个 共positivestrictly-positivenegativestrictly-negative,或表示一系列值的元组。
  • default:要返回的默认值(如果未指定)(默认值 到None
  • convertible_to:将值限制为具有 可转换为一组特定的单元(参见下面的章节)

注意元组和列表将自动转换为numpy 数组,如果它们被认为是有效的。

物理单位

NumericalProperty可用于普通标量和numpy 数组,它也可以用于标量和 单位,支持三个常用的单位处理单位: astropy.unitspint,和 quantities

NumericalProperty限制为具有特定类型 单位,使用convertible_to选项。此选项从 这三个单元包中的任何一个,并将确保传递的任何值 单位等于(但不一定等于)指定的 convertible_to选项。

如果传递给convertible_to的单位是 astropy.units单位,然后是任意 传递给属性的值应该是 astropy.units数量。如果 传递给convertible_to的单元是 pint单位,则任何传递的数量 属性应该是pint 财产最后,如果传递给convertible_to的单元是 quantities单位,然后是任意 传递给属性的数量应为 quantities数量。

astropy.units数量示例

下面的示例演示如何将radius属性限制为 是astropy.units数量 长度单位:

fromastropyimportunitsasuclassSphere(object):radius=NumericalProperty('radius',convertible_to=u.m)

然后将执行以下操作:

>>>s=Sphere()>>>s.radius=3.*u.m>>>s.radius=4.*u.cm>>>s.radius=4.*u.s...ValueError:radiusshouldbeinunitsconvertibletom

品脱数量示例

下面的示例演示如何将radius属性限制为 是一个pint数量,单位为 长度:

frompintimportUnitRegistryureg=UnitRegistry()classSphere(object):radius=NumericalProperty('radius',convertible_to=ureg.m)

然后将执行以下操作:

>>>s=Sphere()>>>s.radius=3.*ureg.m>>>s.radius=4.*ureg.cm>>>s.radius=4.*ureg.s...ValueError:radiusshouldbeinunitsconvertibletometer

数量数量示例

最后,下面的示例演示如何限制radius 属性为quantities 长度单位数量:

importquantitiesaspqclassSphere(object):radius=NumericalProperty('radius',convertible_to=pq.m)

然后将执行以下操作:

>>>s=Sphere()>>>s.radius=3.*pq.m>>>s.radius=4.*pq.cm>>>s.radius=4.*pq.s...ValueError:radiusshouldbeinunitsconvertibletom

计划支持

  • 产权联结s(例如,一个属性应该具有相同的 其他尺寸)

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

推荐PyPI第三方库


热门话题
java使用并发hashmap减少线程池的内存使用?   java为什么在提交片段后出现错误“getParentActivityIntent:badActivity name”?   vim UltiSnips扩展java包   java给出了一个名称列表,如何插入、删除、显示、搜索和退出?Java程序   java Spring集成:只从FTP服务器下载新的或更新的文件?   使用Java中的Scala:将函数作为参数传递   java线程1每秒填充一个映射,线程2每60秒保存一个条目   java从私有类访问公共类中的方法/字段   如何使用ApacheSpark流媒体和JavaAPI从所有人那里获取英语推文?   java是否可以在父标记和子标记中编写XMLSchemainstance?   java无法读取文本文件,也找不到解决方案   java在运行时加载类时无法创建bean