unitcorn 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of unitcorn might be problematic. Click here for more details.
- unitcorn/__init__.py +110 -0
- unitcorn/airQuality/__init__.py +99 -0
- unitcorn/area/__init__.py +3 -0
- unitcorn/area/area.py +103 -0
- unitcorn/base/Decorators.py +72 -0
- unitcorn/base/Registry.py +75 -0
- unitcorn/base/SmartBases.py +26 -0
- unitcorn/base/_Algebra.py +205 -0
- unitcorn/base/_Measurement.py +1122 -0
- unitcorn/base/_ScalingMeasurement.py +398 -0
- unitcorn/base/_SmartFloat.py +1635 -0
- unitcorn/base/__init__.py +6 -0
- unitcorn/config/__init__.py +318 -0
- unitcorn/config/__main__.py +8 -0
- unitcorn/config/si.toml +53 -0
- unitcorn/config/template.toml +146 -0
- unitcorn/config/us.toml +53 -0
- unitcorn/defaults/WeatherFlow/__init__.py +176 -0
- unitcorn/defaults/__init__.py +1 -0
- unitcorn/derived/__init__.py +16 -0
- unitcorn/derived/density.py +26 -0
- unitcorn/derived/partsPer.py +96 -0
- unitcorn/derived/precipitation.py +96 -0
- unitcorn/derived/rate.py +80 -0
- unitcorn/derived/volume.py +122 -0
- unitcorn/derived/wind.py +54 -0
- unitcorn/digital/__init__.py +21 -0
- unitcorn/energy/__init__.py +4 -0
- unitcorn/energy/energy.py +120 -0
- unitcorn/energy/power.py +83 -0
- unitcorn/errors/Conversion.py +12 -0
- unitcorn/errors/Unit.py +4 -0
- unitcorn/errors/__init__.py +7 -0
- unitcorn/length/__init__.py +19 -0
- unitcorn/length/imperial.py +46 -0
- unitcorn/length/length.py +74 -0
- unitcorn/length/metric.py +56 -0
- unitcorn/mass/__init__.py +15 -0
- unitcorn/mass/imperial.py +47 -0
- unitcorn/mass/mass.py +57 -0
- unitcorn/mass/metric.py +47 -0
- unitcorn/others/__init__.py +338 -0
- unitcorn/others/light.py +51 -0
- unitcorn/pressure/__init__.py +3 -0
- unitcorn/pressure/pressure.py +189 -0
- unitcorn/py.typed +0 -0
- unitcorn/temperature/__init__.py +19 -0
- unitcorn/temperature/celsius.py +23 -0
- unitcorn/temperature/change.py +156 -0
- unitcorn/temperature/fahrenheit.py +24 -0
- unitcorn/temperature/kelvin.py +25 -0
- unitcorn/temperature/temperature.py +154 -0
- unitcorn/time_/__init__.py +1 -0
- unitcorn/time_/time.py +387 -0
- unitcorn/utils.py +399 -0
- unitcorn-0.9.0.dist-info/METADATA +536 -0
- unitcorn-0.9.0.dist-info/RECORD +59 -0
- unitcorn-0.9.0.dist-info/WHEEL +4 -0
- unitcorn-0.9.0.dist-info/licenses/LICENSE.txt +17 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""A change in temperature, as distinct from a temperature reading.
|
|
2
|
+
|
|
3
|
+
``Celsius(30) - Celsius(20)`` is a change of 10 °C, which is also a change of
|
|
4
|
+
18 °F, so a change converts by a factor and never by an offset. Adding a change
|
|
5
|
+
to a reading gives a reading: ``Fahrenheit(68) + CelsiusChange(10)`` is 86 °F.
|
|
6
|
+
"""
|
|
7
|
+
from contextlib import contextmanager
|
|
8
|
+
from contextvars import ContextVar
|
|
9
|
+
from typing import Type
|
|
10
|
+
|
|
11
|
+
from ..base import Measurement
|
|
12
|
+
from ..utils import Self
|
|
13
|
+
|
|
14
|
+
__all__ = ['TemperatureChange', 'CelsiusChange', 'FahrenheitChange', 'KelvinChange', 'summingReadings']
|
|
15
|
+
|
|
16
|
+
_summing: ContextVar[bool] = ContextVar('unitcorn.summingReadings', default=False)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@contextmanager
|
|
20
|
+
def summingReadings():
|
|
21
|
+
"""Let ``reading + reading`` add, in the left operand's unit.
|
|
22
|
+
|
|
23
|
+
Off by default: 20 °C + 10 °C is 30 °C, but 68 °F + 50 °F is 118 °F, which is not 86 °F.
|
|
24
|
+
"""
|
|
25
|
+
token = _summing.set(True)
|
|
26
|
+
try:
|
|
27
|
+
yield
|
|
28
|
+
finally:
|
|
29
|
+
_summing.reset(token)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def readingsMaySum() -> bool:
|
|
33
|
+
return _summing.get()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class TemperatureChange(Measurement):
|
|
37
|
+
"""A difference between two temperatures, in one of the three scales' degrees."""
|
|
38
|
+
#: Kelvin per degree of this unit.
|
|
39
|
+
_factor: float = 1.0
|
|
40
|
+
_unit_symbol = '°'
|
|
41
|
+
_show_unit = False
|
|
42
|
+
_id = 'Δ°t'
|
|
43
|
+
_reading: Type[Measurement]
|
|
44
|
+
|
|
45
|
+
def __new__(cls, value=0.0, *args, **kwargs):
|
|
46
|
+
if isinstance(value, TemperatureChange) and type(value) is not cls:
|
|
47
|
+
value = float(value) * value._factor / cls._factor
|
|
48
|
+
return Measurement.__new__(cls, value, *args, **kwargs)
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def kelvin(self) -> 'KelvinChange':
|
|
52
|
+
return KelvinChange(self)
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def celsius(self) -> 'CelsiusChange':
|
|
56
|
+
return CelsiusChange(self)
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def fahrenheit(self) -> 'FahrenheitChange':
|
|
60
|
+
return FahrenheitChange(self)
|
|
61
|
+
|
|
62
|
+
k = kelvin
|
|
63
|
+
c = celsius
|
|
64
|
+
f = fahrenheit
|
|
65
|
+
|
|
66
|
+
def __getitem__(self, item):
|
|
67
|
+
key = str(item).lstrip('Δ°').lower()
|
|
68
|
+
for cls in (CelsiusChange, FahrenheitChange, KelvinChange):
|
|
69
|
+
if key in (cls._unit.lstrip('Δ°').lower(), cls.__name__.lower().replace('change', '')):
|
|
70
|
+
return cls(self)
|
|
71
|
+
return super().__getitem__(item)
|
|
72
|
+
|
|
73
|
+
def _kelvins(self) -> float:
|
|
74
|
+
return float(self) * self._factor
|
|
75
|
+
|
|
76
|
+
def _operand(self, other) -> float:
|
|
77
|
+
"""`other` as a number of this unit's degrees. A bare number is already in them."""
|
|
78
|
+
if isinstance(other, TemperatureChange):
|
|
79
|
+
return other._kelvins() / self._factor
|
|
80
|
+
if isinstance(other, Measurement):
|
|
81
|
+
raise TypeError(f'a change in temperature does not combine with {type(other).__name__}')
|
|
82
|
+
return float(other)
|
|
83
|
+
|
|
84
|
+
def __add__(self, other):
|
|
85
|
+
from .temperature import Temperature
|
|
86
|
+
if isinstance(other, Temperature):
|
|
87
|
+
return other + self
|
|
88
|
+
return type(self)(float(self) + self._operand(other))
|
|
89
|
+
|
|
90
|
+
__radd__ = __add__
|
|
91
|
+
|
|
92
|
+
def __sub__(self, other):
|
|
93
|
+
return type(self)(float(self) - self._operand(other))
|
|
94
|
+
|
|
95
|
+
def __rsub__(self, other):
|
|
96
|
+
return type(self)(self._operand(other) - float(self))
|
|
97
|
+
|
|
98
|
+
def __mul__(self, other):
|
|
99
|
+
if isinstance(other, Measurement):
|
|
100
|
+
return super().__mul__(other)
|
|
101
|
+
return type(self)(float(self) * float(other))
|
|
102
|
+
|
|
103
|
+
def __rmul__(self, other):
|
|
104
|
+
return self.__mul__(other)
|
|
105
|
+
|
|
106
|
+
def __truediv__(self, other):
|
|
107
|
+
if isinstance(other, TemperatureChange):
|
|
108
|
+
return self._kelvins() / other._kelvins()
|
|
109
|
+
if isinstance(other, Measurement):
|
|
110
|
+
return super().__truediv__(other)
|
|
111
|
+
return type(self)(float(self) / float(other))
|
|
112
|
+
|
|
113
|
+
def __neg__(self):
|
|
114
|
+
return type(self)(-float(self))
|
|
115
|
+
|
|
116
|
+
def __pos__(self):
|
|
117
|
+
return type(self)(float(self))
|
|
118
|
+
|
|
119
|
+
def __abs__(self):
|
|
120
|
+
return type(self)(abs(float(self)))
|
|
121
|
+
|
|
122
|
+
def __eq__(self, other):
|
|
123
|
+
try:
|
|
124
|
+
return abs(float(self) - self._operand(other)) < 1e-9
|
|
125
|
+
except (TypeError, ValueError):
|
|
126
|
+
return False
|
|
127
|
+
|
|
128
|
+
def __lt__(self, other):
|
|
129
|
+
return float(self) < self._operand(other)
|
|
130
|
+
|
|
131
|
+
def __le__(self, other):
|
|
132
|
+
return float(self) <= self._operand(other)
|
|
133
|
+
|
|
134
|
+
def __gt__(self, other):
|
|
135
|
+
return float(self) > self._operand(other)
|
|
136
|
+
|
|
137
|
+
def __ge__(self, other):
|
|
138
|
+
return float(self) >= self._operand(other)
|
|
139
|
+
|
|
140
|
+
def __hash__(self):
|
|
141
|
+
return hash(round(self._kelvins(), 9))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class CelsiusChange(TemperatureChange):
|
|
145
|
+
_unit = 'Δ°c'
|
|
146
|
+
_factor = 1.0
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class FahrenheitChange(TemperatureChange):
|
|
150
|
+
_unit = 'Δ°f'
|
|
151
|
+
_factor = 5 / 9
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class KelvinChange(TemperatureChange):
|
|
155
|
+
_unit = 'Δk'
|
|
156
|
+
_factor = 1.0
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from math import inf
|
|
2
|
+
|
|
3
|
+
from ..base import imperial
|
|
4
|
+
from .temperature import Temperature
|
|
5
|
+
|
|
6
|
+
__all__ = ['Fahrenheit']
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Fahrenheit(Temperature, system=imperial):
|
|
10
|
+
_limits = (-459.6699, inf)
|
|
11
|
+
_unit = 'f'
|
|
12
|
+
|
|
13
|
+
def _celsius(self, delta: bool = False):
|
|
14
|
+
if delta:
|
|
15
|
+
return float(self)/1.8
|
|
16
|
+
else:
|
|
17
|
+
return (float(self) - 32)/1.8
|
|
18
|
+
|
|
19
|
+
def _fahrenheit(self, delta: bool = False):
|
|
20
|
+
return self
|
|
21
|
+
|
|
22
|
+
def _kelvin(self):
|
|
23
|
+
# (32°F − 32) × 1.8 + 273.15
|
|
24
|
+
return (float(self) - 32)/1.8 + 273.15
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from math import inf
|
|
2
|
+
|
|
3
|
+
from ..base import metric
|
|
4
|
+
from .temperature import Temperature
|
|
5
|
+
|
|
6
|
+
__all__ = ['Kelvin']
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Kelvin(Temperature, system=metric):
|
|
10
|
+
_show_unit = True
|
|
11
|
+
_unit_symbol = ''
|
|
12
|
+
_limits = (0, inf)
|
|
13
|
+
_unit = 'K'
|
|
14
|
+
|
|
15
|
+
def _kelvin(self):
|
|
16
|
+
return self
|
|
17
|
+
|
|
18
|
+
def _celsius(self, delta: bool = False):
|
|
19
|
+
return float(self) - 273.15
|
|
20
|
+
|
|
21
|
+
def _fahrenheit(self, delta: bool = False):
|
|
22
|
+
if delta:
|
|
23
|
+
return (float(self) - 273.15)*1.8
|
|
24
|
+
else:
|
|
25
|
+
return ((float(self) - 273.15)*1.8) + 32
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
from math import log
|
|
3
|
+
|
|
4
|
+
from ..others import Humidity
|
|
5
|
+
from ..base.Decorators import UnitType
|
|
6
|
+
from ..base import Measurement, Dimension
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@UnitType
|
|
10
|
+
class Temperature(Measurement, metaclass=Dimension, symbol='Θ'):
|
|
11
|
+
Celsius: type
|
|
12
|
+
Fahrenheit: type
|
|
13
|
+
Kelvin: type
|
|
14
|
+
|
|
15
|
+
_show_unit = False
|
|
16
|
+
_unit_symbol = '°'
|
|
17
|
+
_id = '°t'
|
|
18
|
+
|
|
19
|
+
def __new__(cls, value: float | int | Measurement):
|
|
20
|
+
if isinstance(value, Temperature) and not isinstance(value, cls):
|
|
21
|
+
value = value[cls.unit]
|
|
22
|
+
return super().__new__(cls, value)
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def celsius(self):
|
|
26
|
+
from ..temperature import Celsius
|
|
27
|
+
return Celsius(self._celsius())
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def fahrenheit(self):
|
|
31
|
+
from ..temperature import Fahrenheit
|
|
32
|
+
return Fahrenheit(self._fahrenheit())
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def kelvin(self):
|
|
36
|
+
from ..temperature import Kelvin
|
|
37
|
+
return Kelvin(self._kelvin())
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def fDelta(self):
|
|
41
|
+
from ..temperature import Fahrenheit
|
|
42
|
+
return Fahrenheit(self._fahrenheit(delta=True))
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def cDelta(self):
|
|
46
|
+
from ..temperature import Celsius
|
|
47
|
+
return Celsius(self._celsius(delta=True))
|
|
48
|
+
|
|
49
|
+
def dewpoint(self, rh: float):
|
|
50
|
+
from ..temperature import Celsius
|
|
51
|
+
|
|
52
|
+
# Keep rh within reasonable ranges
|
|
53
|
+
rh = self.normalizeRh(rh)
|
|
54
|
+
|
|
55
|
+
T = float(self.c)
|
|
56
|
+
|
|
57
|
+
logRH = log(rh/100)
|
|
58
|
+
A = 243.04
|
|
59
|
+
B = 17.625
|
|
60
|
+
value = A*(logRH + ((B*T)/(A + T)))/(B - logRH - ((B*T)/(A + T)))
|
|
61
|
+
|
|
62
|
+
value = type(self)(Celsius(min(value, T)))
|
|
63
|
+
value = self.transform(value)
|
|
64
|
+
value.title = 'Dewpoint'
|
|
65
|
+
value.calculated = True
|
|
66
|
+
return self.transform(value)
|
|
67
|
+
|
|
68
|
+
def heatIndex(self, rh: float):
|
|
69
|
+
R = float(self.normalizeRh(rh))
|
|
70
|
+
|
|
71
|
+
# below 300 K (about 27 °C, 80 °F) the regression does not apply
|
|
72
|
+
if self.kelvin < 300 or R < 13:
|
|
73
|
+
return self
|
|
74
|
+
|
|
75
|
+
if self._unit == 'f':
|
|
76
|
+
c = [-42.379, 2.04901523, 10.14333427, -0.22477541, -0.00683783, -0.05481717, 0.00122874, 0.00085282, -0.00000199]
|
|
77
|
+
else:
|
|
78
|
+
c = [-8.78469475556, 1.61139411, 2.33854883889, -0.14611605, -0.012308094, -0.0164248277778, 0.002211732, 0.00072546, -0.000003582]
|
|
79
|
+
|
|
80
|
+
T = float(self if self._unit != 'K' else self.c)
|
|
81
|
+
T2 = pow(T, 2)
|
|
82
|
+
R2 = pow(R, 2)
|
|
83
|
+
|
|
84
|
+
hi = c[0] + (c[1]*T) + (c[2]*R) + (c[3]*T*R) + (c[4]*T2) + (c[5]*R2) + (c[6]*T2*R) + (c[7]*T*R2) + (c[8]*T2*R2)
|
|
85
|
+
|
|
86
|
+
hi = hi if self._unit != 'K' else hi + 273.15
|
|
87
|
+
value = self.transform(self.__class__(hi))
|
|
88
|
+
value.title = 'Heat Index'
|
|
89
|
+
value.calculated = True
|
|
90
|
+
return value
|
|
91
|
+
|
|
92
|
+
def windChill(self, wind: 'Wind'):
|
|
93
|
+
if wind.mph < 3:
|
|
94
|
+
return type(self)(self)
|
|
95
|
+
if self._unit == 'f':
|
|
96
|
+
w = float(wind.mih)
|
|
97
|
+
t = float(self)
|
|
98
|
+
value = 35.74 + (0.6215*t) - (35.75*pow(w, 0.16)) + (0.4275*t*pow(w, 0.16))
|
|
99
|
+
else:
|
|
100
|
+
w = float(wind.kmh)
|
|
101
|
+
t = float(self.c)
|
|
102
|
+
value = 13.12 + (0.6215*t) - (11.37*pow(w, 0.16)) + (0.3965*t*pow(w, 0.16))
|
|
103
|
+
|
|
104
|
+
value = self.transform(self.__class__(round(value, self._precision)))
|
|
105
|
+
value.title = 'Wind Chill'
|
|
106
|
+
value.calculated = True
|
|
107
|
+
return value
|
|
108
|
+
|
|
109
|
+
@staticmethod
|
|
110
|
+
def normalizeRh(rh: Union[int, float, 'Humidity']):
|
|
111
|
+
if not isinstance(rh, Humidity):
|
|
112
|
+
rh = Humidity(rh)
|
|
113
|
+
return int(rh)
|
|
114
|
+
|
|
115
|
+
def _convert(self, value: Measurement):
|
|
116
|
+
if isinstance(value, Measurement):
|
|
117
|
+
return value[self.unit]
|
|
118
|
+
return value[self.unit]
|
|
119
|
+
|
|
120
|
+
def __sub__(self, other):
|
|
121
|
+
from .change import TemperatureChange
|
|
122
|
+
if isinstance(other, TemperatureChange):
|
|
123
|
+
# reading - change = reading, whatever scale the change is in
|
|
124
|
+
return type(self)(float(self) - float(type(self).change(other)))
|
|
125
|
+
if isinstance(other, Temperature):
|
|
126
|
+
# reading - reading = change, in this reading's degrees
|
|
127
|
+
return type(self).change(float(self) - float(type(self)(other)))
|
|
128
|
+
return super().__sub__(other)
|
|
129
|
+
|
|
130
|
+
def __add__(self, other):
|
|
131
|
+
from .change import TemperatureChange, readingsMaySum
|
|
132
|
+
if isinstance(other, TemperatureChange):
|
|
133
|
+
return type(self)(float(self) + float(type(self).change(other)))
|
|
134
|
+
if isinstance(other, Temperature) and not readingsMaySum():
|
|
135
|
+
raise TypeError(
|
|
136
|
+
f'cannot add two temperature readings ({self.withUnit} + {other.withUnit}): the sum depends on the unit. '
|
|
137
|
+
f'Add a TemperatureChange, or use `with summingReadings():`'
|
|
138
|
+
)
|
|
139
|
+
if isinstance(other, Temperature):
|
|
140
|
+
other = type(self)(other)
|
|
141
|
+
return super().__add__(other)
|
|
142
|
+
|
|
143
|
+
def __radd__(self, other):
|
|
144
|
+
from .change import TemperatureChange
|
|
145
|
+
if isinstance(other, TemperatureChange):
|
|
146
|
+
return self.__add__(other)
|
|
147
|
+
return super().__radd__(other)
|
|
148
|
+
|
|
149
|
+
# abbreviations
|
|
150
|
+
c = celsius
|
|
151
|
+
k = kelvin
|
|
152
|
+
K = kelvin # the symbol, which unit lookup by attribute uses
|
|
153
|
+
kel = kelvin
|
|
154
|
+
f = fahrenheit
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .time import *
|