{ "info": { "author": "Thomas Robitaille", "author_email": "thomas.robitaille@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "Numerical properties for Python objects\n=======================================\n\n|Travis Build Status| |Coverage Status|\n\n**Please note:** this package is experimental and may still see some\nchanges to the API. If you have any suggestions for improving the API,\nplease open an issue!\n\nAbout\n-----\n\nThis simple module defines a descriptor class that can be used to define\nnumerical properties (scalar and n-dimensional arrays) on classes and\nprovide a way to validate these. Thus, instead of writing something\nlike:\n\n.. code:: python\n\n class Sphere(object):\n\n @property\n def radius(self):\n return self._radius\n\n @radius.setter\n def radius(self, value):\n if value <= 0:\n raise ValueError(\"Value should be strictly positive\")\n if not np.isscalar(value):\n raise TypeError(\"Value should be a scalar\")\n if not np.isreal(value):\n raise TypeError(\"Value should be numerical\")\n self._radius = value\n\nfor each property you want to define, you can simply do:\n\n.. code:: python\n\n from numtraits import NumericalTrait\n from traitlets import HasTraits\n\n class Sphere(HasTraits):\n\n radius = NumericalTrait(domain='strictly-positive', ndim=0)\n\nThe ``NumericalTrait`` class is implemented on top of the\n`traitlets `__ module. Any class\nusing ``NumericalTrait`` for the definition of a property **must**\nderive from the ``traitlets.HasTraits`` class.\n\nSupport is also included for checking the dimensionality and shape of\narrays (which includes converting tuples and lists to arrays\non-the-fly), as well as checking the units of quantities for the\n`astropy.units `__,\n`pint `__, and\n`quantities `__ unit frameworks.\n\nInstalling\n----------\n\nThis package is compatible with Python 2.7, 3.3 and later, and requires\n`numpy `__ and\n`traitlets `__. If you are\ninterested in doing unit validation, you will also need\n`astropy `__,\n`pint `__, or\n`quantities `__, depending on\nwhich unit framework you normally use.\n\nTo install, you can do:\n\n::\n\n pip install numtraits\n\nYou can also bundle ``numtraits.py`` into your package if you want to\navoid using an external dependency, but please be sure to keep the\ncopyright and the license in the file.\n\nUsing\n-----\n\nTo create self-validating numerical properties on a class, use the\n``NumericalTrait`` class:\n\n.. code:: python\n\n from traitlets import HasTraits\n from numtraits import NumericalTrait\n\n class Sphere(HasTraits):\n\n radius = NumericalTrait(domain='strictly-positive', ndim=0)\n position = NumericalTrait(shape=(3,))\n\nWhen a property is set, it will be validated:\n\n.. code:: python\n\n >>> s = Sphere()\n >>> s.radius = 1.\n >>> s.radius = -3\n ...\n TraitError: radius should be strictly positive\n >>> s.radius = [1,2]\n ...\n TraitError: radius should be a scalar value\n >>> s.position = (1,2,3)\n >>> s.position = 3\n ...\n TraitError: position should be a 1-d sequence\n >>> s.position = (1,2,3,4)\n ...\n TraitError: position has incorrect length (expected 3 but found 4)\n\nThe following arguments to ``NumericalTrait`` are available:\n\n- ``ndim``: restrict the values to arrays with this number of dimension\n- ``shape``: restrict the values to arrays with this shape. If\n specified, ``ndim`` does not need to be given.\n- ``domain``: restrict the values to a particular domain - can be one\n of ``positive``, ``strictly-positive``, ``negative``,\n ``strictly-negative``, or a tuple representing a range of values.\n- ``default``: the default value to return, if not specified (defaults\n to ``None``)\n- ``convertible_to``: restrict the values to ones with units that would\n be convertible to a specific set of units (see section below)\n\nNote that tuples and lists will automatically get converted to Numpy\narrays, if they are considered valid.\n\nPhysical units\n--------------\n\nWhile ``NumericalTrait`` can be used for plain scalars and Numpy arrays,\nit can also be used for scalars and arrays which have associated units,\nwith support for three popular unit handling units:\n`astropy.units `__,\n`pint `__, and\n`quantities `__.\n\nTo restrict a ``NumericalTrait`` to quantities with a certain type of\nunit, use the ``convertible_to`` option. This option takes units from\nany of these three unit packages, and will ensure that any value passed\nhas units equivalent (but not necessarily equal) to those specified with\nthe ``convertible_to`` option.\n\nIf the units passed to ``convertible_to`` are\n`astropy.units `__ units, then any\nvalue passed to the property should then be an\n`astropy.units `__ quantity. If the\nunits passed to ``convertible_to`` are\n`pint `__ units, then any quantity passed\nto the property should be a `pint `__\nproperty. And finally if the units passed to ``convertible_to`` are\n`quantities `__ units, then any\nquantity passed to the property should be a\n`quantities `__ quantity.\n\nastropy.units Quantity example\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe following example shows how to restrict the ``radius`` property to\nbe an `astropy.units `__ quantity in\nunits of length:\n\n.. code:: python\n\n from astropy import units as u\n\n class Sphere(HasTraits):\n radius = NumericalTrait(convertible_to=u.m)\n\nwill then behave as follows:\n\n.. code:: python\n\n >>> s = Sphere()\n >>> s.radius = 3. * u.m\n >>> s.radius = 4. * u.cm\n >>> s.radius = 4. * u.s\n ...\n TraitError: radius should be in units convertible to m\n\npint Quantity example\n~~~~~~~~~~~~~~~~~~~~~\n\nThe following example shows how to restrict the ``radius`` property to\nbe a `pint `__ quantity in units of\nlength:\n\n.. code:: python\n\n from pint import UnitRegistry\n ureg = UnitRegistry()\n\n class Sphere(HasTraits):\n radius = NumericalTrait(convertible_to=ureg.m)\n\nwill then behave as follows:\n\n.. code:: python\n\n >>> s = Sphere()\n >>> s.radius = 3. * ureg.m\n >>> s.radius = 4. * ureg.cm\n >>> s.radius = 4. * ureg.s\n ...\n TraitError: radius should be in units convertible to meter\n\nquantities Quantity example\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nFinally, the following example shows how to restrict the ``radius``\nproperty to be a `quantities `__\nquantity in units of length:\n\n.. code:: python\n\n import quantities as pq\n\n class Sphere(HasTraits):\n radius = NumericalTrait(convertible_to=pq.m)\n\nwill then behave as follows:\n\n.. code:: python\n\n >>> s = Sphere()\n >>> s.radius = 3. * pq.m\n >>> s.radius = 4. * pq.cm\n >>> s.radius = 4. * pq.s\n ...\n TraitError: radius should be in units convertible to m\n\nPlanned support\n---------------\n\n- Linking of properties (e.g. a property should have the same\n dimensions as another)\n\n.. |Travis Build Status| image:: https://travis-ci.org/astrofrog/numtraits.svg?branch=master\n :target: https://travis-ci.org/astrofrog/numtraits\n.. |Coverage Status| image:: https://coveralls.io/repos/astrofrog/numtraits/badge.svg\n :target: https://coveralls.io/r/astrofrog/numtraits", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/astrofrog/numtraits", "keywords": null, "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "numtraits", "package_url": "https://pypi.org/project/numtraits/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/numtraits/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/astrofrog/numtraits" }, "release_url": "https://pypi.org/project/numtraits/0.2/", "requires_dist": null, "requires_python": null, "summary": "Numerical traits for Python objects", "version": "0.2" }, "last_serial": 1735072, "releases": { "0.2": [ { "comment_text": "", "digests": { "md5": "fa635d4202af8eae4f34e3ce390ac265", "sha256": "2fca9a6c9334f7358ef1a3e2e64ccaa6a479fc99fc096910e0d5fbe8edcdfd7e" }, "downloads": -1, "filename": "numtraits-0.2.tar.gz", "has_sig": false, "md5_digest": "fa635d4202af8eae4f34e3ce390ac265", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8612, "upload_time": "2015-09-23T16:12:04", "url": "https://files.pythonhosted.org/packages/5b/5e/53a2bc57030f611ae48ab3df7410eb6bce2e666b566f4b8809f1468f4161/numtraits-0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "fa635d4202af8eae4f34e3ce390ac265", "sha256": "2fca9a6c9334f7358ef1a3e2e64ccaa6a479fc99fc096910e0d5fbe8edcdfd7e" }, "downloads": -1, "filename": "numtraits-0.2.tar.gz", "has_sig": false, "md5_digest": "fa635d4202af8eae4f34e3ce390ac265", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8612, "upload_time": "2015-09-23T16:12:04", "url": "https://files.pythonhosted.org/packages/5b/5e/53a2bc57030f611ae48ab3df7410eb6bce2e666b566f4b8809f1468f4161/numtraits-0.2.tar.gz" } ] }