blob: a0456de54a11ca8a4b5b35267de6be9105c52c6e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
"""
pint.facets.nonmultiplicative.objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: 2022 by Pint Authors, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import annotations
class NonMultiplicativeQuantity:
@property
def _is_multiplicative(self) -> bool:
"""Check if the PlainQuantity object has only multiplicative units."""
return not self._get_non_multiplicative_units()
def _get_non_multiplicative_units(self) -> list[str]:
"""Return a list of the of non-multiplicative units of the PlainQuantity object."""
return [
unit
for unit in self._units
if not self._get_unit_definition(unit).is_multiplicative
]
def _get_delta_units(self) -> list[str]:
"""Return list of delta units ot the PlainQuantity object."""
return [u for u in self._units if u.startswith("delta_")]
def _has_compatible_delta(self, unit: str) -> bool:
""" "Check if PlainQuantity object has a delta_unit that is compatible with unit"""
deltas = self._get_delta_units()
if "delta_" + unit in deltas:
return True
# Look for delta units with same dimension as the offset unit
offset_unit_dim = self._get_unit_definition(unit).reference
return any(
self._get_unit_definition(d).reference == offset_unit_dim for d in deltas
)
def _ok_for_muldiv(self, no_offset_units=None) -> bool:
"""Checks if PlainQuantity object can be multiplied or divided"""
is_ok = True
if no_offset_units is None:
no_offset_units = len(self._get_non_multiplicative_units())
if no_offset_units > 1:
is_ok = False
if no_offset_units == 1:
if len(self._units) > 1:
is_ok = False
if (
len(self._units) == 1
and not self._REGISTRY.autoconvert_offset_to_baseunit
):
is_ok = False
if next(iter(self._units.values())) != 1:
is_ok = False
return is_ok
|