physicalproperty - Descriptor class for physical property attributes

Scope

This module features a PhysicalProperty descriptor class which is useful for implementing class attributes which model a physical property. Specifically, PhysicalProperty provides class attributes with:

  • Units
  • Optional upper and/or lower bounds

Units and upper/lower bounds are defined along with the class definition; any attempt to set the attribute with an incompatible unit or a value out-of-bounds will raise an exception.

Constraining class attributes in this way provides assurance that, for example, an absolute temperature cannot accidentally be set to a negative value, a voltage can’t accidentally be set to a capacitance, etc.

Documentation

Located on ReadTheDocs.

Installation

Dependencies

The physicalproperty module currently only works with python 2.7.

Since this package depends on numpy, I’ve eschewed the tradiational pip/pypi approach for packaging and distribution and instead opted for conda/binstar. I recommend using the most recent release of the Anaconda scientific python distribution by Continuum Analytics to mitigate the difficulty of installing numpy on your system. Once you’ve installed anaconda, you can install the most recent version of physicalproperty like so:

conda install -c jrsmith3 physicalproperty

Building from source

First, clone the git repo or uncompress a tarball of the source. Second, execute the following command:

conda build path/to/physicalproperty/conda.recipe

Finally, install the built package with:

conda install physicalproperty --use-local

Examples

Lets say you want to implement a class which will return the energy flux of blackbody emission.

>>> from astropy import units, constants
>>> from physicalproperty import PhysicalProperty
>>> class blackbody(object):
...     """
...     Model blackbody radiator
...     """
...     temp = PhysicalProperty(unit="K", lo_bnd=0)
...     emissivity = PhysicalProperty(lo_bnd=0, up_bnd=1)
...     def __init__(self, temp, emissivity=1):
...         self.temp = temp
...         self.emissivity = emissivity
...     def energy_flux(self):
...         """
...         Energy flux from blackbody radiator
...         """
...         flux = constants.sigma_sb * self.emissivity * self.temp**4
...         return flux.to("W/m2")
>>> bb = blackbody(temp=300)
>>> bb.temp
<Quantity 300.0 K>
>>> bb.energy_flux()
<Quantity 459.30021300000004 W / m2>
>>> bb.temp = -10.2
Traceback (most recent call last):
ValueError: Cannot set less than 0.0 K

License

The code is licensed under the MIT license. You can use this code in your project without telling me, but it would be great to hear about who’s using the code. You can reach me at joshua.r.smith@gmail.com.

Contributing

The repository is hosted on github . Feel free to fork this project and/or submit a pull request. Please notify me of any issues using the issue tracker .

In the unlikely event that a community forms around this project, please adhere to the Python Community code of conduct.

Version numbers follow the PEP440 rubric. Versions will have three components: major.minor.patch. These components can be understood within the semver rubric.

API Reference

Base Library (physicalproperty)

class physicalproperty.PhysicalProperty(unit=<MagicMock name='mock.units.dimensionless_unscaled' id='140710543518928'>, up_bnd=<MagicMock name='mock.inf' id='140710543019920'>, lo_bnd=<MagicMock name='mock.inf.__neg__()' id='140710543099280'>)

Descriptor class of a public data attribute of a physical property.

Physical properties have several characteristics that are different from plain numerical data:

  • upper and/or lower bounds
  • numerical type
  • units

The purpose of this class is to constrain data that represents a physical quantity according to the specifications in the above list.

physicalproperty.find_PhysicalProperty(obj)

List of data attributes of obj implemented as PhysicalProperty descriptors.