跳转到主要内容

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._value = value

for each property you want to define, you can simply do

from numprops import NumericalProperty

class Sphere(object):

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

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

安装

此包与Python 2.6、2.7和3.3以及更高版本兼容,并需要numpy。如果您有兴趣进行单位验证,您还需要astropypintquantities,具体取决于您通常使用的哪个单位框架。

要安装,您可以这样做:

pip install numprops

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

使用

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

from numprops import NumericalProperty

class Sphere(object):

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

当设置属性时,它将被验证

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

NumericalProperty的以下参数可用(除属性名称外)

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

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

  • :将值限制在特定域内 - 可以是以下之一:正数严格正数负数严格负数,或表示值范围的元组。

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

  • 可转换为:将值限制为可以转换为特定单位集合的值(参见下文章节)

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

物理单位

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

要限制 NumericalProperty 到具有某种类型单位的量,请使用 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(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: radius should be in units convertible to m

pint 量示例

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

from pint import UnitRegistry
ureg = UnitRegistry()

class Sphere(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: radius should be in units convertible to meter

quantities 量示例

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

import quantities as pq

class Sphere(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: radius should be in units convertible to m

计划支持

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

项目详情


下载文件

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

源分发

numprops-0.1.tar.gz (8.2 kB 查看散列)

上传时间

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面