kuchka 0.1.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.
- kuchka/__init__.py +259 -0
- kuchka/constants.py +72 -0
- kuchka/dimensions.py +96 -0
- kuchka/errors.py +90 -0
- kuchka/numeric.py +72 -0
- kuchka/py.typed +0 -0
- kuchka/quantities/__init__.py +61 -0
- kuchka/quantities/amount.py +22 -0
- kuchka/quantities/base.py +380 -0
- kuchka/quantities/charge.py +44 -0
- kuchka/quantities/electrical.py +137 -0
- kuchka/quantities/energy.py +43 -0
- kuchka/quantities/kinematics.py +52 -0
- kuchka/quantities/length.py +38 -0
- kuchka/quantities/luminous_intensity.py +22 -0
- kuchka/quantities/mass.py +22 -0
- kuchka/quantities/proportion.py +28 -0
- kuchka/quantities/temporal.py +292 -0
- kuchka/quantities/thermal.py +275 -0
- kuchka/units/__init__.py +313 -0
- kuchka/units/amount.py +60 -0
- kuchka/units/charge.py +58 -0
- kuchka/units/current.py +65 -0
- kuchka/units/energy.py +86 -0
- kuchka/units/kinematics.py +52 -0
- kuchka/units/length.py +111 -0
- kuchka/units/luminous_intensity.py +47 -0
- kuchka/units/mass.py +103 -0
- kuchka/units/power.py +58 -0
- kuchka/units/proportion.py +51 -0
- kuchka/units/resistance.py +65 -0
- kuchka/units/temporal.py +232 -0
- kuchka/units/voltage.py +71 -0
- kuchka-0.1.0.dist-info/METADATA +385 -0
- kuchka-0.1.0.dist-info/RECORD +36 -0
- kuchka-0.1.0.dist-info/WHEEL +4 -0
kuchka/__init__.py
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"""kuchka -- a small pile of physically-typed units for engineering code.
|
|
2
|
+
|
|
3
|
+
from kuchka import Volt, Milliampere
|
|
4
|
+
|
|
5
|
+
voltage = Volt(4.2)
|
|
6
|
+
current = Milliampere(150)
|
|
7
|
+
power = voltage * current # -> Power, computed and typed automatically
|
|
8
|
+
|
|
9
|
+
Everything below is the entire intended public surface.
|
|
10
|
+
Internal modules (``quantities``, ``dimensions``, ``numeric``, ...) are implementation
|
|
11
|
+
details and are not part of the supported API.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from importlib.metadata import PackageNotFoundError
|
|
15
|
+
from importlib.metadata import version
|
|
16
|
+
|
|
17
|
+
from kuchka.constants import ELECTRON_MASS
|
|
18
|
+
from kuchka.constants import ELEMENTARY_CHARGE
|
|
19
|
+
from kuchka.constants import NEUTRON_MASS
|
|
20
|
+
from kuchka.constants import PROTON_MASS
|
|
21
|
+
from kuchka.constants import SPEED_OF_LIGHT
|
|
22
|
+
from kuchka.errors import IncompatibleDimensionError
|
|
23
|
+
from kuchka.errors import InvalidConversionError
|
|
24
|
+
from kuchka.errors import InvalidMeasurementError
|
|
25
|
+
from kuchka.errors import MeasurementError
|
|
26
|
+
from kuchka.errors import PlannedDimensionError
|
|
27
|
+
from kuchka.errors import UnrepresentableDimensionError
|
|
28
|
+
from kuchka.quantities import AmountOfSubstance
|
|
29
|
+
from kuchka.quantities import BaudRate
|
|
30
|
+
from kuchka.quantities import Charge
|
|
31
|
+
from kuchka.quantities import ConvertibleToFrequency
|
|
32
|
+
from kuchka.quantities import Current
|
|
33
|
+
from kuchka.quantities import Energy
|
|
34
|
+
from kuchka.quantities import Frequency
|
|
35
|
+
from kuchka.quantities import Length
|
|
36
|
+
from kuchka.quantities import LuminousIntensity
|
|
37
|
+
from kuchka.quantities import Mass
|
|
38
|
+
from kuchka.quantities import Power
|
|
39
|
+
from kuchka.quantities import Proportion
|
|
40
|
+
from kuchka.quantities import Resistance
|
|
41
|
+
from kuchka.quantities import SampleRate
|
|
42
|
+
from kuchka.quantities import SamplingPeriod
|
|
43
|
+
from kuchka.quantities import Temperature
|
|
44
|
+
from kuchka.quantities import TemperatureDelta
|
|
45
|
+
from kuchka.quantities import Velocity
|
|
46
|
+
from kuchka.quantities import Time
|
|
47
|
+
from kuchka.quantities import Voltage
|
|
48
|
+
from kuchka.quantities.thermal import Celsius
|
|
49
|
+
from kuchka.quantities.thermal import DeltaCelsius
|
|
50
|
+
from kuchka.quantities.thermal import DeltaKelvin
|
|
51
|
+
from kuchka.quantities.thermal import Kelvin
|
|
52
|
+
from kuchka.units import Ampere
|
|
53
|
+
from kuchka.units import COULOMB
|
|
54
|
+
from kuchka.units import Coulomb
|
|
55
|
+
from kuchka.units import ENERGY_UNITS
|
|
56
|
+
from kuchka.units import JOULE
|
|
57
|
+
from kuchka.units import Joule
|
|
58
|
+
from kuchka.units import KILOJOULE
|
|
59
|
+
from kuchka.units import KILOMETRE
|
|
60
|
+
from kuchka.units import Kilojoule
|
|
61
|
+
from kuchka.units import Kilometre
|
|
62
|
+
from kuchka.units import MEGAJOULE
|
|
63
|
+
from kuchka.units import METRE
|
|
64
|
+
from kuchka.units import Megajoule
|
|
65
|
+
from kuchka.units import Metre
|
|
66
|
+
from kuchka.units import METRE_PER_SECOND
|
|
67
|
+
from kuchka.units import MeterPerSecond
|
|
68
|
+
from kuchka.units import MetrePerSecond
|
|
69
|
+
from kuchka.units import MICROCOULOMB
|
|
70
|
+
from kuchka.units import MICROJOULE
|
|
71
|
+
from kuchka.units import MICROMETRE
|
|
72
|
+
from kuchka.units import Microcoulomb
|
|
73
|
+
from kuchka.units import Microjoule
|
|
74
|
+
from kuchka.units import Micrometre
|
|
75
|
+
from kuchka.units import MILLICOULOMB
|
|
76
|
+
from kuchka.units import MILLIJOULE
|
|
77
|
+
from kuchka.units import MILLIMETRE
|
|
78
|
+
from kuchka.units import Millicoulomb
|
|
79
|
+
from kuchka.units import Millijoule
|
|
80
|
+
from kuchka.units import Millimetre
|
|
81
|
+
from kuchka.units import NANOJOULE
|
|
82
|
+
from kuchka.units import NANOMETRE
|
|
83
|
+
from kuchka.units import Nanojoule
|
|
84
|
+
from kuchka.units import Nanometre
|
|
85
|
+
from kuchka.units import PICOJOULE
|
|
86
|
+
from kuchka.units import Picojoule
|
|
87
|
+
from kuchka.units import Baud
|
|
88
|
+
from kuchka.units import Candela
|
|
89
|
+
from kuchka.units import Gram
|
|
90
|
+
from kuchka.units import Hertz
|
|
91
|
+
from kuchka.units import Inch
|
|
92
|
+
from kuchka.units import Kiloampere
|
|
93
|
+
from kuchka.units import Kilobaud
|
|
94
|
+
from kuchka.units import Kilogram
|
|
95
|
+
from kuchka.units import Kilohertz
|
|
96
|
+
from kuchka.units import Kilometer
|
|
97
|
+
from kuchka.units import Kiloohm
|
|
98
|
+
from kuchka.units import KilosamplesPerSecond
|
|
99
|
+
from kuchka.units import Kilovolt
|
|
100
|
+
from kuchka.units import Kilowatt
|
|
101
|
+
from kuchka.units import Megagram
|
|
102
|
+
from kuchka.units import Megahertz
|
|
103
|
+
from kuchka.units import Megaohm
|
|
104
|
+
from kuchka.units import MegasamplesPerSecond
|
|
105
|
+
from kuchka.units import Meter
|
|
106
|
+
from kuchka.units import Microampere
|
|
107
|
+
from kuchka.units import Microgram
|
|
108
|
+
from kuchka.units import Micrometer
|
|
109
|
+
from kuchka.units import Micromole
|
|
110
|
+
from kuchka.units import Microsecond
|
|
111
|
+
from kuchka.units import MicrosecondSamplingPeriod
|
|
112
|
+
from kuchka.units import Microvolt
|
|
113
|
+
from kuchka.units import Milliampere
|
|
114
|
+
from kuchka.units import Milligram
|
|
115
|
+
from kuchka.units import Millimeter
|
|
116
|
+
from kuchka.units import Millimole
|
|
117
|
+
from kuchka.units import Milliohm
|
|
118
|
+
from kuchka.units import Millisecond
|
|
119
|
+
from kuchka.units import MillisecondSamplingPeriod
|
|
120
|
+
from kuchka.units import Millivolt
|
|
121
|
+
from kuchka.units import Milliwatt
|
|
122
|
+
from kuchka.units import Mole
|
|
123
|
+
from kuchka.units import Nanogram
|
|
124
|
+
from kuchka.units import Nanometer
|
|
125
|
+
from kuchka.units import Ohm
|
|
126
|
+
from kuchka.units import Percent
|
|
127
|
+
from kuchka.units import Ratio
|
|
128
|
+
from kuchka.units import SamplesPerSecond
|
|
129
|
+
from kuchka.units import Second
|
|
130
|
+
from kuchka.units import SecondSamplingPeriod
|
|
131
|
+
from kuchka.units import Tonne
|
|
132
|
+
from kuchka.units import Volt
|
|
133
|
+
from kuchka.units import Watt
|
|
134
|
+
|
|
135
|
+
try:
|
|
136
|
+
__version__ = version("kuchka")
|
|
137
|
+
except PackageNotFoundError: # pragma: no cover -- running from an uninstalled checkout
|
|
138
|
+
__version__ = "0.0.0+unknown"
|
|
139
|
+
|
|
140
|
+
__all__: list[str] = [
|
|
141
|
+
"COULOMB",
|
|
142
|
+
"ELECTRON_MASS",
|
|
143
|
+
"ELEMENTARY_CHARGE",
|
|
144
|
+
"ENERGY_UNITS",
|
|
145
|
+
"JOULE",
|
|
146
|
+
"KILOJOULE",
|
|
147
|
+
"KILOMETRE",
|
|
148
|
+
"MEGAJOULE",
|
|
149
|
+
"METRE",
|
|
150
|
+
"METRE_PER_SECOND",
|
|
151
|
+
"MICROCOULOMB",
|
|
152
|
+
"MICROJOULE",
|
|
153
|
+
"MICROMETRE",
|
|
154
|
+
"MILLICOULOMB",
|
|
155
|
+
"MILLIJOULE",
|
|
156
|
+
"MILLIMETRE",
|
|
157
|
+
"NANOJOULE",
|
|
158
|
+
"NANOMETRE",
|
|
159
|
+
"NEUTRON_MASS",
|
|
160
|
+
"PICOJOULE",
|
|
161
|
+
"PROTON_MASS",
|
|
162
|
+
"SPEED_OF_LIGHT",
|
|
163
|
+
"AmountOfSubstance",
|
|
164
|
+
"Ampere",
|
|
165
|
+
"Baud",
|
|
166
|
+
"BaudRate",
|
|
167
|
+
"Candela",
|
|
168
|
+
"Celsius",
|
|
169
|
+
"Charge",
|
|
170
|
+
"ConvertibleToFrequency",
|
|
171
|
+
"Coulomb",
|
|
172
|
+
"Current",
|
|
173
|
+
"DeltaCelsius",
|
|
174
|
+
"DeltaKelvin",
|
|
175
|
+
"Energy",
|
|
176
|
+
"Frequency",
|
|
177
|
+
"Gram",
|
|
178
|
+
"Hertz",
|
|
179
|
+
"Inch",
|
|
180
|
+
"IncompatibleDimensionError",
|
|
181
|
+
"InvalidConversionError",
|
|
182
|
+
"InvalidMeasurementError",
|
|
183
|
+
"Joule",
|
|
184
|
+
"Kelvin",
|
|
185
|
+
"Kiloampere",
|
|
186
|
+
"Kilobaud",
|
|
187
|
+
"Kilogram",
|
|
188
|
+
"Kilohertz",
|
|
189
|
+
"Kilojoule",
|
|
190
|
+
"Kilometer",
|
|
191
|
+
"Kilometre",
|
|
192
|
+
"Kiloohm",
|
|
193
|
+
"KilosamplesPerSecond",
|
|
194
|
+
"Kilovolt",
|
|
195
|
+
"Kilowatt",
|
|
196
|
+
"Length",
|
|
197
|
+
"LuminousIntensity",
|
|
198
|
+
"Mass",
|
|
199
|
+
"MeasurementError",
|
|
200
|
+
"Megagram",
|
|
201
|
+
"Megahertz",
|
|
202
|
+
"Megajoule",
|
|
203
|
+
"Megaohm",
|
|
204
|
+
"MegasamplesPerSecond",
|
|
205
|
+
"Meter",
|
|
206
|
+
"MeterPerSecond",
|
|
207
|
+
"Metre",
|
|
208
|
+
"MetrePerSecond",
|
|
209
|
+
"Microampere",
|
|
210
|
+
"Microcoulomb",
|
|
211
|
+
"Microgram",
|
|
212
|
+
"Microjoule",
|
|
213
|
+
"Micrometer",
|
|
214
|
+
"Micrometre",
|
|
215
|
+
"Micromole",
|
|
216
|
+
"Microsecond",
|
|
217
|
+
"MicrosecondSamplingPeriod",
|
|
218
|
+
"Microvolt",
|
|
219
|
+
"Milliampere",
|
|
220
|
+
"Millicoulomb",
|
|
221
|
+
"Milligram",
|
|
222
|
+
"Millijoule",
|
|
223
|
+
"Millimeter",
|
|
224
|
+
"Millimetre",
|
|
225
|
+
"Millimole",
|
|
226
|
+
"Milliohm",
|
|
227
|
+
"Millisecond",
|
|
228
|
+
"MillisecondSamplingPeriod",
|
|
229
|
+
"Millivolt",
|
|
230
|
+
"Milliwatt",
|
|
231
|
+
"Mole",
|
|
232
|
+
"Nanogram",
|
|
233
|
+
"Nanojoule",
|
|
234
|
+
"Nanometer",
|
|
235
|
+
"Nanometre",
|
|
236
|
+
"Ohm",
|
|
237
|
+
"Percent",
|
|
238
|
+
"Picojoule",
|
|
239
|
+
"PlannedDimensionError",
|
|
240
|
+
"Power",
|
|
241
|
+
"Proportion",
|
|
242
|
+
"Ratio",
|
|
243
|
+
"Resistance",
|
|
244
|
+
"SampleRate",
|
|
245
|
+
"SamplesPerSecond",
|
|
246
|
+
"SamplingPeriod",
|
|
247
|
+
"Second",
|
|
248
|
+
"SecondSamplingPeriod",
|
|
249
|
+
"Temperature",
|
|
250
|
+
"TemperatureDelta",
|
|
251
|
+
"Time",
|
|
252
|
+
"Tonne",
|
|
253
|
+
"UnrepresentableDimensionError",
|
|
254
|
+
"Velocity",
|
|
255
|
+
"Volt",
|
|
256
|
+
"Voltage",
|
|
257
|
+
"Watt",
|
|
258
|
+
"__version__",
|
|
259
|
+
]
|
kuchka/constants.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""A small set of physical constants, expressed as ordinary kuchka quantities.
|
|
2
|
+
|
|
3
|
+
from kuchka import Hertz
|
|
4
|
+
from kuchka.constants import SPEED_OF_LIGHT
|
|
5
|
+
|
|
6
|
+
wifi_wavelength = SPEED_OF_LIGHT / Hertz(2_400_000_000)
|
|
7
|
+
|
|
8
|
+
Two different kinds of "exact" live in this module, and each constant's docstring says
|
|
9
|
+
which one applies:
|
|
10
|
+
|
|
11
|
+
- SI-defining constants (`SPEED_OF_LIGHT`, `ELEMENTARY_CHARGE`) have been exact by
|
|
12
|
+
definition since the 2019 SI redefinition.
|
|
13
|
+
The `Decimal` below is the entire value, not a rounded measurement, and every digit
|
|
14
|
+
shown is significant.
|
|
15
|
+
- Measured constants (`ELECTRON_MASS`, `PROTON_MASS`, `NEUTRON_MASS`) are CODATA 2022
|
|
16
|
+
recommended values: the best current measurement, not a mathematical constant.
|
|
17
|
+
The `Decimal` stored here carries exactly as many significant digits as CODATA
|
|
18
|
+
publishes for the central value.
|
|
19
|
+
Adding more digits would not add precision -- it would misrepresent how well the
|
|
20
|
+
quantity is actually known.
|
|
21
|
+
Each docstring quotes CODATA's own parenthetical uncertainty in the last digits
|
|
22
|
+
for that reason, even though the stored magnitude itself only carries the central
|
|
23
|
+
value.
|
|
24
|
+
|
|
25
|
+
Source: CODATA 2022 recommended values,
|
|
26
|
+
https://physics.nist.gov/cuu/Constants/
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
from decimal import Decimal
|
|
32
|
+
from typing import TYPE_CHECKING
|
|
33
|
+
|
|
34
|
+
from kuchka.units import Coulomb
|
|
35
|
+
from kuchka.units import Kilogram
|
|
36
|
+
from kuchka.units import MetrePerSecond
|
|
37
|
+
|
|
38
|
+
if TYPE_CHECKING:
|
|
39
|
+
from kuchka.quantities import Charge
|
|
40
|
+
from kuchka.quantities import Mass
|
|
41
|
+
from kuchka.quantities import Velocity
|
|
42
|
+
|
|
43
|
+
SPEED_OF_LIGHT: Velocity = MetrePerSecond(299_792_458)
|
|
44
|
+
"""The speed of light in vacuum, c.
|
|
45
|
+
|
|
46
|
+
Exact by definition: the metre has been defined in terms of c and the second since 1983.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
ELEMENTARY_CHARGE: Charge = Coulomb(Decimal("1.602176634E-19"))
|
|
50
|
+
"""The elementary charge, e.
|
|
51
|
+
|
|
52
|
+
Exact by definition since the 2019 SI redefinition: the coulomb is now defined in terms
|
|
53
|
+
of e.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
ELECTRON_MASS: Mass = Kilogram(Decimal("9.1093837139E-31"))
|
|
57
|
+
"""The electron rest mass, m_e.
|
|
58
|
+
|
|
59
|
+
CODATA 2022 recommended value: 9.109 383 7139(28) x 10^-31 kg. Measured, not exact.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
PROTON_MASS: Mass = Kilogram(Decimal("1.67262192595E-27"))
|
|
63
|
+
"""The proton rest mass, m_p.
|
|
64
|
+
|
|
65
|
+
CODATA 2022 recommended value: 1.672 621 925 95(52) x 10^-27 kg. Measured, not exact.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
NEUTRON_MASS: Mass = Kilogram(Decimal("1.67492750056E-27"))
|
|
69
|
+
"""The neutron rest mass, m_n.
|
|
70
|
+
|
|
71
|
+
CODATA 2022 recommended value: 1.674 927 500 56(85) x 10^-27 kg. Measured, not exact.
|
|
72
|
+
"""
|
kuchka/dimensions.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Dimensional analysis primitives.
|
|
2
|
+
|
|
3
|
+
This module is internal machinery.
|
|
4
|
+
Ordinary users of the library never construct a :class:`Dimension` directly,
|
|
5
|
+
as they use the named engineering types (``Voltage``, ``Current``, ...),
|
|
6
|
+
and unit constructors (``Volt``, ``Milliampere``, ...) exposed from the top-level.
|
|
7
|
+
|
|
8
|
+
Dimensions are represented as exponent vectors over all seven SI base quantities:
|
|
9
|
+
mass, length, time, electric current, thermodynamic temperature, amount of substance,
|
|
10
|
+
and luminous intensity.
|
|
11
|
+
|
|
12
|
+
Multiplying two physical quantities adds their exponent vectors.
|
|
13
|
+
Dividing subtracts them.
|
|
14
|
+
This gives the arithmetic engine a single general mechanism for deriving compound
|
|
15
|
+
dimensions (Power from Voltage * Current, Resistance from Voltage / Current, and so on),
|
|
16
|
+
instead of a hand-written table of every possible pairwise combination.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from dataclasses import dataclass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True, slots=True)
|
|
25
|
+
class Dimension:
|
|
26
|
+
"""An exponent vector over the SI base dimensions used by this library.
|
|
27
|
+
|
|
28
|
+
Two ``Dimension`` instances compare equal (and hash equal) exactly when every
|
|
29
|
+
exponent matches -- this is what lets the arithmetic engine use a plain `dict` to
|
|
30
|
+
look up "which named type does M^1 L^2 T^-3 correspond to?".
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
mass: int = 0
|
|
34
|
+
length: int = 0
|
|
35
|
+
time: int = 0
|
|
36
|
+
current: int = 0
|
|
37
|
+
temperature: int = 0
|
|
38
|
+
amount: int = 0
|
|
39
|
+
luminous_intensity: int = 0
|
|
40
|
+
|
|
41
|
+
def __mul__(self, other: Dimension) -> Dimension:
|
|
42
|
+
return Dimension(
|
|
43
|
+
mass=self.mass + other.mass,
|
|
44
|
+
length=self.length + other.length,
|
|
45
|
+
time=self.time + other.time,
|
|
46
|
+
current=self.current + other.current,
|
|
47
|
+
temperature=self.temperature + other.temperature,
|
|
48
|
+
amount=self.amount + other.amount,
|
|
49
|
+
luminous_intensity=self.luminous_intensity + other.luminous_intensity,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
def __truediv__(self, other: Dimension) -> Dimension:
|
|
53
|
+
return Dimension(
|
|
54
|
+
mass=self.mass - other.mass,
|
|
55
|
+
length=self.length - other.length,
|
|
56
|
+
time=self.time - other.time,
|
|
57
|
+
current=self.current - other.current,
|
|
58
|
+
temperature=self.temperature - other.temperature,
|
|
59
|
+
amount=self.amount - other.amount,
|
|
60
|
+
luminous_intensity=self.luminous_intensity - other.luminous_intensity,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
def __repr__(self) -> str:
|
|
64
|
+
labels = (
|
|
65
|
+
("mass", "M"),
|
|
66
|
+
("length", "L"),
|
|
67
|
+
("time", "T"),
|
|
68
|
+
("current", "I"),
|
|
69
|
+
("temperature", "\u0398"),
|
|
70
|
+
("amount", "N"),
|
|
71
|
+
("luminous_intensity", "J"),
|
|
72
|
+
)
|
|
73
|
+
parts = []
|
|
74
|
+
for field, label in labels:
|
|
75
|
+
exponent = getattr(self, field)
|
|
76
|
+
if exponent == 0:
|
|
77
|
+
continue
|
|
78
|
+
parts.append(label if exponent == 1 else f"{label}^{exponent}")
|
|
79
|
+
return " ".join(parts) if parts else "dimensionless"
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
DIMENSIONLESS = Dimension()
|
|
83
|
+
MASS = Dimension(mass=1)
|
|
84
|
+
CURRENT = Dimension(current=1)
|
|
85
|
+
VOLTAGE = Dimension(mass=1, length=2, time=-3, current=-1)
|
|
86
|
+
POWER = Dimension(mass=1, length=2, time=-3)
|
|
87
|
+
RESISTANCE = Dimension(mass=1, length=2, time=-3, current=-2)
|
|
88
|
+
TEMPERATURE_DELTA = Dimension(temperature=1)
|
|
89
|
+
TIME = Dimension(time=1)
|
|
90
|
+
FREQUENCY = Dimension(time=-1)
|
|
91
|
+
LENGTH = Dimension(length=1)
|
|
92
|
+
AMOUNT_OF_SUBSTANCE = Dimension(amount=1)
|
|
93
|
+
LUMINOUS_INTENSITY = Dimension(luminous_intensity=1)
|
|
94
|
+
VELOCITY = Dimension(length=1, time=-1)
|
|
95
|
+
CHARGE = Dimension(current=1, time=1)
|
|
96
|
+
ENERGY = Dimension(mass=1, length=2, time=-2)
|
kuchka/errors.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""The measurement system's exception hierarchy.
|
|
2
|
+
|
|
3
|
+
Every error a user is likely to hit -- a dimension mismatch, a naked float where a
|
|
4
|
+
measurement was expected, an unrecognised conversion target -- is a subclass of
|
|
5
|
+
:class:`MeasurementError`, so application code can catch the whole family with one
|
|
6
|
+
except clause if it wants to.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MeasurementError(Exception):
|
|
13
|
+
"""Base class for all measurement-system errors."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class InvalidMeasurementError(MeasurementError):
|
|
17
|
+
"""A value could not be turned into, or combined with, a measurement.
|
|
18
|
+
|
|
19
|
+
Raised at construction time, e.g. passing a string, or a measurement of the wrong
|
|
20
|
+
dimension, into a unit constructor, and at arithmetic boundaries where a naked
|
|
21
|
+
number was used where a measurement was expected.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class IncompatibleDimensionError(MeasurementError):
|
|
26
|
+
"""An operation was attempted between two incompatible dimensions."""
|
|
27
|
+
|
|
28
|
+
def __init__(self, operation: str, left: object, right: object) -> None:
|
|
29
|
+
"""Build the error message from the operands' runtime types and dimensions."""
|
|
30
|
+
left_name = type(left).__name__
|
|
31
|
+
right_name = type(right).__name__
|
|
32
|
+
left_dim = getattr(left, "dimension", None)
|
|
33
|
+
right_dim = getattr(right, "dimension", None)
|
|
34
|
+
message = (
|
|
35
|
+
f"Cannot {operation} {left_name} and {right_name}.\n"
|
|
36
|
+
f"{left_name} has dimension [{left_dim!r}].\n"
|
|
37
|
+
f"{right_name} has dimension [{right_dim!r}]."
|
|
38
|
+
)
|
|
39
|
+
super().__init__(message)
|
|
40
|
+
self.operation = operation
|
|
41
|
+
self.left = left
|
|
42
|
+
self.right = right
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class InvalidConversionError(MeasurementError):
|
|
46
|
+
"""``.to(...)`` was given something that is not a valid conversion target."""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class UnrepresentableDimensionError(MeasurementError):
|
|
50
|
+
"""Arithmetic produced a dimension with no registered engineering type.
|
|
51
|
+
|
|
52
|
+
This is a scope boundary: each released version of the library registers a
|
|
53
|
+
specific, deliberately limited vocabulary of engineering quantities
|
|
54
|
+
(see ``kuchka.quantities``).
|
|
55
|
+
|
|
56
|
+
An operation that mathematically produces something outside that vocabulary,
|
|
57
|
+
e.g. Resistance times Resistance, is rejected clearly here rather than silently
|
|
58
|
+
returning an untyped number.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def __init__(self, dimension: object) -> None:
|
|
62
|
+
"""Build the error message from the unregistered dimension."""
|
|
63
|
+
super().__init__(
|
|
64
|
+
f"the result of this operation has dimension [{dimension!r}], "
|
|
65
|
+
"which has no registered engineering type in this version of "
|
|
66
|
+
"the library.",
|
|
67
|
+
)
|
|
68
|
+
self.dimension = dimension
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class PlannedDimensionError(UnrepresentableDimensionError):
|
|
72
|
+
"""Arithmetic produced a dimension that corresponds to a named, planned quantity.
|
|
73
|
+
|
|
74
|
+
Raised for a dimension this library already has a name for, e.g., formerly, Length
|
|
75
|
+
divided by Time producing Velocity, so the message can say what the result actually
|
|
76
|
+
is rather than only printing its exponent vector.
|
|
77
|
+
A subclass of :class:`UnrepresentableDimensionError`, so code that catches the
|
|
78
|
+
general error still catches this one too.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(self, dimension: object, planned_name: str) -> None:
|
|
82
|
+
"""Build the error message from the dimension and its planned quantity name."""
|
|
83
|
+
MeasurementError.__init__(
|
|
84
|
+
self,
|
|
85
|
+
f"the result of this operation has dimension [{dimension!r}], "
|
|
86
|
+
f"which corresponds to {planned_name} -- planned but not yet "
|
|
87
|
+
"implemented in this version of the library.",
|
|
88
|
+
)
|
|
89
|
+
self.dimension = dimension
|
|
90
|
+
self.planned_name = planned_name
|
kuchka/numeric.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Strict numeric coercion into :class:`decimal.Decimal`.
|
|
2
|
+
|
|
3
|
+
SI prefixes (milli, kilo, mega, ...) are all powers of ten, and powers of
|
|
4
|
+
ten are exactly representable in ``Decimal`` but generally are *not*
|
|
5
|
+
exactly representable in binary ``float``.
|
|
6
|
+
|
|
7
|
+
That is the concrete, practical reason this library stores magnitudes as ``Decimal``
|
|
8
|
+
internally rather than ``float``:
|
|
9
|
+
it is what makes ``Volt(1) + Millivolt(500) == Volt(1.5)`` true by construction instead
|
|
10
|
+
of true by coincidence.
|
|
11
|
+
|
|
12
|
+
This module is the single place where "a plain number" is turned into a ``Decimal``
|
|
13
|
+
for use inside a measurement.
|
|
14
|
+
Every unit constructor (``Volt``, ``Ampere``, ``Celsius``, ...) routes through
|
|
15
|
+
:func:`to_decimal`, which is deliberately strict:
|
|
16
|
+
it accepts the numeric types an engineer is likely to have on hand, and rejects
|
|
17
|
+
everything else -- including other measurements -- with a message that (hopefully)
|
|
18
|
+
explains what went wrong.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
from decimal import Decimal
|
|
24
|
+
from fractions import Fraction
|
|
25
|
+
|
|
26
|
+
from kuchka.errors import InvalidMeasurementError
|
|
27
|
+
|
|
28
|
+
Numeric = int | float | Decimal | Fraction
|
|
29
|
+
|
|
30
|
+
_NUMERIC_TYPES = (int, float, Decimal, Fraction)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def to_decimal(value: object) -> Decimal:
|
|
34
|
+
"""Coerce ``value`` into an exact :class:`~decimal.Decimal`.
|
|
35
|
+
|
|
36
|
+
``float`` values are routed through ``str()`` first. ``Decimal(4.2)`` would
|
|
37
|
+
faithfully reproduce the binary float's rounding error
|
|
38
|
+
(``4.20000000000000017763...``);
|
|
39
|
+
``Decimal(str(4.2))`` reproduces what an engineer actually typed.
|
|
40
|
+
|
|
41
|
+
That one-line rule is what keeps float literals like ``Volt(4.2)`` numerically sane
|
|
42
|
+
without asking the engineer to write ``Volt(Decimal("4.2"))`` themselves.
|
|
43
|
+
"""
|
|
44
|
+
# NOTE: bool is a subclass of int in Python; True/False silently becoming 1/0
|
|
45
|
+
# inside a measurement is a classic footgun, so it is rejected explicitly rather
|
|
46
|
+
# than accepted by isinstance(value, int).
|
|
47
|
+
if isinstance(value, bool):
|
|
48
|
+
raise InvalidMeasurementError(
|
|
49
|
+
f"cannot construct a measurement from a bool ({value!r}); "
|
|
50
|
+
"expected int, float, Decimal, or Fraction",
|
|
51
|
+
)
|
|
52
|
+
if isinstance(value, Decimal):
|
|
53
|
+
return value
|
|
54
|
+
if isinstance(value, int):
|
|
55
|
+
return Decimal(value)
|
|
56
|
+
if isinstance(value, Fraction):
|
|
57
|
+
return Decimal(value.numerator) / Decimal(value.denominator)
|
|
58
|
+
if isinstance(value, float):
|
|
59
|
+
return Decimal(str(value))
|
|
60
|
+
|
|
61
|
+
type_name = type(value).__name__
|
|
62
|
+
hint = ""
|
|
63
|
+
if hasattr(value, "magnitude") or hasattr(value, "dimension"):
|
|
64
|
+
hint = (
|
|
65
|
+
" It looks like you passed a measurement where a plain number "
|
|
66
|
+
"was expected -- did you mean to use its .magnitude, or is "
|
|
67
|
+
"this a dimension mismatch?"
|
|
68
|
+
)
|
|
69
|
+
raise InvalidMeasurementError(
|
|
70
|
+
f"cannot construct a measurement from {type_name}; expected int, "
|
|
71
|
+
f"float, Decimal, or Fraction.{hint}",
|
|
72
|
+
)
|
kuchka/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""The generic quantity engine and the named engineering types built on it.
|
|
2
|
+
|
|
3
|
+
Application code should not need to read this package.
|
|
4
|
+
|
|
5
|
+
It exists so that ``Voltage``, ``Current``, ``Power``, ``Resistance``,
|
|
6
|
+
``TemperatureDelta``, and the rest of the vocabulary do not each have to hand-roll
|
|
7
|
+
their own arithmetic, conversion, and comparison logic:
|
|
8
|
+
they share one implementation (:class:`LinearQuantity`, in ``base.py``) and differ only
|
|
9
|
+
in their :class:`~kuchka.dimensions.Dimension` and, where relevant, their
|
|
10
|
+
multiplication and division overloads (``electrical.py``, ``thermal.py``).
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from kuchka.quantities.amount import AmountOfSubstance
|
|
16
|
+
from kuchka.quantities.base import LinearQuantity
|
|
17
|
+
from kuchka.quantities.base import UnitDef
|
|
18
|
+
from kuchka.quantities.charge import Charge
|
|
19
|
+
from kuchka.quantities.electrical import Current
|
|
20
|
+
from kuchka.quantities.electrical import Power
|
|
21
|
+
from kuchka.quantities.electrical import Resistance
|
|
22
|
+
from kuchka.quantities.electrical import Voltage
|
|
23
|
+
from kuchka.quantities.energy import Energy
|
|
24
|
+
from kuchka.quantities.kinematics import Velocity
|
|
25
|
+
from kuchka.quantities.length import Length
|
|
26
|
+
from kuchka.quantities.luminous_intensity import LuminousIntensity
|
|
27
|
+
from kuchka.quantities.mass import Mass
|
|
28
|
+
from kuchka.quantities.proportion import Proportion
|
|
29
|
+
from kuchka.quantities.temporal import BaudRate
|
|
30
|
+
from kuchka.quantities.temporal import ConvertibleToFrequency
|
|
31
|
+
from kuchka.quantities.temporal import Frequency
|
|
32
|
+
from kuchka.quantities.temporal import SampleRate
|
|
33
|
+
from kuchka.quantities.temporal import SamplingPeriod
|
|
34
|
+
from kuchka.quantities.temporal import Time
|
|
35
|
+
from kuchka.quantities.thermal import Temperature
|
|
36
|
+
from kuchka.quantities.thermal import TemperatureDelta
|
|
37
|
+
|
|
38
|
+
__all__: list[str] = [
|
|
39
|
+
"AmountOfSubstance",
|
|
40
|
+
"BaudRate",
|
|
41
|
+
"Charge",
|
|
42
|
+
"ConvertibleToFrequency",
|
|
43
|
+
"Current",
|
|
44
|
+
"Energy",
|
|
45
|
+
"Frequency",
|
|
46
|
+
"Length",
|
|
47
|
+
"LinearQuantity",
|
|
48
|
+
"LuminousIntensity",
|
|
49
|
+
"Mass",
|
|
50
|
+
"Power",
|
|
51
|
+
"Proportion",
|
|
52
|
+
"Resistance",
|
|
53
|
+
"SampleRate",
|
|
54
|
+
"SamplingPeriod",
|
|
55
|
+
"Temperature",
|
|
56
|
+
"TemperatureDelta",
|
|
57
|
+
"Time",
|
|
58
|
+
"UnitDef",
|
|
59
|
+
"Velocity",
|
|
60
|
+
"Voltage",
|
|
61
|
+
]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Amount Of Substance: a count of elementary entities, expressed in moles."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import ClassVar
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from kuchka.dimensions import AMOUNT_OF_SUBSTANCE
|
|
9
|
+
from kuchka.quantities.base import LinearQuantity
|
|
10
|
+
from kuchka.quantities.base import register
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from kuchka.dimensions import Dimension
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class AmountOfSubstance(LinearQuantity):
|
|
17
|
+
"""A count of elementary entities, expressed in moles."""
|
|
18
|
+
|
|
19
|
+
dimension: ClassVar[Dimension] = AMOUNT_OF_SUBSTANCE
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
register(AMOUNT_OF_SUBSTANCE, AmountOfSubstance)
|