跳转到主要内容

Python对象的数值特性

项目描述

Travis Build Status Coverage Status

请注意: 此包是实验性的,API可能还会进行一些更改。如果您有任何改进API的建议,请打开一个问题!

关于

此简单模块定义了一个描述符类,可以用于在类上定义数值属性(标量和n维数组)并提供验证这些属性的方法。因此,您无需为每个要定义的属性编写代码,只需简单地进行以下操作:

class Sphere(object):

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, value):
        if value <= 0:
            raise ValueError("Value should be strictly positive")
        if not np.isscalar(value):
            raise TypeError("Value should be a scalar")
        if not np.isreal(value):
            raise TypeError("Value should be numerical")
        self._radius = value

您可以为每个要定义的属性执行以下操作:

from numtraits import NumericalTrait
from traitlets import HasTraits

class Sphere(HasTraits):

    radius = NumericalTrait(domain='strictly-positive', ndim=0)

NumericalTrait 类是在 traitlets 模块的基础上实现的。任何使用 NumericalTrait 定义属性 必须traitlets.HasTraits 类派生。

还支持检查数组的维度和形状(包括将元组和列表转换为数组),以及检查 astropy.unitspintquantities 单位框架的量单位。

安装

此包与Python 2.7,3.3及更高版本兼容,需要 numpytraitlets。如果您想进行单位验证,您还需要 astropypintquantities,具体取决于您通常使用的哪个单位框架。

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

pip install numtraits

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

使用

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

from traitlets import HasTraits
from numtraits import NumericalTrait

class Sphere(HasTraits):

    radius = NumericalTrait(domain='strictly-positive', ndim=0)
    position = NumericalTrait(shape=(3,))

设置属性时,将进行验证

>>> s = Sphere()
>>> s.radius = 1.
>>> s.radius = -3
...
TraitError: radius should be strictly positive
>>> s.radius = [1,2]
...
TraitError: radius should be a scalar value
>>> s.position = (1,2,3)
>>> s.position = 3
...
TraitError: position should be a 1-d sequence
>>> s.position = (1,2,3,4)
...
TraitError: position has incorrect length (expected 3 but found 4)

NumericalTrait 的以下参数可用

  • ndim:将值限制为数组具有此维数

  • shape:将值限制为具有此形状的数组。如果指定,则不需要提供 ndim

  • domain:将值限制在特定的域内 - 可以是 positivestrictly-positivenegativestrictly-negative 或表示值范围的元组。

  • default:未指定时返回的默认值(默认为 None

  • convertible_to:将值限制为具有可以转换为特定单位集的单位(请参阅以下部分)

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

物理单位

虽然 NumericalTrait 可以用于纯标量和 Numpy 数组,但它也可以用于具有关联单位的标量和数组,支持三个流行的单位处理单位:astropy.unitspintquantities

要限制 NumericalTrait 以仅包含特定类型单位的数量,请使用 convertible_to 选项。此选项可从这三个单位包中的任何一个获取单位,并确保传递给任何值的单位等效(但不一定相等)于使用 convertible_to 选项指定的单位。

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

astropy.units 量示例

以下示例显示了如何将 radius 属性限制为以长度单位表示的 astropy.units

from astropy import units as u

class Sphere(HasTraits):
    radius = NumericalTrait(convertible_to=u.m)

将如下行为

>>> s = Sphere()
>>> s.radius = 3. * u.m
>>> s.radius = 4. * u.cm
>>> s.radius = 4. * u.s
...
TraitError: radius should be in units convertible to m

pint 量示例

以下示例显示了如何将 radius 属性限制为以长度单位表示的 pint

from pint import UnitRegistry
ureg = UnitRegistry()

class Sphere(HasTraits):
    radius = NumericalTrait(convertible_to=ureg.m)

将如下行为

>>> s = Sphere()
>>> s.radius = 3. * ureg.m
>>> s.radius = 4. * ureg.cm
>>> s.radius = 4. * ureg.s
...
TraitError: radius should be in units convertible to meter

quantities 量示例

最后,以下示例显示了如何将 radius 属性限制为以长度单位表示的 quantities

import quantities as pq

class Sphere(HasTraits):
    radius = NumericalTrait(convertible_to=pq.m)

将如下行为

>>> s = Sphere()
>>> s.radius = 3. * pq.m
>>> s.radius = 4. * pq.cm
>>> s.radius = 4. * pq.s
...
TraitError: radius should be in units convertible to m

计划支持

  • 属性链接(例如,一个属性应具有与其他属性相同的维度)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分发

numtraits-0.2.tar.gz (8.6 kB 查看哈希值)

上传时间

由以下提供支持