summaryrefslogtreecommitdiff
path: root/pint/unit.py
diff options
context:
space:
mode:
Diffstat (limited to 'pint/unit.py')
-rw-r--r--pint/unit.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/pint/unit.py b/pint/unit.py
index 5db27cd..25084b4 100644
--- a/pint/unit.py
+++ b/pint/unit.py
@@ -15,6 +15,7 @@ from numbers import Number
from .compat import NUMERIC_TYPES, is_upcast_type
from .definitions import UnitDefinition
+from .errors import DimensionalityError
from .formatting import siunitx_format_unit
from .util import PrettyIPython, SharedRegistryObject, UnitsContainer
@@ -143,6 +144,40 @@ class Unit(PrettyIPython, SharedRegistryObject):
return self._REGISTRY.get_compatible_units(self)
+ def is_compatible_with(self, other, *contexts, **ctx_kwargs):
+ """ check if the other object is compatible
+
+ Parameters
+ ----------
+ other
+ The object to check. Treated as dimensionless if not a
+ Quantity, Unit or str.
+ *contexts : str or pint.Context
+ Contexts to use in the transformation.
+ **ctx_kwargs :
+ Values for the Context/s
+
+ Returns
+ -------
+ bool
+ """
+ if contexts:
+ try:
+ (1 * self).to(other, *contexts, **ctx_kwargs)
+ return True
+ except DimensionalityError:
+ return False
+
+ if isinstance(other, (self._REGISTRY.Quantity, self._REGISTRY.Unit)):
+ return self.dimensionality == other.dimensionality
+
+ if isinstance(other, str):
+ return (
+ self.dimensionality == self._REGISTRY.parse_units(other).dimensionality
+ )
+
+ return self.dimensionless
+
def __mul__(self, other):
if self._check(other):
if isinstance(other, self.__class__):