| 1 |
""" |
| 2 |
pint.facets |
| 3 |
~~~~~~~~~~~ |
| 4 |
|
| 5 |
Facets are way to add a specific set of funcionalities to Pint. It is more |
| 6 |
an organization logic than anything else. It aims to enable growth while |
| 7 |
keeping each part small enough to be hackable. |
| 8 |
|
| 9 |
Each facet contains one or more of the following modules: |
| 10 |
- definitions: classes describing specific unit-related definitons. |
| 11 |
These objects must be immutable, pickable and not reference the registry (e.g. ContextDefinition) |
| 12 |
- objects: classes and functions that encapsulate behavior (e.g. Context) |
| 13 |
- registry: implements a subclass of PlainRegistry or class that can be |
| 14 |
mixed with it (e.g. ContextRegistry) |
| 15 |
|
| 16 |
In certain cases, some of these modules might be collapsed into a single one |
| 17 |
as the code is very short (like in dask) or expanded as the code is too long |
| 18 |
(like in plain, where quantity and unit object are in their own module). |
| 19 |
Additionally, certain facets might not have one of them. |
| 20 |
|
| 21 |
An important part of this scheme is that each facet should export only a few |
| 22 |
classes in the __init__.py and everything else should not be accessed by any |
| 23 |
other module (except for testing). This is Python, so accessing it cannot be |
| 24 |
really limited. So is more an agreement than a rule. |
| 25 |
|
| 26 |
It is worth noticing that a Pint Quantity or Unit is always connected to a |
| 27 |
*specific* registry. Therefore we need to provide a way in which functionality |
| 28 |
can be added to a Quantity class in an easy way. This is achieved beautifully |
| 29 |
using specific class attributes. For example, the NumpyRegistry looks like this: |
| 30 |
|
| 31 |
class NumpyRegistry: |
| 32 |
|
| 33 |
Quantity = NumpyQuantity |
| 34 |
Unit = NumpyUnit |
| 35 |
|
| 36 |
This tells pint that it should use NumpyQuantity as base class for a quantity |
| 37 |
class that belongs to a registry that has NumpyRegistry as one of its bases. |
| 38 |
|
| 39 |
Currently the folowing facets are implemented: |
| 40 |
|
| 41 |
- plain: basic manipulation and calculation with multiplicative |
| 42 |
dimensions, units and quantities (e.g. length, time, mass, etc). |
| 43 |
|
| 44 |
- nonmultiplicative: manipulation and calculation with offset and |
| 45 |
log units and quantities (e.g. temperature and decibel). |
| 46 |
|
| 47 |
- measurement: manipulation and calculation of a quantity with |
| 48 |
an uncertainty. |
| 49 |
|
| 50 |
- numpy: using numpy array as magnitude and properly handling |
| 51 |
numpy functions operating on quantities. |
| 52 |
|
| 53 |
- dask: allows pint to interoperate with dask by implementing |
| 54 |
dask magic methods. |
| 55 |
|
| 56 |
- group: allow to make collections of units that can be then |
| 57 |
addressed together. |
| 58 |
|
| 59 |
- system: redefine base units for dimensions for a particular |
| 60 |
collection of units (e.g. imperial) |
| 61 |
|
| 62 |
- context: provides the means to interconvert between incompatible |
| 63 |
units through well defined relations (e.g. spectroscopy allows |
| 64 |
converting between spatial wavelength and temporal frequency) |
| 65 |
|
| 66 |
:copyright: 2022 by Pint Authors, see AUTHORS for more details. |
| 67 |
:license: BSD, see LICENSE for more details. |
| 68 |
""" |
| 69 |
|
| 70 |
from __future__ import annotations |
| 71 |
|
| 72 |
from .context import ContextRegistry, GenericContextRegistry |
| 73 |
from .dask import DaskRegistry, GenericDaskRegistry |
| 74 |
from .group import GenericGroupRegistry, GroupRegistry |
| 75 |
from .measurement import GenericMeasurementRegistry, MeasurementRegistry |
| 76 |
from .nonmultiplicative import ( |
| 77 |
GenericNonMultiplicativeRegistry, |
| 78 |
NonMultiplicativeRegistry, |
| 79 |
) |
| 80 |
from .numpy import GenericNumpyRegistry, NumpyRegistry |
| 81 |
from .plain import GenericPlainRegistry, MagnitudeT, PlainRegistry, QuantityT, UnitT |
| 82 |
from .system import GenericSystemRegistry, SystemRegistry |
| 83 |
|
| 84 |
__all__ = [ |
| 85 |
"ContextRegistry", |
| 86 |
"DaskRegistry", |
| 87 |
"FormattingRegistry", |
| 88 |
"GroupRegistry", |
| 89 |
"MeasurementRegistry", |
| 90 |
"NonMultiplicativeRegistry", |
| 91 |
"NumpyRegistry", |
| 92 |
"PlainRegistry", |
| 93 |
"SystemRegistry", |
| 94 |
"GenericContextRegistry", |
| 95 |
"GenericDaskRegistry", |
| 96 |
"GenericFormattingRegistry", |
| 97 |
"GenericGroupRegistry", |
| 98 |
"GenericMeasurementRegistry", |
| 99 |
"GenericNonMultiplicativeRegistry", |
| 100 |
"GenericNumpyRegistry", |
| 101 |
"GenericPlainRegistry", |
| 102 |
"GenericSystemRegistry", |
| 103 |
"QuantityT", |
| 104 |
"UnitT", |
| 105 |
"MagnitudeT", |
| 106 |
] |