Skip to content

Core Concepts

measured is a library for measurements and quantities.

PyPi Python Versions

measured

The goal of the measured library is to provide a sound foundation for recording and converting physical quantities, while maintaining the integrity of their units and dimensions.

While it aims to be the fastest library of its kind, automatically tracking the units and dimensions of quantities introduces significant overhead. You can use measured for applications where the accuracy of the units is more important than raw numerical computing speed.

The value classes of measured should generally be treated as immutable. Rather than setting the magnitude of a Quantity, consider instantiating a new one, or performing arithmetic on an existing one to produce new values. Instances of Dimension, Prefix, and Unit are always singletons within a Python process.

For more background on the approach taken by this library, please see the material on quantity calculus and dimensional analysis. measured uses the terms "dimension", "unit", "prefix", and "quantity" in the same way.

Examples:

>>> from measured.si import Meter, Second
>>> distance = 10 * Meter
>>> time = 2 * Second
>>> speed = distance / time
>>> assert speed == 5 * Meter / Second

Attributes: Fundamental dimensions

Number (Dimension): the basis of [counting and measuring][1], used to define
    [dimensionless quantities][2]

    [1]: https://en.wikipedia.org/wiki/Number

    [2]: https://en.wikipedia.org/wiki/Dimensionless_quantity

Length (Dimension): [distance][1] or extent through space

    [1]: https://en.wikipedia.org/wiki/Length

Time (Dimension): [what clocks read][1], the intervals between events

    [1]: https://en.wikipedia.org/wiki/Time

Mass (Dimension): how much [matter][1] is in a physical body

    [1]: https://en.wikipedia.org/wiki/Mass

Charge (Dimension): how strongly matter [interacts with the electric field][1]

    [1]: https://en.wikipedia.org/wiki/Electric_charge

Temperature (Dimension): the average amount of [kinetic energy][1] in a system

    [1]: https://en.wikipedia.org/wiki/Temperature

AmountOfSubstance (Dimension): how many [elementary entities][1] are in an object

    [1]: https://en.wikipedia.org/wiki/Mole_(unit)

LuminousIntensity (Dimension): how strong a [light source][1] is over a volume

    [1]: https://en.wikipedia.org/wiki/Luminous_intensity

Information (Dimension): how much [entropy][1] is present in a random variable

    [1]: https://en.wikipedia.org/wiki/Information#Information_theory

Attributes: Derived dimensions

Area (Dimension):

Volume (Dimension):

PlaneAngle (Dimension):

SolidAngle (Dimension):

Speed (Dimension):

Acceleration (Dimension):

Jerk (Dimension):

Snap (Dimension):

Crackle (Dimension):

Pop (Dimension):

Frequency (Dimension):

Force (Dimension):

Energy (Dimension):

Power (Dimension):

Current (Dimension):

Potential (Dimension):

Capacitance (Dimension):

Resistance (Dimension):

Conductance (Dimension):

Inductance (Dimension):

MagneticFlux (Dimension):

MagneticBField (Dimension):

LuminousFlux (Dimension):

Illuminance (Dimension):

RadioactiveDose (Dimension):

Catalysis (Dimension):

Attributes: Base Prefixes

IdentityPrefix (Prefix): represents the number 1, expressed as the prefix 0⁰

Attributes: Base Units

One (Unit): represents the number 1, expressed as a unit of dimension `Number`

Attributes: Logarithms

Bel (Logarithm): A logarithmic ratio in base 10
Decibel (Logarithm): 1/10th of a Bel
Neper (Logarithm): A logarithmic ratio in base _e_

measured.Dimension

Dimension represents the kind of physical quantity being measured.

Unless you're doing something really cool, you probably won't instantiate new dimensions directly. Instead, you'll import the base dimensions and combine them through multiplication, division, or exponentation.

Attributes:

Name Type Description
name Optional[str]

The name of this dimension, which may not be set in the case of complex dimensions

symbol Optional[str]

The conventional dimensional analysis symbol of this dimension

exponents Tuple[int, ...]

The exponents that define this dimension in terms of the fundamental dimensions

Examples:

>>> from measured import (
...     Number,
...     Length,
...     Time,
...     Mass,
...     Current,
...     Temperature,
...     AmountOfSubstance,
...     LuminousIntensity,
...     Information
... )
>>> Length.symbol
'L'
>>> Length.name
'length'

These dimensions form the basis for more complex dimensions, which can be
produced through multiplication, exponentation, and division.

>>> from measured import Area, Volume, Frequency, Speed
>>> assert Area == Length * Length
>>> assert Volume == Length**3
>>> assert Speed == Length / Time
>>> assert Frequency == Number / Time

`measured` maintains `Dimension` instances as singletons within a single
process, so they can be used in both equality and identity tests.

>>> assert Volume is Length**3

Dimensions are hashable and may be used as keys in dictionaries.

>>> lookup = {Volume: 'spacious'}
>>> lookup[Volume]
'spacious'

Dimensions can be serialized in a number of ways, preserving their identity.

>>> import pickle
>>> assert pickle.loads(pickle.dumps(Volume)) is Volume

>>> import json
>>> from measured.json import MeasuredJSONEncoder, MeasuredJSONDecoder
>>> json.dumps(Length, cls=MeasuredJSONEncoder)
'{"__measured__": "Dimension", "name": "length", "symbol": "L", ...}'

While using `measured`'s [JSON codecs](../serialization), Dimensions may be
deserialized directly from that JSON representation.

>>> encoded = json.dumps(Length, cls=MeasuredJSONEncoder)
>>> json.loads(encoded, cls=MeasuredJSONDecoder)
Dimension(exponents=(0, 1, 0, 0, 0, 0, 0, 0, 0, 0), name='length', symbol='L')

With measured's JSON codecs installed, you can omit passing the encoder and
decoder.

>>> from measured.json import codecs_installed
>>> with codecs_installed():
...     assert json.loads(json.dumps(Volume)) is Volume
Source code in src/measured/__init__.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
class Dimension:
    """Dimension represents the kind of physical quantity being measured.

    Unless you're doing something really cool, you probably won't instantiate new
    dimensions directly.  Instead, you'll import the base dimensions and combine them
    through multiplication, division, or exponentation.

    Attributes:
        name (Optional[str]): The name of this dimension, which may not be set in the
            case of complex dimensions

        symbol (Optional[str]): The conventional [dimensional
            analysis](https://en.wikipedia.org/wiki/Dimensional_analysis#Definition)
            symbol of this dimension

        exponents (Tuple[int, ...]): The exponents that define this dimension in terms
            of the fundamental dimensions

    Examples:

        >>> from measured import (
        ...     Number,
        ...     Length,
        ...     Time,
        ...     Mass,
        ...     Current,
        ...     Temperature,
        ...     AmountOfSubstance,
        ...     LuminousIntensity,
        ...     Information
        ... )
        >>> Length.symbol
        'L'
        >>> Length.name
        'length'

        These dimensions form the basis for more complex dimensions, which can be
        produced through multiplication, exponentation, and division.

        >>> from measured import Area, Volume, Frequency, Speed
        >>> assert Area == Length * Length
        >>> assert Volume == Length**3
        >>> assert Speed == Length / Time
        >>> assert Frequency == Number / Time

        `measured` maintains `Dimension` instances as singletons within a single
        process, so they can be used in both equality and identity tests.

        >>> assert Volume is Length**3

        Dimensions are hashable and may be used as keys in dictionaries.

        >>> lookup = {Volume: 'spacious'}
        >>> lookup[Volume]
        'spacious'

        Dimensions can be serialized in a number of ways, preserving their identity.

        >>> import pickle
        >>> assert pickle.loads(pickle.dumps(Volume)) is Volume

        >>> import json
        >>> from measured.json import MeasuredJSONEncoder, MeasuredJSONDecoder
        >>> json.dumps(Length, cls=MeasuredJSONEncoder)
        '{"__measured__": "Dimension", "name": "length", "symbol": "L", ...}'

        While using `measured`'s [JSON codecs](../serialization), Dimensions may be
        deserialized directly from that JSON representation.

        >>> encoded = json.dumps(Length, cls=MeasuredJSONEncoder)
        >>> json.loads(encoded, cls=MeasuredJSONDecoder)
        Dimension(exponents=(0, 1, 0, 0, 0, 0, 0, 0, 0, 0), name='length', symbol='L')

        With measured's JSON codecs installed, you can omit passing the encoder and
        decoder.

        >>> from measured.json import codecs_installed
        >>> with codecs_installed():
        ...     assert json.loads(json.dumps(Volume)) is Volume
    """

    _known: ClassVar[Dict[Tuple[int, ...], "Dimension"]] = {}
    _initialized: bool

    _fundamental: ClassVar[List["Dimension"]] = []
    _by_name: ClassVar[Dict[str, "Dimension"]] = {}

    __slots__ = ("_initialized", "exponents", "name", "symbol")

    exponents: Tuple[int, ...]
    name: Optional[str]
    symbol: Optional[str]

    def __new__(
        cls,
        exponents: Tuple[int, ...],
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> "Dimension":
        key = exponents
        if key in cls._known:
            return cls._known[key]

        self = super().__new__(cls)
        self._initialized = False
        cls._known[key] = self
        return self

    def __init__(
        self,
        exponents: Tuple[int, ...],
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> None:
        if self._initialized:
            return

        self.exponents = exponents
        self.name = name
        self.symbol = symbol
        self._initialized = True

        if name:
            self._by_name[name] = self

    @classmethod
    def fundamental(cls) -> Iterable["Dimension"]:
        """Returns the registered fundamental dimensions

        Fundamental dimensions are not derived from other dimensions, and they will
        always have an `exponents` tuple with a single `1`, like `[0, 0, 1, 0, 0...]`
        (except for `Number`, which is all `0`s).

        The fundamental dimensions are those defined directly using
        [`Dimension.define`][measured.Dimension.define], like those provided by
        `measured`.  The length and order of these Dimensions is also the length and
        order of each Dimensions `exponents` tuple.
        """
        return list(cls._fundamental)

    @classmethod
    def define(cls, name: str, symbol: str) -> "Dimension":
        """Defines a new fundamental Dimension

        Used by `measured` itself to define the fundamental Dimensions.  You may define
        additional dimensions, but be aware that doing so will change the cardinality of
        the `exponents` tuple for _all_ defined Dimensions.
        """
        index = len(cls._fundamental)
        if index == 0:
            # the first dimension must be Number, with an exponent of zero (identity)
            exponents = tuple([0])
        else:
            # other dimensions start with their exponent at one, and all others zero
            exponents = tuple(([0] * index) + [1])

        dimension = cls(exponents, name=name, symbol=symbol)

        # resize the keys for all previously known dimensions to account
        # for this new fundamental dimension
        for previous in list(cls._known.values()):
            del cls._known[previous.exponents]
            previous.exponents += (0,)
            cls._known[previous.exponents] = previous

        cls._fundamental.append(dimension)

        return dimension

    @classmethod
    def named(cls, name: str) -> "Dimension | None":
        """Return a previously registered named Dimension if it is registered"""
        return cls._by_name.get(name)

    @classmethod
    def derive(
        cls, dimension: "Dimension", name: str, symbol: Optional[str] = None
    ) -> "Dimension":
        """Registers a new named dimension derived from other dimension"""
        dimension.name = name
        dimension.symbol = symbol or str(dimension)
        cls._by_name[name] = dimension
        return dimension

    def unit(self, name: str, symbol: str) -> "Unit":
        """Define a new unit of this dimension"""
        return Unit.define(self, name, symbol)

    def scale(self, zero: "Quantity", name: str, symbol: str) -> "Unit":
        """Define a new scale of this dimension, setting a zero point of another unit"""
        unit = self.unit(name, symbol)
        conversions.translate(unit, zero)
        return unit

    # Pickle support

    def __getnewargs_ex__(self) -> Tuple[Tuple[Tuple[int, ...]], Dict[str, Any]]:
        return (self.exponents,), {}

    # JSON support

    def __json__(self) -> Dict[str, Any]:
        return {
            "__measured__": "Dimension",
            "name": self.name,
            "symbol": self.symbol,
            "exponents": list(self.exponents),
        }

    @classmethod
    def __from_json__(cls, json_object: Dict[str, Any]) -> "Dimension":
        return Dimension(tuple(json_object["exponents"]))

    # Pydantic support

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source: Type[Any], handler: "GetCoreSchemaHandler"
    ) -> "core_schema.CoreSchema":
        from pydantic_core import core_schema

        return core_schema.no_info_plain_validator_function(
            cls._pydantic_validate,
            serialization=core_schema.plain_serializer_function_ser_schema(
                cls.__json__, when_used="json-unless-none"
            ),
        )

    @classmethod
    def _pydantic_validate(
        cls, value: Union[str, Dict[str, Any], "Dimension"]
    ) -> "Dimension":
        if isinstance(value, dict):
            return cls.__from_json__(value)

        if isinstance(value, str):
            if value not in cls._by_name:
                raise ValueError(f"{value!r} is not a named Dimension")

            return cls._by_name[value]

        if isinstance(value, Dimension):
            return value

        raise ValueError(f"No conversion from {value!r} to Dimension")

    __repr__ = formatting.dimension_repr
    __str__ = formatting.dimension_str
    _repr_pretty_ = formatting.dimension_pretty
    _repr_html_ = formatting.mathml(formatting.dimension_mathml)

    # https://en.wikipedia.org/wiki/Dimensional_analysis#Dimensional_homogeneity
    #
    # Only commensurable quantities (physical quantities having the same dimension)
    # may be compared, equated, added, or subtracted.

    def __add__(self, other: "Dimension") -> "Dimension":
        if self is not other:
            return NotImplemented

        return self

    def __sub__(self, other: "Dimension") -> "Dimension":
        if self is not other:
            return NotImplemented

        return self

    # https://en.wikipedia.org/wiki/Dimensional_analysis#Dimensional_homogeneity
    #
    # The dimensions form an [abelian
    # group](https://en.wikipedia.org/wiki/Abelian_group) under multiplication, so:
    # One may take ratios of incommensurable quantities (quantities with different
    # dimensions), and multiply or divide them.

    @staticmethod
    @lru_cache(maxsize=None)
    def _multiply(self: "Dimension", other: "Dimension") -> "Dimension":
        return Dimension(tuple(s + o for s, o in zip(self.exponents, other.exponents)))

    def __mul__(self, other: "Dimension") -> "Dimension":
        if not isinstance(other, Dimension):
            return NotImplemented

        return Dimension._multiply(self, other)

    @staticmethod
    @lru_cache(maxsize=None)
    def _divide(self: "Dimension", other: "Dimension") -> "Dimension":
        return Dimension(tuple(s - o for s, o in zip(self.exponents, other.exponents)))

    def __truediv__(self, other: "Dimension") -> "Dimension":
        if not isinstance(other, Dimension):
            return NotImplemented

        return Dimension._divide(self, other)

    def __pow__(self, power: int) -> "Dimension":
        if not isinstance(power, int):
            return NotImplemented

        return Dimension(tuple(s * power for s in self.exponents))

    def root(self, degree: int) -> "Dimension":
        """Returns the nth root of this Dimension"""
        if not isinstance(degree, int):
            raise TypeError(f"degree should be an integer, not {type(degree)}")

        if degree == 0:
            return Number

        if any(s // degree != s / degree for s in self.exponents):
            raise FractionalDimensionError(degree, self)

        return Dimension(tuple(s // degree for s in self.exponents))

    @lru_cache(maxsize=None)
    def as_ratio(self) -> Tuple["Dimension", "Dimension"]:
        """Returns this dimension, split into a numerator and denominator"""
        numerator = tuple(e if e >= 0 else 0 for e in self.exponents)
        denominator = tuple(-e if e < 0 else 0 for e in self.exponents)
        return Dimension(numerator), Dimension(denominator)

    def is_factor(self, other: "Dimension") -> bool:
        """Returns true if this dimension is a factor of the other dimension"""
        if self is other or self is Number:
            return True

        exponents = [
            i
            for i, (mine, theirs) in enumerate(zip(self.exponents, other.exponents))
            if (theirs and mine) and theirs >= mine
        ]
        return bool(exponents)

as_ratio() -> Tuple[Dimension, Dimension] cached

Returns this dimension, split into a numerator and denominator

Source code in src/measured/__init__.py
518
519
520
521
522
523
@lru_cache(maxsize=None)
def as_ratio(self) -> Tuple["Dimension", "Dimension"]:
    """Returns this dimension, split into a numerator and denominator"""
    numerator = tuple(e if e >= 0 else 0 for e in self.exponents)
    denominator = tuple(-e if e < 0 else 0 for e in self.exponents)
    return Dimension(numerator), Dimension(denominator)

define(name: str, symbol: str) -> Dimension classmethod

Defines a new fundamental Dimension

Used by measured itself to define the fundamental Dimensions. You may define additional dimensions, but be aware that doing so will change the cardinality of the exponents tuple for all defined Dimensions.

Source code in src/measured/__init__.py
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
@classmethod
def define(cls, name: str, symbol: str) -> "Dimension":
    """Defines a new fundamental Dimension

    Used by `measured` itself to define the fundamental Dimensions.  You may define
    additional dimensions, but be aware that doing so will change the cardinality of
    the `exponents` tuple for _all_ defined Dimensions.
    """
    index = len(cls._fundamental)
    if index == 0:
        # the first dimension must be Number, with an exponent of zero (identity)
        exponents = tuple([0])
    else:
        # other dimensions start with their exponent at one, and all others zero
        exponents = tuple(([0] * index) + [1])

    dimension = cls(exponents, name=name, symbol=symbol)

    # resize the keys for all previously known dimensions to account
    # for this new fundamental dimension
    for previous in list(cls._known.values()):
        del cls._known[previous.exponents]
        previous.exponents += (0,)
        cls._known[previous.exponents] = previous

    cls._fundamental.append(dimension)

    return dimension

derive(dimension: Dimension, name: str, symbol: Optional[str] = None) -> Dimension classmethod

Registers a new named dimension derived from other dimension

Source code in src/measured/__init__.py
376
377
378
379
380
381
382
383
384
@classmethod
def derive(
    cls, dimension: "Dimension", name: str, symbol: Optional[str] = None
) -> "Dimension":
    """Registers a new named dimension derived from other dimension"""
    dimension.name = name
    dimension.symbol = symbol or str(dimension)
    cls._by_name[name] = dimension
    return dimension

fundamental() -> Iterable[Dimension] classmethod

Returns the registered fundamental dimensions

Fundamental dimensions are not derived from other dimensions, and they will always have an exponents tuple with a single 1, like [0, 0, 1, 0, 0...] (except for Number, which is all 0s).

The fundamental dimensions are those defined directly using Dimension.define, like those provided by measured. The length and order of these Dimensions is also the length and order of each Dimensions exponents tuple.

Source code in src/measured/__init__.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
@classmethod
def fundamental(cls) -> Iterable["Dimension"]:
    """Returns the registered fundamental dimensions

    Fundamental dimensions are not derived from other dimensions, and they will
    always have an `exponents` tuple with a single `1`, like `[0, 0, 1, 0, 0...]`
    (except for `Number`, which is all `0`s).

    The fundamental dimensions are those defined directly using
    [`Dimension.define`][measured.Dimension.define], like those provided by
    `measured`.  The length and order of these Dimensions is also the length and
    order of each Dimensions `exponents` tuple.
    """
    return list(cls._fundamental)

is_factor(other: Dimension) -> bool

Returns true if this dimension is a factor of the other dimension

Source code in src/measured/__init__.py
525
526
527
528
529
530
531
532
533
534
535
def is_factor(self, other: "Dimension") -> bool:
    """Returns true if this dimension is a factor of the other dimension"""
    if self is other or self is Number:
        return True

    exponents = [
        i
        for i, (mine, theirs) in enumerate(zip(self.exponents, other.exponents))
        if (theirs and mine) and theirs >= mine
    ]
    return bool(exponents)

named(name: str) -> Dimension | None classmethod

Return a previously registered named Dimension if it is registered

Source code in src/measured/__init__.py
371
372
373
374
@classmethod
def named(cls, name: str) -> "Dimension | None":
    """Return a previously registered named Dimension if it is registered"""
    return cls._by_name.get(name)

root(degree: int) -> Dimension

Returns the nth root of this Dimension

Source code in src/measured/__init__.py
505
506
507
508
509
510
511
512
513
514
515
516
def root(self, degree: int) -> "Dimension":
    """Returns the nth root of this Dimension"""
    if not isinstance(degree, int):
        raise TypeError(f"degree should be an integer, not {type(degree)}")

    if degree == 0:
        return Number

    if any(s // degree != s / degree for s in self.exponents):
        raise FractionalDimensionError(degree, self)

    return Dimension(tuple(s // degree for s in self.exponents))

scale(zero: Quantity, name: str, symbol: str) -> Unit

Define a new scale of this dimension, setting a zero point of another unit

Source code in src/measured/__init__.py
390
391
392
393
394
def scale(self, zero: "Quantity", name: str, symbol: str) -> "Unit":
    """Define a new scale of this dimension, setting a zero point of another unit"""
    unit = self.unit(name, symbol)
    conversions.translate(unit, zero)
    return unit

unit(name: str, symbol: str) -> Unit

Define a new unit of this dimension

Source code in src/measured/__init__.py
386
387
388
def unit(self, name: str, symbol: str) -> "Unit":
    """Define a new unit of this dimension"""
    return Unit.define(self, name, symbol)

measured.Unit

Unit is a predetermined reference amount or definition for a measurable quantity

measured includes a number of well-known units, and additional contributions are always welcome. The SI system of units is covered in the measured.si package.

Attributes:

name (Optional[str]): the name of this unit, which may not be set in the case
    of complex derived units

symbol (Optional[str]): the symbol of this unit, which may not be set in the
    case of complex derived units

prefix (Prefix): the prefix to multiply this unit by (`IdentityPrefix` for
    unprefixed units)

factors (Mapping["Unit", int]): a mapping of the base units and their powers
    that make up a compound unit, or `{self: 1}` for base units

Examples:

>>> from measured import Length, Time, Speed
>>> from measured.si import Meter, Second
>>> Meter.name, Meter.symbol
('meter', 'm')

>>> meter_per_second = Meter / Second
>>> assert meter_per_second.dimension is Speed
>>> assert meter_per_second.dimension is Length / Time
>>> meter_per_second.name, meter_per_second.symbol
(None, None)

>>> back_to_meter = (meter_per_second * Second)
>>> back_to_meter.name, back_to_meter.symbol
('meter', 'm')

>>> assert Meter.factors == {Meter: 1}
>>> assert Second.factors == {Second: 1}
>>> assert meter_per_second.factors == {Meter: 1, Second: -1}


[`Unit`][measured.Unit] values define format specifiers to control how they are
formatted as strings:

>>> from measured.si import Meter, Second
>>> str(Meter / Second)
'm⋅s⁻¹'
>>> f"{Meter / Second}"
'm⋅s⁻¹'
>>> f"{Meter**2 / Second**2}"
'm²⋅s⁻²'

The `/` format specifier presents the unit as a ratio:

>>> f"{Meter / Second:/}"
'm/s'
>>> f"{Meter**2/Second:/}"
'm²/s'
>>> f"{Meter**2/Second**2:/}"
'm²/s²'
Source code in src/measured/__init__.py
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
class Unit:
    """Unit is a predetermined reference amount or definition for a measurable quantity

    `measured` includes a number of well-known units, and additional contributions are
    always welcome.  The SI system of units is covered in the [`measured.si`](../si)
    package.

    Attributes:

        name (Optional[str]): the name of this unit, which may not be set in the case
            of complex derived units

        symbol (Optional[str]): the symbol of this unit, which may not be set in the
            case of complex derived units

        prefix (Prefix): the prefix to multiply this unit by (`IdentityPrefix` for
            unprefixed units)

        factors (Mapping["Unit", int]): a mapping of the base units and their powers
            that make up a compound unit, or `{self: 1}` for base units

    Examples:

        >>> from measured import Length, Time, Speed
        >>> from measured.si import Meter, Second
        >>> Meter.name, Meter.symbol
        ('meter', 'm')

        >>> meter_per_second = Meter / Second
        >>> assert meter_per_second.dimension is Speed
        >>> assert meter_per_second.dimension is Length / Time
        >>> meter_per_second.name, meter_per_second.symbol
        (None, None)

        >>> back_to_meter = (meter_per_second * Second)
        >>> back_to_meter.name, back_to_meter.symbol
        ('meter', 'm')

        >>> assert Meter.factors == {Meter: 1}
        >>> assert Second.factors == {Second: 1}
        >>> assert meter_per_second.factors == {Meter: 1, Second: -1}


        [`Unit`][measured.Unit] values define format specifiers to control how they are
        formatted as strings:

        >>> from measured.si import Meter, Second
        >>> str(Meter / Second)
        'm⋅s⁻¹'
        >>> f"{Meter / Second}"
        'm⋅s⁻¹'
        >>> f"{Meter**2 / Second**2}"
        'm²⋅s⁻²'

        The `/` format specifier presents the unit as a ratio:

        >>> f"{Meter / Second:/}"
        'm/s'
        >>> f"{Meter**2/Second:/}"
        'm²/s'
        >>> f"{Meter**2/Second**2:/}"
        'm²/s²'
    """

    UnitKey = Tuple[Prefix, Tuple[Tuple["Unit", int], ...]]

    _known: ClassVar[Dict[UnitKey, "Unit"]] = {}
    _initialized: bool

    _base: ClassVar[Set["Unit"]] = set()
    _by_name: ClassVar[Dict[str, "Unit"]] = {}
    _by_symbol: ClassVar[Dict[str, "Unit"]] = {}

    __slots__ = ("_initialized", "prefix", "factors", "dimension", "names", "symbols")

    prefix: Prefix
    factors: Mapping["Unit", int]
    dimension: Dimension
    names: Tuple[str, ...]
    symbols: Tuple[str, ...]

    def __new__(
        cls,
        prefix: Prefix,
        factors: Mapping["Unit", int],
        dimension: Dimension,
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> "Unit":
        key = cls._build_key(prefix, factors)
        if key in cls._known:
            return cls._known[key]

        if name and name in cls._by_name:
            return cls._by_name[name]

        self = super().__new__(cls)
        self._initialized = False
        if not factors:
            key = cls._build_key(prefix, {self: 1})
        cls._known[key] = self
        return self

    def __init__(
        self,
        prefix: Prefix,
        factors: Mapping["Unit", int],
        dimension: Dimension,
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> None:
        if self._initialized:
            return

        self.prefix = prefix
        self.factors = factors or {self: 1}
        self.dimension = dimension
        self.names = tuple()
        self.symbols = tuple()
        self.alias(name=name, symbol=symbol)
        self._initialized = True

    @classmethod
    def _build_key(cls, prefix: Prefix, factors: Mapping["Unit", int]) -> UnitKey:
        factor_key = tuple(sorted(factors.items(), key=lambda pair: id(pair[0])))
        return (prefix, factor_key)

    @classmethod
    def base(cls) -> Iterable["Unit"]:
        return list(cls._base)

    @classmethod
    def define(cls, dimension: Dimension, name: str, symbol: str) -> "Unit":
        """Defines a new base unit"""
        if name in cls._by_name:
            raise ValueError(f"A unit named {name} is already defined")

        if symbol in cls._by_symbol:
            raise ValueError(f"A unit with symbol {symbol} is already defined")

        unit = cls(IdentityPrefix, {}, dimension, name, symbol)
        cls._base.add(unit)
        return unit

    @classmethod
    def derive(cls, unit: "Unit", name: str, symbol: str) -> "Unit":
        """Registers a new named unit derived from other units"""
        unit.alias(name=name, symbol=symbol)
        return unit

    @classmethod
    def named(cls, name: str) -> "Unit":
        """Returns the unit with the given name"""
        return cls._by_name[name]

    @classmethod
    def resolve_symbol(cls, symbol: str) -> "Unit":
        """Returns the unit with the given (possibly prefixed) symbol"""
        if symbol in cls._by_symbol:
            return cls._by_symbol[symbol]

        for i in range(1, len(symbol)):
            try:
                prefix = Prefix.resolve_symbol(symbol[:i])
                unit = cls._by_symbol[symbol[i:]]
            except KeyError:
                continue

            return prefix * unit

        if symbol in cls._by_name:
            return cls._by_name[symbol]

        raise KeyError(f"No unit (or prefixed unit) matching {symbol!r}")

    def equals(self, other: "Quantity") -> None:
        """Defines a conversion between this Unit and another"""
        if other.unit == self and self is not One:
            raise ValueError("No need to define conversions for a unit and itself")
        conversions.equate(1 * self, other)

    def alias(self, name: Optional[str] = None, symbol: Optional[str] = None) -> None:
        """Adds an alternative name and/or symbol to the unit"""
        if name:
            if name in self._by_name and self._by_name[name] is not self:
                raise ValueError(f"A unit named {name} is already defined")

            self.names = self.names + (name,)
            self._by_name[name] = self

        if symbol:
            if symbol in self._by_symbol and self._by_symbol[symbol] is not self:
                raise ValueError(f"A unit with symbol {symbol} is already defined")

            if symbol and " " in symbol:
                raise ValueError(f"{symbol!r} will not be parsable if it has spaces.")

            self.symbols = self.symbols + (symbol,)
            self._by_symbol[symbol] = self

    @property
    def name(self) -> Optional[str]:
        return self.names[0] if self.names else None

    @property
    def symbol(self) -> Optional[str]:
        return self.symbols[0] if self.symbols else None

    # Pickle support

    def __getnewargs_ex__(
        self,
    ) -> Tuple[Tuple[Prefix, Mapping["Unit", int], Dimension], Dict[str, Any]]:
        factors = {} if self.factors == {self: 1} else self.factors
        args = (self.prefix, factors, self.dimension)
        kwargs = {"name": self.name, "symbol": self.symbol}
        return args, kwargs

    # JSON support

    def __json__(self) -> Dict[str, Any]:
        prefix, factors = self._build_key(self.prefix, self.factors)
        return {
            "__measured__": "Unit",
            "name": self.name,
            "symbol": self.symbol,
            "dimension": {"__measured__": "Dimension", **self.dimension.__json__()},
            "prefix": (
                {"__measured__": "Prefix", **prefix.__json__()}
                if prefix.quantify() != 1
                else None
            ),
            "factors": None if factors == ((self, 1),) else factors,
        }

    @classmethod
    def __from_json__(cls, json_object: Dict[str, Any]) -> "Unit":
        if not json_object["factors"]:
            return cls._by_name[json_object["name"]]

        prefix = json_object["prefix"] or Prefix(0, 0)
        factors = dict(json_object["factors"])
        dimension = json_object["dimension"]
        return Unit(prefix, factors, dimension)

    # Pydantic support

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source: Type[Any], handler: "GetCoreSchemaHandler"
    ) -> "core_schema.CoreSchema":
        from pydantic_core import core_schema

        return core_schema.no_info_plain_validator_function(
            cls._pydantic_validate,
            serialization=core_schema.plain_serializer_function_ser_schema(
                cls.__json__, when_used="json-unless-none"
            ),
        )

    @classmethod
    def _pydantic_validate(cls, value: Union[str, Dict[str, Any], "Unit"]) -> "Unit":
        if isinstance(value, dict):
            return cls.__from_json__(value)

        if isinstance(value, str):
            if value not in cls._by_name:
                raise ValueError(f"{value!r} is not a named Unit")

            return cls._by_name[value]

        if isinstance(value, Unit):
            return value

        raise ValueError(f"No conversion from {value!r} to Unit")

    __repr__ = formatting.unit_repr
    __str__ = formatting.unit_str
    __format__ = formatting.unit_format
    _repr_pretty_ = formatting.unit_pretty
    _repr_html_ = formatting.mathml(formatting.unit_mathml)

    @classmethod
    def parse(cls, string: str) -> "Unit":
        """
        Parses a unit from a string.

        Examples:

            It is important to import any modules of units you will be parsing.  Units
            and their symbols are registered when they are first imported and created.

            >>> from measured import Unit, si

            Integer and floating point quantities can be parsed, along with units of
            any complexity.

            >>> meter_per_second = Unit.parse('m/s')
            >>> meter_per_second.dimension is Speed
            True
            >>> amperes_cubed_per_area = Unit.parse('A³/m²')
            >>> from measured.si import Meter, Ampere
            >>> amperes_cubed_per_area is Ampere**3/Meter**2
            True
            >>> str(amperes_cubed_per_area)
            'C³⋅s⁻³⋅m⁻²'

            Measured can parse any unit in the same format it produces with `str`, but
            also understands easier-to-type versions:

            >>> assert Unit.parse('m^2/s') == Unit.parse('m²⋅s⁻¹')
            >>> assert Unit.parse('m^2*s') == Unit.parse('m²⋅s')
        """
        return cast(Unit, parser.parse(string, start="unit"))

    @classmethod
    def _simplify(cls, factors: Mapping["Unit", int]) -> Dict["Unit", int]:
        simplified = {
            unit: exponent
            for unit, exponent in factors.items()
            if unit is not One and exponent != 0
        }
        return simplified or {One: 1}

    @lru_cache(maxsize=None)
    def quantify(self) -> "Quantity":
        """Produce a Quantity of this Unit, including the Prefix.

        Examples:

            >>> from measured.si import Kilo, Meter
            >>> assert Kilo * Meter != 1000 * Meter
            >>> assert 1 * Kilo * Meter == 1000 * Meter
            >>> assert (Kilo * Meter).quantify() == 1000 * Meter
        """
        return self.prefix.quantify() * Unit(
            IdentityPrefix, self.factors, self.dimension
        )

    def __add__(self, other: "Unit") -> "Unit":
        if self is not other:
            return NotImplemented

        return self

    def __sub__(self, other: "Unit") -> "Unit":
        if self is not other:
            return NotImplemented

        return self

    @staticmethod
    @lru_cache(maxsize=None)
    def _multiply(self: "Unit", other: "Unit") -> "Unit":
        dimension = self.dimension * other.dimension
        prefix = self.prefix * other.prefix

        factors: Dict["Unit", int] = defaultdict(int, self.factors)
        for unit, exponent in other.factors.items():
            factors[unit] += exponent
        factors = self._simplify(factors)

        return Unit(prefix, factors, dimension)

    @overload
    def __mul__(self, other: "Unit") -> "Unit":
        ...  # pragma: no cover

    @overload
    def __mul__(self, other: Numeric) -> "Quantity":
        ...  # pragma: no cover

    def __mul__(self, other: Union["Unit", Numeric]) -> Union["Unit", "Quantity"]:
        if isinstance(other, NUMERIC_CLASSES):
            return Quantity(other, self)

        if not isinstance(other, Unit):
            return NotImplemented

        return Unit._multiply(self, other)

    __rmul__ = __mul__

    @staticmethod
    @lru_cache(maxsize=None)
    def _divide(self: "Unit", other: "Unit") -> "Unit":
        dimension = self.dimension / other.dimension
        prefix = self.prefix / other.prefix

        factors: Dict["Unit", int] = defaultdict(int, self.factors)
        for unit, exponent in other.factors.items():
            factors[unit] -= exponent
        factors = self._simplify(factors)

        return Unit(prefix, factors, dimension)

    def __truediv__(self, other: "Unit") -> "Unit":
        if not isinstance(other, Unit):
            return NotImplemented

        return Unit._divide(self, other)

    def __pow__(self, power: int) -> "Unit":
        if not isinstance(power, int):
            return NotImplemented

        dimension = self.dimension**power
        prefix = self.prefix**power

        factors = self._simplify(
            {unit: exponent * power for unit, exponent in self.factors.items()}
        )
        return Unit(prefix, factors, dimension)

    def root(self, degree: int) -> "Unit":
        """Returns the nth root of this Unit"""
        if not isinstance(degree, int):
            raise TypeError(f"degree should be an integer, not {type(degree)}")

        if degree == 0:
            return One

        dimension = self.dimension.root(degree)
        prefix = self.prefix.root(degree)

        if any(
            exponent // degree != exponent / degree
            for unit, exponent in self.factors.items()
            if exponent > 0 and unit is not One
        ):
            raise FractionalDimensionError(degree, self)

        factors = self._simplify(
            {unit: int(exponent // degree) for unit, exponent in self.factors.items()}
        )
        return Unit(prefix, factors, dimension)

    @lru_cache(maxsize=None)
    def as_ratio(self) -> Tuple["Unit", "Unit"]:
        """Returns this unit, split into a numerator and denominator"""
        numerator, denominator = self.dimension.as_ratio()
        return (
            Unit(
                self.prefix,
                {u: e for u, e in self.factors.items() if e >= 0} or {One: 1},
                numerator,
            ),
            Unit(
                IdentityPrefix,
                {u: -e for u, e in self.factors.items() if e < 0} or {One: 1},
                denominator,
            ),
        )

alias(name: Optional[str] = None, symbol: Optional[str] = None) -> None

Adds an alternative name and/or symbol to the unit

Source code in src/measured/__init__.py
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
def alias(self, name: Optional[str] = None, symbol: Optional[str] = None) -> None:
    """Adds an alternative name and/or symbol to the unit"""
    if name:
        if name in self._by_name and self._by_name[name] is not self:
            raise ValueError(f"A unit named {name} is already defined")

        self.names = self.names + (name,)
        self._by_name[name] = self

    if symbol:
        if symbol in self._by_symbol and self._by_symbol[symbol] is not self:
            raise ValueError(f"A unit with symbol {symbol} is already defined")

        if symbol and " " in symbol:
            raise ValueError(f"{symbol!r} will not be parsable if it has spaces.")

        self.symbols = self.symbols + (symbol,)
        self._by_symbol[symbol] = self

as_ratio() -> Tuple[Unit, Unit] cached

Returns this unit, split into a numerator and denominator

Source code in src/measured/__init__.py
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
@lru_cache(maxsize=None)
def as_ratio(self) -> Tuple["Unit", "Unit"]:
    """Returns this unit, split into a numerator and denominator"""
    numerator, denominator = self.dimension.as_ratio()
    return (
        Unit(
            self.prefix,
            {u: e for u, e in self.factors.items() if e >= 0} or {One: 1},
            numerator,
        ),
        Unit(
            IdentityPrefix,
            {u: -e for u, e in self.factors.items() if e < 0} or {One: 1},
            denominator,
        ),
    )

define(dimension: Dimension, name: str, symbol: str) -> Unit classmethod

Defines a new base unit

Source code in src/measured/__init__.py
942
943
944
945
946
947
948
949
950
951
952
953
@classmethod
def define(cls, dimension: Dimension, name: str, symbol: str) -> "Unit":
    """Defines a new base unit"""
    if name in cls._by_name:
        raise ValueError(f"A unit named {name} is already defined")

    if symbol in cls._by_symbol:
        raise ValueError(f"A unit with symbol {symbol} is already defined")

    unit = cls(IdentityPrefix, {}, dimension, name, symbol)
    cls._base.add(unit)
    return unit

derive(unit: Unit, name: str, symbol: str) -> Unit classmethod

Registers a new named unit derived from other units

Source code in src/measured/__init__.py
955
956
957
958
959
@classmethod
def derive(cls, unit: "Unit", name: str, symbol: str) -> "Unit":
    """Registers a new named unit derived from other units"""
    unit.alias(name=name, symbol=symbol)
    return unit

equals(other: Quantity) -> None

Defines a conversion between this Unit and another

Source code in src/measured/__init__.py
986
987
988
989
990
def equals(self, other: "Quantity") -> None:
    """Defines a conversion between this Unit and another"""
    if other.unit == self and self is not One:
        raise ValueError("No need to define conversions for a unit and itself")
    conversions.equate(1 * self, other)

named(name: str) -> Unit classmethod

Returns the unit with the given name

Source code in src/measured/__init__.py
961
962
963
964
@classmethod
def named(cls, name: str) -> "Unit":
    """Returns the unit with the given name"""
    return cls._by_name[name]

parse(string: str) -> Unit classmethod

Parses a unit from a string.

Examples:

It is important to import any modules of units you will be parsing.  Units
and their symbols are registered when they are first imported and created.

>>> from measured import Unit, si

Integer and floating point quantities can be parsed, along with units of
any complexity.

>>> meter_per_second = Unit.parse('m/s')
>>> meter_per_second.dimension is Speed
True
>>> amperes_cubed_per_area = Unit.parse('A³/m²')
>>> from measured.si import Meter, Ampere
>>> amperes_cubed_per_area is Ampere**3/Meter**2
True
>>> str(amperes_cubed_per_area)
'C³⋅s⁻³⋅m⁻²'

Measured can parse any unit in the same format it produces with `str`, but
also understands easier-to-type versions:

>>> assert Unit.parse('m^2/s') == Unit.parse('m²⋅s⁻¹')
>>> assert Unit.parse('m^2*s') == Unit.parse('m²⋅s')
Source code in src/measured/__init__.py
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
@classmethod
def parse(cls, string: str) -> "Unit":
    """
    Parses a unit from a string.

    Examples:

        It is important to import any modules of units you will be parsing.  Units
        and their symbols are registered when they are first imported and created.

        >>> from measured import Unit, si

        Integer and floating point quantities can be parsed, along with units of
        any complexity.

        >>> meter_per_second = Unit.parse('m/s')
        >>> meter_per_second.dimension is Speed
        True
        >>> amperes_cubed_per_area = Unit.parse('A³/m²')
        >>> from measured.si import Meter, Ampere
        >>> amperes_cubed_per_area is Ampere**3/Meter**2
        True
        >>> str(amperes_cubed_per_area)
        'C³⋅s⁻³⋅m⁻²'

        Measured can parse any unit in the same format it produces with `str`, but
        also understands easier-to-type versions:

        >>> assert Unit.parse('m^2/s') == Unit.parse('m²⋅s⁻¹')
        >>> assert Unit.parse('m^2*s') == Unit.parse('m²⋅s')
    """
    return cast(Unit, parser.parse(string, start="unit"))

quantify() -> Quantity cached

Produce a Quantity of this Unit, including the Prefix.

Examples:

>>> from measured.si import Kilo, Meter
>>> assert Kilo * Meter != 1000 * Meter
>>> assert 1 * Kilo * Meter == 1000 * Meter
>>> assert (Kilo * Meter).quantify() == 1000 * Meter
Source code in src/measured/__init__.py
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
@lru_cache(maxsize=None)
def quantify(self) -> "Quantity":
    """Produce a Quantity of this Unit, including the Prefix.

    Examples:

        >>> from measured.si import Kilo, Meter
        >>> assert Kilo * Meter != 1000 * Meter
        >>> assert 1 * Kilo * Meter == 1000 * Meter
        >>> assert (Kilo * Meter).quantify() == 1000 * Meter
    """
    return self.prefix.quantify() * Unit(
        IdentityPrefix, self.factors, self.dimension
    )

resolve_symbol(symbol: str) -> Unit classmethod

Returns the unit with the given (possibly prefixed) symbol

Source code in src/measured/__init__.py
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
@classmethod
def resolve_symbol(cls, symbol: str) -> "Unit":
    """Returns the unit with the given (possibly prefixed) symbol"""
    if symbol in cls._by_symbol:
        return cls._by_symbol[symbol]

    for i in range(1, len(symbol)):
        try:
            prefix = Prefix.resolve_symbol(symbol[:i])
            unit = cls._by_symbol[symbol[i:]]
        except KeyError:
            continue

        return prefix * unit

    if symbol in cls._by_name:
        return cls._by_name[symbol]

    raise KeyError(f"No unit (or prefixed unit) matching {symbol!r}")

root(degree: int) -> Unit

Returns the nth root of this Unit

Source code in src/measured/__init__.py
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
def root(self, degree: int) -> "Unit":
    """Returns the nth root of this Unit"""
    if not isinstance(degree, int):
        raise TypeError(f"degree should be an integer, not {type(degree)}")

    if degree == 0:
        return One

    dimension = self.dimension.root(degree)
    prefix = self.prefix.root(degree)

    if any(
        exponent // degree != exponent / degree
        for unit, exponent in self.factors.items()
        if exponent > 0 and unit is not One
    ):
        raise FractionalDimensionError(degree, self)

    factors = self._simplify(
        {unit: int(exponent // degree) for unit, exponent in self.factors.items()}
    )
    return Unit(prefix, factors, dimension)

measured.Prefix

Prefixes scale a Unit up or down by a constant factor.

Prefixes are defined in systems of factors with a common integer base, and successive positive and/or negative exponents, like the SI system of prefixes using base 10 (micro, milli, kilo, mega, etc) or the IEC system of binary prefixes (kibi, mebi, gibi, etc).

Attributes:

base (int): The base of the system, like `10` for the SI system
exponent (int | float): The exponent that the base is raised to

Examples:

Prefixes have a base and an exponent:

>>> from measured.si import Milli, Kilo
>>> Milli.symbol, Milli.base, Milli.exponent
('m', 10, -3)
>>> Kilo.symbol, Kilo.base, Kilo.exponent
('k', 10, 3)

Prefixes can be multiplied by numbers, Prefixes, and Units:

>>> from measured.si import Kilo, Mega, Meter
>>> assert 1 * Kilo * Meter == 1000 * Meter
>>> assert 1 * (Kilo * Kilo * Meter) == 1 * Mega * Meter

Prefixes can be divided by Prefixes and other prefixed units:

>>> from measured.si import Kilo, Mega, Meter, Second
>>> assert (1 * Mega * Meter) / (1 * Kilo * Second) == 1000 * Meter / Second

Prefixes can be raised to an exponent:

>>> from measured.si import Kilo, Mega, Meter
>>> assert (2 * Kilo * Meter)**2 == 4 * Mega * Meter**2

Prefixes can translate between bases, although be careful with the loss of
precision during this conversion:

>>> from measured.si import Kilo
>>> from measured.iec import Mebi, Bit
>>> assert 1 * Mebi * Bit == 1048.576 * Kilo * Bit
Source code in src/measured/__init__.py
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
class Prefix:
    """Prefixes scale a [`Unit`][measured.Unit] up or down by a constant factor.

    Prefixes are defined in systems of factors with a common integer base, and
    successive positive and/or negative exponents, like the SI system of prefixes using
    base 10 (micro, milli, kilo, mega, etc) or the IEC system of binary prefixes (kibi,
    mebi, gibi, etc).

    Attributes:

        base (int): The base of the system, like `10` for the SI system
        exponent (int | float): The exponent that the base is raised to

    Examples:

        Prefixes have a base and an exponent:

        >>> from measured.si import Milli, Kilo
        >>> Milli.symbol, Milli.base, Milli.exponent
        ('m', 10, -3)
        >>> Kilo.symbol, Kilo.base, Kilo.exponent
        ('k', 10, 3)

        Prefixes can be multiplied by numbers, Prefixes, and Units:

        >>> from measured.si import Kilo, Mega, Meter
        >>> assert 1 * Kilo * Meter == 1000 * Meter
        >>> assert 1 * (Kilo * Kilo * Meter) == 1 * Mega * Meter

        Prefixes can be divided by Prefixes and other prefixed units:

        >>> from measured.si import Kilo, Mega, Meter, Second
        >>> assert (1 * Mega * Meter) / (1 * Kilo * Second) == 1000 * Meter / Second

        Prefixes can be raised to an exponent:

        >>> from measured.si import Kilo, Mega, Meter
        >>> assert (2 * Kilo * Meter)**2 == 4 * Mega * Meter**2

        Prefixes can translate between bases, although be careful with the loss of
        precision during this conversion:

        >>> from measured.si import Kilo
        >>> from measured.iec import Mebi, Bit
        >>> assert 1 * Mebi * Bit == 1048.576 * Kilo * Bit

    """

    _known: ClassVar[Dict[Tuple[int, Numeric], "Prefix"]] = {}
    _initialized: bool

    _by_name: ClassVar[Dict[str, "Prefix"]] = {}
    _by_symbol: ClassVar[Dict[str, "Prefix"]] = {}

    __slots__ = ("_initialized", "base", "exponent", "name", "symbol")

    base: int
    exponent: Numeric
    name: Optional[str]
    symbol: Optional[str]

    def __new__(
        cls,
        base: int,
        exponent: Numeric,
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> "Prefix":
        if base != 0 and exponent == 0:
            return IdentityPrefix

        key = (base, exponent)
        if key in cls._known:
            return cls._known[key]

        self = super().__new__(cls)
        self._initialized = False
        cls._known[key] = self
        return self

    def __init__(
        self,
        base: int,
        exponent: Numeric,
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> None:
        if self._initialized:
            return

        self.base = base
        self.exponent = exponent
        self.name = name
        self.symbol = symbol
        self._initialized = True

        if name:
            self._by_name[name] = self
        if symbol:
            self._by_symbol[symbol] = self

    @classmethod
    def resolve_symbol(cls, symbol: str) -> "Prefix":
        """Returns the Prefix with the given symbol"""
        return cls._by_symbol[symbol]

    # Pickle support

    def __getnewargs_ex__(self) -> Tuple[Tuple[int, Numeric], Dict[str, Any]]:
        return (self.base, self.exponent), {}

    # JSON support

    def __json__(self) -> Dict[str, Any]:
        return {
            "__measured__": "Prefix",
            "base": self.base,
            "exponent": self.exponent,
            "name": self.name,
            "symbol": self.symbol,
        }

    @classmethod
    def __from_json__(cls, json_object: Dict[str, Any]) -> "Prefix":
        return Prefix(json_object["base"], json_object["exponent"])

    # Pydantic support

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source: Type[Any], handler: "GetCoreSchemaHandler"
    ) -> "core_schema.CoreSchema":
        from pydantic_core import core_schema

        return core_schema.no_info_plain_validator_function(
            cls._pydantic_validate,
            serialization=core_schema.plain_serializer_function_ser_schema(
                cls.__json__, when_used="json-unless-none"
            ),
        )

    @classmethod
    def _pydantic_validate(
        cls, value: Union[str, Dict[str, Any], "Prefix"]
    ) -> "Prefix":
        if isinstance(value, dict):
            return cls.__from_json__(value)

        if isinstance(value, str):
            if value not in cls._by_name:
                raise ValueError(f"{value!r} is not a named Prefix")

            return cls._by_name[value]

        if isinstance(value, Prefix):
            return value

        raise ValueError(f"No conversion from {value!r} to Prefix")

    __repr__ = formatting.prefix_repr
    __str__ = formatting.prefix_str
    _repr_pretty_ = formatting.prefix_pretty
    _repr_html_ = formatting.mathml(formatting.prefix_mathml)

    def quantify(self) -> Numeric:
        return cast(Numeric, self.base**self.exponent)

    @overload
    def __mul__(self, other: "Prefix") -> "Prefix":
        ...  # pragma: no cover

    @overload
    def __mul__(self, other: "Unit") -> "Unit":
        ...  # pragma: no cover

    @overload
    def __mul__(self, other: Numeric) -> "Quantity":
        ...  # pragma: no cover

    def __mul__(
        self, other: Union["Prefix", "Unit", Numeric]
    ) -> Union["Prefix", "Unit", "Quantity"]:
        if isinstance(other, Prefix):
            if other.base == 0:
                return self
            elif self.base == 0:
                return other
            elif other.base == self.base:
                return Prefix(self.base, _add(self.exponent, other.exponent))

            base, exponent = self.base, self.exponent
            base_change = _mul(
                other.exponent, (math.log(other.base) / math.log(self.base))
            )

            return Prefix(base, _add(exponent, base_change))

        if isinstance(other, Unit):
            return Unit(other.prefix * self, other.factors, other.dimension)

        if isinstance(other, NUMERIC_CLASSES):
            return (other * One) * self.quantify()

        return NotImplemented

    __rmul__ = __mul__

    def __truediv__(self, other: "Prefix") -> "Prefix":
        if not isinstance(other, Prefix):
            return NotImplemented

        if other.base == 0:
            return self
        elif self.base == 0:
            return Prefix(other.base, -other.exponent)
        elif other.base == self.base:
            return Prefix(self.base, _sub(self.exponent, other.exponent))

        base, exponent = self.base, self.exponent
        base_change = _mul(other.exponent, (math.log(other.base) / math.log(self.base)))

        return Prefix(base, _sub(exponent, base_change))

    def __pow__(self, power: int) -> "Prefix":
        return Prefix(self.base, self.exponent * power)

    def root(self, degree: int) -> "Prefix":
        """Returns the nth root of this Prefix"""
        if not isinstance(degree, int):
            raise TypeError(f"degree should be an integer, not {type(degree)}")

        if degree == 0:
            return IdentityPrefix

        if self.exponent // degree != self.exponent / degree:
            raise FractionalDimensionError(degree, self)

        return Prefix(self.base, int(self.exponent // degree))

resolve_symbol(symbol: str) -> Prefix classmethod

Returns the Prefix with the given symbol

Source code in src/measured/__init__.py
672
673
674
675
@classmethod
def resolve_symbol(cls, symbol: str) -> "Prefix":
    """Returns the Prefix with the given symbol"""
    return cls._by_symbol[symbol]

root(degree: int) -> Prefix

Returns the nth root of this Prefix

Source code in src/measured/__init__.py
797
798
799
800
801
802
803
804
805
806
807
808
def root(self, degree: int) -> "Prefix":
    """Returns the nth root of this Prefix"""
    if not isinstance(degree, int):
        raise TypeError(f"degree should be an integer, not {type(degree)}")

    if degree == 0:
        return IdentityPrefix

    if self.exponent // degree != self.exponent / degree:
        raise FractionalDimensionError(degree, self)

    return Prefix(self.base, int(self.exponent // degree))

measured.Quantity

Quantity represents a quantity of some Unit

Attributes:

magnitude (int | float): the quantity

unit (Unit): the [`Unit`][measured.Unit]

Examples:

All of the arithmetic operations are supported between Quantities.
Multiplication and division with [`Unit`][measured.Unit], `int`, and `float`
are also supported.

>>> from measured.si import Meter, Second
>>> assert 2 * Meter + 3 * Meter == 5 * Meter
>>> assert 3 * Meter - 1 * Meter == 2 * Meter
>>> assert (2 * Meter) / (1 * Second) == 2 * Meter / Second
>>> assert 10 * Meter / 2 == 5 * Meter
>>> assert 10 * Meter * 2 == 20 * Meter
>>> assert (10 * Meter)**2 == 100 * Meter**2

Keep an eye on Python's operator precedence, which may lead to surprising
results.  Consider the following:

>>> assert (2 * Meter / 1 * Second) != (2 * Meter) / (1 * Second)

In the above example, the order of operations on the left is:

* `2 * Meter` → `Quantity(2, Meter)`
* `Quantity(2, Meter) / 1` → `Quantity(2, Meter)`
* `Quantity(2, Meter) * Second` → `Quantity(2, Meter * Second)`

But on the right, the order is:

* `2 * Meter` → `Quantity(2, Meter)`
* `1 * Second` → `Quantity(1, Second)`
* `Quantity(2, Meter) / Quantity(1, Second)` → `Quantity(2, Meter / Second)`

[`Quantity`][measured.Quantity] values define format specifiers to control how
they are formatted as strings:

>>> from measured.si import Meter, Second
>>> str(5 * Meter / Second)
'5 m⋅s⁻¹'
>>> f"{5 * Meter / Second}"
'5 m⋅s⁻¹'
>>> f"{5 * Meter**2 / Second**2}"
'5 m²⋅s⁻²'

When formatting a [`Quantity`][measured.Quantity], you can specify two separate
format specifiers, separated by a `:`.  The first specifier is used to format
the `magnitude`, and the second is used to format the `unit` (see
[`Unit`][measured.Unit] for more information about the available specifiers).

Specifying both the magnitude and unit format:

>>> f"{5.1234 * Meter / Second:.2f:/}"
'5.12 m/s'

Specifying only the magnitude's format:

>>> f"{5.1234 * Meter / Second:.2f}"
'5.12 m⋅s⁻¹'

Specifying only the unit's format:

>>> f"{5.1234 * Meter / Second::/}"
'5.1234 m/s'
Source code in src/measured/__init__.py
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
@total_ordering
class Quantity:
    """Quantity represents a quantity of some Unit

    Attributes:

        magnitude (int | float): the quantity

        unit (Unit): the [`Unit`][measured.Unit]

    Examples:

        All of the arithmetic operations are supported between Quantities.
        Multiplication and division with [`Unit`][measured.Unit], `int`, and `float`
        are also supported.

        >>> from measured.si import Meter, Second
        >>> assert 2 * Meter + 3 * Meter == 5 * Meter
        >>> assert 3 * Meter - 1 * Meter == 2 * Meter
        >>> assert (2 * Meter) / (1 * Second) == 2 * Meter / Second
        >>> assert 10 * Meter / 2 == 5 * Meter
        >>> assert 10 * Meter * 2 == 20 * Meter
        >>> assert (10 * Meter)**2 == 100 * Meter**2

        Keep an eye on Python's operator precedence, which may lead to surprising
        results.  Consider the following:

        >>> assert (2 * Meter / 1 * Second) != (2 * Meter) / (1 * Second)

        In the above example, the order of operations on the left is:

        * `2 * Meter` → `Quantity(2, Meter)`
        * `Quantity(2, Meter) / 1` → `Quantity(2, Meter)`
        * `Quantity(2, Meter) * Second` → `Quantity(2, Meter * Second)`

        But on the right, the order is:

        * `2 * Meter` → `Quantity(2, Meter)`
        * `1 * Second` → `Quantity(1, Second)`
        * `Quantity(2, Meter) / Quantity(1, Second)` → `Quantity(2, Meter / Second)`

        [`Quantity`][measured.Quantity] values define format specifiers to control how
        they are formatted as strings:

        >>> from measured.si import Meter, Second
        >>> str(5 * Meter / Second)
        '5 m⋅s⁻¹'
        >>> f"{5 * Meter / Second}"
        '5 m⋅s⁻¹'
        >>> f"{5 * Meter**2 / Second**2}"
        '5 m²⋅s⁻²'

        When formatting a [`Quantity`][measured.Quantity], you can specify two separate
        format specifiers, separated by a `:`.  The first specifier is used to format
        the `magnitude`, and the second is used to format the `unit` (see
        [`Unit`][measured.Unit] for more information about the available specifiers).

        Specifying both the magnitude and unit format:

        >>> f"{5.1234 * Meter / Second:.2f:/}"
        '5.12 m/s'

        Specifying only the magnitude's format:

        >>> f"{5.1234 * Meter / Second:.2f}"
        '5.12 m⋅s⁻¹'

        Specifying only the unit's format:

        >>> f"{5.1234 * Meter / Second::/}"
        '5.1234 m/s'

    """

    __slots__ = ("magnitude", "unit")

    magnitude: Numeric
    unit: Unit

    def __init__(self, magnitude: Numeric, unit: Union[Unit, str]):
        self.magnitude = magnitude
        if isinstance(unit, str):
            unit = Unit.parse(unit)
        self.unit = unit

    def unprefixed(self) -> "Quantity":
        """Reduces this Quantity into a new Quantity expressed only in base units
        without any Prefixes"""
        return self.magnitude * self.unit.quantify()

    def in_unit(self, other: Unit) -> "Quantity":
        """Convert this Quantity into another unit"""
        return conversions.convert(self, other)

    # JSON support

    def __json__(self) -> Dict[str, Any]:
        magnitude: Union[Numeric, str] = self.magnitude
        if isinstance(magnitude, Decimal):
            magnitude = str(magnitude)
        return {
            "__measured__": "Quantity",
            "magnitude": magnitude,
            "unit": str(self.unit),
        }

    @classmethod
    def __from_json__(cls, json_object: Dict[str, Any]) -> "Quantity":
        magnitude = json_object["magnitude"]
        if isinstance(magnitude, str):
            magnitude = Decimal(magnitude)
        return Quantity(magnitude, json_object["unit"])

    # Pydantic support

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source: Type[Any], handler: "GetCoreSchemaHandler"
    ) -> "core_schema.CoreSchema":
        from pydantic_core import core_schema

        return core_schema.no_info_plain_validator_function(
            cls._pydantic_validate,
            serialization=core_schema.plain_serializer_function_ser_schema(
                cls.__json__, when_used="json-unless-none"
            ),
        )

    @classmethod
    def _pydantic_validate(
        cls, value: Union[str, Dict[str, Any], "Quantity"]
    ) -> "Quantity":
        if isinstance(value, dict):
            return cls.__from_json__(value)

        if isinstance(value, Quantity):
            return value

        raise ValueError(f"No conversion from {value!r} to Quantity")

    # SQLAlchemy support

    def __composite_values__(self) -> Tuple[Numeric, str]:
        """
        From the [SQLAlchemy documentation][1]:

            The requirements for the custom datatype class are that it have a
            constructor which accepts positional arguments corresponding to its column
            format, and also provides a method __composite_values__() which returns the
            state of the object as a list or tuple, in order of its column-based
            attributes. It also should supply adequate __eq__() and __ne__() methods
            which test the equality of two instances.

            [1]: https://docs.sqlalchemy.org/en/20/orm/composites.html
        """
        return self.magnitude, str(self.unit)

    __repr__ = formatting.quantity_repr
    __str__ = formatting.quantity_str
    __format__ = formatting.quantity_format
    _repr_pretty_ = formatting.quantity_pretty
    _repr_html_ = formatting.mathml(formatting.quantity_mathml)

    @classmethod
    def parse(cls, string: str) -> "Quantity":
        """
        Parses a quantity from a string.

        Examples:

            It is important to import any modules of units you will be parsing.  Units
            and their symbols are registered when they are first imported and created.

            >>> from measured import Quantity, si

            Integer and floating point quantities can be parsed, along with units of
            any complexity.

            >>> distance = Quantity.parse('5200 m')
            >>> speed = Quantity.parse('5.2e2 m/s')
            >>> time = distance / speed
            >>> str(time)
            '10.0 s'
            >>> amperes_cubed_per_area = Quantity.parse('2 A³/m²')
            >>> str(amperes_cubed_per_area)
            '2 C³⋅s⁻³⋅m⁻²'

            Measured can parse any unit in the same format it produces with `str`, but
            also understands easier-to-type versions:

            >>> assert Quantity.parse('2 m^2/s') == Quantity.parse('2 m²⋅s⁻¹')
            >>> assert Quantity.parse('2 m^2*s') == Quantity.parse('2 m²⋅s')
        """
        return cast(Quantity, parser.parse(string, start="quantity"))

    def __hash__(self) -> int:
        return hash((self.magnitude, self.unit))

    def __add__(self, other: "Quantity") -> "Quantity":
        if isinstance(other, Quantity):
            other = other.in_unit(self.unit)
            return Quantity(_add(self.magnitude, other.magnitude), self.unit)

        return NotImplemented

    def __sub__(self, other: "Quantity") -> "Quantity":
        if isinstance(other, Quantity):
            other = other.in_unit(self.unit)
            return Quantity(_sub(self.magnitude, other.magnitude), self.unit)

        return NotImplemented

    def __mul__(self, other: Union["Quantity", "Unit", Numeric]) -> "Quantity":
        if isinstance(other, Unit):
            return Quantity(self.magnitude, self.unit * other)

        if isinstance(other, Quantity):
            return Quantity(
                _mul(self.magnitude, other.magnitude), self.unit * other.unit
            )

        if isinstance(other, NUMERIC_CLASSES):
            return Quantity(_mul(self.magnitude, other), self.unit)

        return NotImplemented

    __rmul__ = __mul__

    def __truediv__(self, other: Union["Quantity", "Unit", Numeric]) -> "Quantity":
        if isinstance(other, Unit):
            return Quantity(self.magnitude, self.unit / other)

        if isinstance(other, Quantity):
            return Quantity(
                _div(self.magnitude, other.magnitude), self.unit / other.unit
            )

        if isinstance(other, NUMERIC_CLASSES):
            return Quantity(_div(self.magnitude, other), self.unit)

        return NotImplemented

    def __rtruediv__(self, other: Numeric) -> "Quantity":
        if isinstance(other, NUMERIC_CLASSES):
            return Quantity(_div(other, self.magnitude), self.unit)

        return NotImplemented

    def __pow__(self, power: int) -> "Quantity":
        return Quantity(self.magnitude**power, self.unit**power)

    def root(self, degree: int) -> "Quantity":
        """Returns the nth root of this Quantity"""
        if degree == 0:
            return 1 * One
        return Quantity(_pow(self.magnitude, (1 / degree)), self.unit.root(degree))

    def __neg__(self) -> "Quantity":
        return Quantity(-self.magnitude, self.unit)

    def __pos__(self) -> "Quantity":
        return Quantity(+self.magnitude, self.unit)

    def __abs__(self) -> "Quantity":
        absolute = abs(self.magnitude)
        assert isinstance(absolute, NUMERIC_CLASSES)
        return Quantity(absolute, self.unit)

    def __eq__(self, other: Any) -> bool:
        if isinstance(other, Level):
            other = other.quantify()

        if not isinstance(other, Quantity):
            return NotImplemented

        if self.unit.dimension != other.unit.dimension:
            return NotImplemented

        this = self.unprefixed()
        other = other.unprefixed()

        if this.unit == other.unit:
            return this.magnitude == other.magnitude

        try:
            return this.in_unit(other.unit) == other
        except conversions.ConversionNotFound:
            return NotImplemented

    def __lt__(self, other: Any) -> bool:
        if isinstance(other, Level):
            other = other.quantify()

        if not isinstance(other, Quantity):
            return NotImplemented

        if self.unit.dimension != other.unit.dimension:
            return NotImplemented

        this = self.unprefixed()
        other = other.unprefixed()

        if this.unit == other.unit:
            return this.magnitude < other.magnitude

        try:
            return this.in_unit(other.unit) < other
        except conversions.ConversionNotFound:
            return NotImplemented

    def level(self, unit: "LogarithmicUnit") -> "Level":
        return unit.level(self)

__composite_values__() -> Tuple[Numeric, str]

From the [SQLAlchemy documentation][1]:

The requirements for the custom datatype class are that it have a
constructor which accepts positional arguments corresponding to its column
format, and also provides a method __composite_values__() which returns the
state of the object as a list or tuple, in order of its column-based
attributes. It also should supply adequate __eq__() and __ne__() methods
which test the equality of two instances.

[1]: https://docs.sqlalchemy.org/en/20/orm/composites.html
Source code in src/measured/__init__.py
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
def __composite_values__(self) -> Tuple[Numeric, str]:
    """
    From the [SQLAlchemy documentation][1]:

        The requirements for the custom datatype class are that it have a
        constructor which accepts positional arguments corresponding to its column
        format, and also provides a method __composite_values__() which returns the
        state of the object as a list or tuple, in order of its column-based
        attributes. It also should supply adequate __eq__() and __ne__() methods
        which test the equality of two instances.

        [1]: https://docs.sqlalchemy.org/en/20/orm/composites.html
    """
    return self.magnitude, str(self.unit)

in_unit(other: Unit) -> Quantity

Convert this Quantity into another unit

Source code in src/measured/__init__.py
1356
1357
1358
def in_unit(self, other: Unit) -> "Quantity":
    """Convert this Quantity into another unit"""
    return conversions.convert(self, other)

parse(string: str) -> Quantity classmethod

Parses a quantity from a string.

Examples:

It is important to import any modules of units you will be parsing.  Units
and their symbols are registered when they are first imported and created.

>>> from measured import Quantity, si

Integer and floating point quantities can be parsed, along with units of
any complexity.

>>> distance = Quantity.parse('5200 m')
>>> speed = Quantity.parse('5.2e2 m/s')
>>> time = distance / speed
>>> str(time)
'10.0 s'
>>> amperes_cubed_per_area = Quantity.parse('2 A³/m²')
>>> str(amperes_cubed_per_area)
'2 C³⋅s⁻³⋅m⁻²'

Measured can parse any unit in the same format it produces with `str`, but
also understands easier-to-type versions:

>>> assert Quantity.parse('2 m^2/s') == Quantity.parse('2 m²⋅s⁻¹')
>>> assert Quantity.parse('2 m^2*s') == Quantity.parse('2 m²⋅s')
Source code in src/measured/__init__.py
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
@classmethod
def parse(cls, string: str) -> "Quantity":
    """
    Parses a quantity from a string.

    Examples:

        It is important to import any modules of units you will be parsing.  Units
        and their symbols are registered when they are first imported and created.

        >>> from measured import Quantity, si

        Integer and floating point quantities can be parsed, along with units of
        any complexity.

        >>> distance = Quantity.parse('5200 m')
        >>> speed = Quantity.parse('5.2e2 m/s')
        >>> time = distance / speed
        >>> str(time)
        '10.0 s'
        >>> amperes_cubed_per_area = Quantity.parse('2 A³/m²')
        >>> str(amperes_cubed_per_area)
        '2 C³⋅s⁻³⋅m⁻²'

        Measured can parse any unit in the same format it produces with `str`, but
        also understands easier-to-type versions:

        >>> assert Quantity.parse('2 m^2/s') == Quantity.parse('2 m²⋅s⁻¹')
        >>> assert Quantity.parse('2 m^2*s') == Quantity.parse('2 m²⋅s')
    """
    return cast(Quantity, parser.parse(string, start="quantity"))

root(degree: int) -> Quantity

Returns the nth root of this Quantity

Source code in src/measured/__init__.py
1517
1518
1519
1520
1521
def root(self, degree: int) -> "Quantity":
    """Returns the nth root of this Quantity"""
    if degree == 0:
        return 1 * One
    return Quantity(_pow(self.magnitude, (1 / degree)), self.unit.root(degree))

unprefixed() -> Quantity

Reduces this Quantity into a new Quantity expressed only in base units without any Prefixes

Source code in src/measured/__init__.py
1351
1352
1353
1354
def unprefixed(self) -> "Quantity":
    """Reduces this Quantity into a new Quantity expressed only in base units
    without any Prefixes"""
    return self.magnitude * self.unit.quantify()

measured.Logarithm

A Logarithm forms a family of LogarithmicUnits, which measures the ratio of a measured Quantity to a reference Quantity on a logarithmic scale. Commonly used logarithms include the Decibel (base 10) and the Neper (base e)

The logarithm is not a unit itself, but rather a family of units, each with a specific reference Quantity. For example, the unit dBW, commonly used in electrical and communications applications, measures the ratio of a Power quantity against the reference value 1 Watt, in increments of 10 (the base) to the 1/10th power (the deci- prefix).

Examples:

Use a `Logarithm`, like `Decibel`, to _create_ a new
[`LogarithmicUnit`][measured.LogarithmicUnit] based on a reference value.  In
the example below, the reference value is 1 Watt:

>>> from measured import Decibel
>>> from measured.si import Watt
>>> dBW = Decibel[1 * Watt]

The `dBW` unit may now be used to produces [`Levels`][measured.Level] that may
be used in operations with Quantities of the same dimension (`Power`, in this
case):

>>> assert 100 * Watt == 20 * dBW
Source code in src/measured/__init__.py
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
class Logarithm:
    """A `Logarithm` forms a family of [`LogarithmicUnits`][measured.LogarithmicUnit],
    which measures the _ratio_ of a measured [`Quantity`][measured.Quantity] to a
    reference [`Quantity`][measured.Quantity] on a logarithmic scale.  Commonly used
    logarithms include the `Decibel` (base 10) and the `Neper` (base _e_)

    The logarithm is not a unit itself, but rather a family of units, each with a
    specific reference [`Quantity`][measured.Quantity].  For example, the unit `dBW`,
    commonly used in electrical and communications applications, measures the ratio of a
    `Power` quantity against the reference value 1 Watt, in increments of 10 (the base)
    to the 1/10th power (the deci- prefix).

    Examples:

        Use a `Logarithm`, like `Decibel`, to _create_ a new
        [`LogarithmicUnit`][measured.LogarithmicUnit] based on a reference value.  In
        the example below, the reference value is 1 Watt:

        >>> from measured import Decibel
        >>> from measured.si import Watt
        >>> dBW = Decibel[1 * Watt]

        The `dBW` unit may now be used to produces [`Levels`][measured.Level] that may
        be used in operations with Quantities of the same dimension (`Power`, in this
        case):

        >>> assert 100 * Watt == 20 * dBW
    """

    _known: ClassVar[Dict[Tuple[float, Prefix], "Logarithm"]] = {}
    _initialized: bool

    base: float
    prefix: Prefix
    name: Optional[str]
    symbol: Optional[str]

    def __new__(
        cls,
        base: float,
        prefix: Prefix = Prefix(0, 0),
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> "Logarithm":
        key = (base, prefix)
        if key in cls._known:
            return cls._known[key]

        self = super().__new__(cls)
        self._initialized = False
        cls._known[key] = self
        return self

    def __init__(
        self,
        base: float,
        prefix: Prefix = Prefix(0, 0),
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> None:
        if self._initialized:
            return

        self.base = base
        self.prefix = prefix
        self.name = name
        self.symbol = symbol
        self._initialized = True

    def alias(self, name: str, symbol: str) -> "Logarithm":
        """Gives a name and symbol to this Logarithm"""
        self.name = name
        self.symbol = symbol
        return self

    __repr__ = formatting.logarithm_repr
    __str__ = formatting.logarithm_str
    _repr_pretty_ = formatting.logarithm_pretty
    _repr_html_ = formatting.mathml(formatting.logarithm_mathml)

    def __mul__(self, other: Prefix) -> "Logarithm":
        if isinstance(other, Prefix):
            return Logarithm(self.base, prefix=self.prefix * other)

        return NotImplemented

    __rmul__ = __mul__

    def __getitem__(self, reference: Quantity) -> "LogarithmicUnit":
        return LogarithmicUnit(self, reference)

alias(name: str, symbol: str) -> Logarithm

Gives a name and symbol to this Logarithm

Source code in src/measured/__init__.py
1649
1650
1651
1652
1653
def alias(self, name: str, symbol: str) -> "Logarithm":
    """Gives a name and symbol to this Logarithm"""
    self.name = name
    self.symbol = symbol
    return self

measured.LogarithmicUnit

A LogarithmicUnit represents a ratio of a measured Quantity to a reference Quantity on a logarithmic scale.

Examples:

You may construct a `LogarithmicUnit` by applying the reference quantity as a
"suffix" (using Python's indexing operator `[]`) to the
[`Logarithm`][measured.Logarithm] in question:

>>> from measured import Decibel
>>> from measured.si import Watt, Milli
>>> dBW = Decibel[1 * Watt]
>>> dBm = Decibel[1 * Milli * Watt]
>>> assert dBm is not dBW

`LogarithmicUnits` create [`Levels`][measured.Level] through multiplication:

>>> level = 20 * dBW

A [`Level`][measured.Level] is analogous to a [`Quantity`][measured.Quantity]:

>>> level.magnitude
20
>>> level.unit is dBW
True
>>> 100 * Watt == level
True
Source code in src/measured/__init__.py
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
class LogarithmicUnit:
    """A `LogarithmicUnit` represents a _ratio_ of a measured
    [`Quantity`][measured.Quantity] to a reference [`Quantity`][measured.Quantity] on a
    logarithmic scale.

    Examples:

        You may construct a `LogarithmicUnit` by applying the reference quantity as a
        "suffix" (using Python's indexing operator `[]`) to the
        [`Logarithm`][measured.Logarithm] in question:

        >>> from measured import Decibel
        >>> from measured.si import Watt, Milli
        >>> dBW = Decibel[1 * Watt]
        >>> dBm = Decibel[1 * Milli * Watt]
        >>> assert dBm is not dBW

        `LogarithmicUnits` create [`Levels`][measured.Level] through multiplication:

        >>> level = 20 * dBW

        A [`Level`][measured.Level] is analogous to a [`Quantity`][measured.Quantity]:

        >>> level.magnitude
        20
        >>> level.unit is dBW
        True
        >>> 100 * Watt == level
        True
    """

    _known: ClassVar[Dict[Tuple[Logarithm, Quantity], "LogarithmicUnit"]] = {}
    _initialized: bool

    logarithm: Logarithm
    reference: Quantity
    name: Optional[str]
    symbol: Optional[str]

    def __new__(
        cls,
        logarithm: Logarithm,
        reference: Quantity,
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> "LogarithmicUnit":
        key = (logarithm, reference)
        if key in cls._known:
            return cls._known[key]

        self = super().__new__(cls)
        self._initialized = False
        cls._known[key] = self
        return self

    def __init__(
        self,
        logarithm: Logarithm,
        reference: Quantity,
        name: Optional[str] = None,
        symbol: Optional[str] = None,
    ) -> None:
        if self._initialized:
            return

        self.logarithm = logarithm
        self.reference = reference.unprefixed()
        self.name = name
        self.symbol = symbol
        self._initialized = True

    def alias(self, name: str, symbol: str) -> "LogarithmicUnit":
        """Gives a name and symbol to this LogarithmicUnit"""
        assert not self.name and not self.symbol
        self.name = name
        self.symbol = symbol
        return self

    @property
    def power_ratio(self) -> int:
        """Indicates whether the reference quantity is a power (1) or root-power (2)
        quantity"""
        if self.reference.unit.dimension in ROOT_POWER_DIMENSIONS:
            return 2
        return 1

    __repr__ = formatting.logarithmic_unit_repr
    __str__ = formatting.logarithmic_unit_str
    _repr_pretty_ = formatting.logarithmic_unit_pretty
    _repr_html_ = formatting.mathml(formatting.logarithmic_unit_mathml)

    def __mul__(self, magnitude: Numeric) -> "Level":
        return Level(magnitude, self)

    __rmul__ = __mul__

    def level(self, quantity: Quantity) -> "Level":
        base = self.logarithm.base
        prefix = self.logarithm.prefix
        power_ratio = self.power_ratio
        ratio = quantity.in_unit(self.reference.unit) / self.reference
        inverted = _mul(1 / prefix.quantify(), math.log(ratio.magnitude, base))
        magnitude = power_ratio * inverted
        return Level(magnitude, self)

power_ratio: int property

Indicates whether the reference quantity is a power (1) or root-power (2) quantity

alias(name: str, symbol: str) -> LogarithmicUnit

Gives a name and symbol to this LogarithmicUnit

Source code in src/measured/__init__.py
1743
1744
1745
1746
1747
1748
def alias(self, name: str, symbol: str) -> "LogarithmicUnit":
    """Gives a name and symbol to this LogarithmicUnit"""
    assert not self.name and not self.symbol
    self.name = name
    self.symbol = symbol
    return self

measured.Level

A Level is analogous to a Quantity, but represents a ratio of a quantity to a reference quantity. The reference quantity is captured by the LogarithmicUnit.

Attributes:

magnitude (int | float): the quantity

unit (LogarithmicUnit): the [`LogarithmicUnit`][measured.LogarithmicUnit]
Source code in src/measured/__init__.py
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
class Level:
    """A `Level` is analogous to a [`Quantity`][measured.Quantity], but represents a
    ratio of a quantity to a reference quantity.  The reference quantity is captured by
    the [`LogarithmicUnit`][measured.LogarithmicUnit].

    Attributes:

        magnitude (int | float): the quantity

        unit (LogarithmicUnit): the [`LogarithmicUnit`][measured.LogarithmicUnit]
    """

    magnitude: Numeric
    unit: LogarithmicUnit

    def __init__(self, magnitude: Numeric, unit: LogarithmicUnit) -> None:
        self.magnitude = magnitude
        self.unit = unit

    __repr__ = formatting.level_repr
    __str__ = formatting.level_str
    _repr_pretty_ = formatting.level_pretty
    _repr_html_ = formatting.mathml(formatting.level_mathml)

    def quantify(self) -> Quantity:
        """Converts this Level into a Quantity of the reference unit"""
        base = self.unit.logarithm.base
        prefix = self.unit.logarithm.prefix
        power_ratio = self.unit.power_ratio
        reference = self.unit.reference
        exponent = _mul(self.magnitude, prefix.quantify())
        magnitude = _pow(base, (exponent / power_ratio))
        return magnitude * reference

    def __add__(self, other: "Level") -> "Level":
        if isinstance(other, Level):
            if other.unit != self.unit:
                return NotImplemented

            base = self.unit.logarithm.base
            prefix = self.unit.logarithm.prefix
            power_ratio = self.unit.power_ratio

            left_exponent = _mul(self.magnitude, prefix.quantify())
            right_exponent = _mul(other.magnitude, prefix.quantify())

            left_magnitude = _pow(base, (left_exponent * power_ratio))
            right_magnitude = _pow(base, (right_exponent * power_ratio))

            magnitude = base * math.log(_add(left_magnitude, right_magnitude), base)

            return Level(magnitude, self.unit)

        return NotImplemented

    def __sub__(self, other: "Level") -> "Level":
        if isinstance(other, Level):
            if other.unit != self.unit:
                return NotImplemented

            base = self.unit.logarithm.base
            prefix = self.unit.logarithm.prefix
            power_ratio = self.unit.power_ratio

            left_exponent = _mul(self.magnitude, prefix.quantify())
            right_exponent = _mul(other.magnitude, prefix.quantify())

            left_magnitude = _pow(base, (left_exponent * power_ratio))
            right_magnitude = _pow(base, (right_exponent * power_ratio))

            magnitude = base * math.log(_sub(left_magnitude, right_magnitude), base)

            return Level(magnitude, self.unit)

        return NotImplemented

    def __mul__(self, other: Numeric) -> "Level":
        if isinstance(other, NUMERIC_CLASSES):
            return Level(_add(self.magnitude, other), self.unit)

        return NotImplemented

    def __truediv__(self, other: Numeric) -> "Level":
        if isinstance(other, NUMERIC_CLASSES):
            return Level(_sub(self.magnitude, other), self.unit)

        return NotImplemented

    def __eq__(self, other: object) -> bool:
        if isinstance(other, Level):
            return self.quantify() == other.quantify()

        if isinstance(other, Quantity):
            return self.quantify() == other

        return NotImplemented

quantify() -> Quantity

Converts this Level into a Quantity of the reference unit

Source code in src/measured/__init__.py
1802
1803
1804
1805
1806
1807
1808
1809
1810
def quantify(self) -> Quantity:
    """Converts this Level into a Quantity of the reference unit"""
    base = self.unit.logarithm.base
    prefix = self.unit.logarithm.prefix
    power_ratio = self.unit.power_ratio
    reference = self.unit.reference
    exponent = _mul(self.magnitude, prefix.quantify())
    magnitude = _pow(base, (exponent / power_ratio))
    return magnitude * reference

measured.Measurement

Measurement represents an uncertain measurement of some Quantity, and will propagate that uncertainty through arithmetic operations with Quantities and other Measurements.

Attributes:

measurand (Quantity): the measured value

uncertainty (Quantity | Numeric): the uncertainty of the measurement

Examples:

All of the arithmetic operations are supported with Quantities and Measurements:

>>> from measured import Measurement
>>> from measured.si import Meter, Second
>>> length = Measurement(2 * Meter, uncertainty=0.1)
>>> width = Measurement(3 * Meter, uncertainty=0.2)
>>> depth = 4 * Meter

The uncertainty of the measurements propagate through arithmetic operations
assuming that they are Gaussian, normally-distributed errors.

>>> area = length * width
>>> assert isinstance(area, Measurement)
>>> assert area.measurand == 6 * Meter**2
>>> assert area.uncertainty == 0.5 * Meter**2

Quantities are treated as if they have an uncertainty of 0:

>>> volume = area * depth
>>> assert isinstance(volume, Measurement)
>>> assert volume.measurand == 24 * Meter**3
>>> assert volume.uncertainty == 2.0 * Meter**3

When formatting a [`Measurement`][measured.Measurement], you can specify up to
three separate format specifiers, separated by a `:`.  The first specifier
controls how the uncertainty will be formatted.

>>> speed = Measurement(10.0 * Meter / Second, 0.1)

Uncertainties can be formatted as a ± interval using either the "±" or "+"
specifier (the default)

>>> f"{speed}"
'10.0±0.1 m⋅s⁻¹'

They can also be formatted as percentages using the "%" specifier:

>>> f"{speed:%}"
'10.0±1.00% m⋅s⁻¹'

In any of those cases, the significant digits of the uncertainty can also be
specified:

>>> f"{speed:%.4f}"
'10.0±1.0000% m⋅s⁻¹'
>>> f"{speed:±.4f}"
'10.0±0.1000 m⋅s⁻¹'
>>> f"{speed:+.4f}"
'10.0±0.1000 m⋅s⁻¹'

The second and third specifiers are used to format the measurand and follow the
same conventions as with [`Quantity`][measured.Quantity].

>>> f"{speed:%.4f:.3f:/}"
'10.000±1.0000% m/s'
Source code in src/measured/__init__.py
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
class Measurement:
    """Measurement represents an uncertain measurement of some Quantity, and will
    propagate that uncertainty through arithmetic operations with Quantities and other
    Measurements.

    Attributes:

        measurand (Quantity): the measured value

        uncertainty (Quantity | Numeric): the uncertainty of the measurement

    Examples:

        All of the arithmetic operations are supported with Quantities and Measurements:

        >>> from measured import Measurement
        >>> from measured.si import Meter, Second
        >>> length = Measurement(2 * Meter, uncertainty=0.1)
        >>> width = Measurement(3 * Meter, uncertainty=0.2)
        >>> depth = 4 * Meter

        The uncertainty of the measurements propagate through arithmetic operations
        assuming that they are Gaussian, normally-distributed errors.

        >>> area = length * width
        >>> assert isinstance(area, Measurement)
        >>> assert area.measurand == 6 * Meter**2
        >>> assert area.uncertainty == 0.5 * Meter**2

        Quantities are treated as if they have an uncertainty of 0:

        >>> volume = area * depth
        >>> assert isinstance(volume, Measurement)
        >>> assert volume.measurand == 24 * Meter**3
        >>> assert volume.uncertainty == 2.0 * Meter**3

        When formatting a [`Measurement`][measured.Measurement], you can specify up to
        three separate format specifiers, separated by a `:`.  The first specifier
        controls how the uncertainty will be formatted.

        >>> speed = Measurement(10.0 * Meter / Second, 0.1)

        Uncertainties can be formatted as a ± interval using either the "±" or "+"
        specifier (the default)

        >>> f"{speed}"
        '10.0±0.1 m⋅s⁻¹'

        They can also be formatted as percentages using the "%" specifier:

        >>> f"{speed:%}"
        '10.0±1.00% m⋅s⁻¹'

        In any of those cases, the significant digits of the uncertainty can also be
        specified:

        >>> f"{speed:%.4f}"
        '10.0±1.0000% m⋅s⁻¹'
        >>> f"{speed:±.4f}"
        '10.0±0.1000 m⋅s⁻¹'
        >>> f"{speed:+.4f}"
        '10.0±0.1000 m⋅s⁻¹'

        The second and third specifiers are used to format the measurand and follow the
        same conventions as with [`Quantity`][measured.Quantity].

        >>> f"{speed:%.4f:.3f:/}"
        '10.000±1.0000% m/s'
    """

    measurand: Quantity
    uncertainty: Quantity

    def __init__(
        self, measurand: Quantity, uncertainty: Union[Numeric, Quantity]
    ) -> None:
        self.measurand = measurand
        if not isinstance(uncertainty, Quantity):
            uncertainty = Quantity(uncertainty, measurand.unit)
        assert uncertainty.unit is measurand.unit
        self.uncertainty = abs(uncertainty)

    @property
    def uncertainty_ratio(self) -> float:
        """The uncertainty, expressed as a fraction of the magnitude of the measurand"""
        return float(self.uncertainty.magnitude) / float(self.measurand.magnitude)

    @property
    def uncertainty_percent(self) -> float:
        """The uncertainty, expressed as a percent of the magnitude of the measurand"""
        return float(self.uncertainty_ratio) * 100

    __repr__ = formatting.measurement_repr
    __str__ = formatting.measurement_str
    __format__ = formatting.measurement_format
    _repr_pretty_ = formatting.measurement_pretty
    _repr_html_ = formatting.mathml(formatting.measurement_mathml)

    def __eq__(self, other: object) -> bool:
        if isinstance(other, Quantity):
            other = Measurement(other, 0)
        if isinstance(other, Level):
            other = Measurement(other.quantify(), 0)

        if not isinstance(other, Measurement):
            return False

        if self.measurand.unit.dimension is not other.measurand.unit.dimension:
            return False

        self_lower = self.measurand - self.uncertainty
        other_lower = other.measurand - other.uncertainty
        self_upper = self.measurand + self.uncertainty
        other_upper = other.measurand + other.uncertainty

        try:
            overlaps_lower = self_lower <= other_lower <= self_upper
            overlaps_upper = self_lower <= other_upper <= self_upper
        except TypeError:
            return False

        return overlaps_lower or overlaps_upper

    def __lt__(self, other: object) -> bool:
        if isinstance(other, Quantity):
            other = Measurement(other, 0)
        if isinstance(other, Level):
            other = Measurement(other.quantify(), 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        self_lower = self.measurand - self.uncertainty
        other_lower = other.measurand - other.uncertainty
        return self_lower < other_lower

    def __le__(self, other: object) -> bool:
        if isinstance(other, Quantity):
            other = Measurement(other, 0)
        if isinstance(other, Level):
            other = Measurement(other.quantify(), 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        self_lower = self.measurand - self.uncertainty
        other_lower = other.measurand - other.uncertainty
        return self_lower <= other_lower

    def __gt__(self, other: object) -> bool:
        if isinstance(other, Quantity):
            other = Measurement(other, 0)
        if isinstance(other, Level):
            other = Measurement(other.quantify(), 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        self_upper = self.measurand + self.uncertainty
        other_upper = other.measurand + other.uncertainty
        return self_upper > other_upper

    def __ge__(self, other: object) -> bool:
        if isinstance(other, Quantity):
            other = Measurement(other, 0)
        if isinstance(other, Level):
            other = Measurement(other.quantify(), 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        self_upper = self.measurand + self.uncertainty
        other_upper = other.measurand + other.uncertainty
        return self_upper >= other_upper

    def __add__(self, other: Union["Measurement", Quantity]) -> "Measurement":
        if isinstance(other, Quantity):
            other = Measurement(other, 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        measurand = self.measurand + other.measurand
        uncertainty = (self.uncertainty**2 + other.uncertainty**2).root(2)
        return Measurement(measurand, uncertainty)

    __radd__ = __add__

    def __sub__(self, other: Union["Measurement", Quantity]) -> "Measurement":
        if isinstance(other, Quantity):
            other = Measurement(other, 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        measurand = self.measurand - other.measurand
        uncertainty = (self.uncertainty**2 + other.uncertainty**2).root(2)
        return Measurement(measurand, uncertainty)

    def __rsub__(self, other: Union["Measurement", Quantity]) -> "Measurement":
        if isinstance(other, Quantity):
            other = Measurement(other, 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        return other - self

    def __mul__(self, other: Union["Measurement", Quantity]) -> "Measurement":
        if isinstance(other, Quantity):
            other = Measurement(other, 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        measurand = self.measurand * other.measurand
        uncertainty = self._join_uncertainties(measurand, other)
        return Measurement(measurand, uncertainty)

    __rmul__ = __mul__

    def __truediv__(self, other: Union["Measurement", Quantity]) -> "Measurement":
        if isinstance(other, Quantity):
            other = Measurement(other, 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        measurand = self.measurand / other.measurand
        uncertainty = self._join_uncertainties(measurand, other)
        return Measurement(measurand, uncertainty)

    def _join_uncertainties(self, measurand: Quantity, other: "Measurement") -> float:
        return math.sqrt(
            _mul(
                _pow(measurand.magnitude, 2),
                (
                    _add(
                        _div(
                            _pow(self.uncertainty.magnitude, 2),
                            _pow(self.measurand.magnitude, 2),
                        ),
                        _div(
                            _pow(other.uncertainty.magnitude, 2),
                            _pow(other.measurand.magnitude, 2),
                        ),
                    )
                ),
            )
        )

    def __rtruediv__(self, other: Union["Measurement", Quantity]) -> "Measurement":
        if isinstance(other, Quantity):
            other = Measurement(other, 0)

        if not isinstance(other, Measurement):
            return NotImplemented

        return other / self

    def __pow__(self, exponent: int) -> "Measurement":
        if not isinstance(exponent, int):
            return NotImplemented

        measurand = self.measurand**exponent
        uncertainty = math.sqrt(
            _pow(
                _mul(
                    exponent,
                    _mul(
                        _pow(self.measurand.magnitude, 2),
                        self.uncertainty.magnitude,
                    ),
                ),
                2,
            )
        )
        return Measurement(measurand, uncertainty)

uncertainty_percent: float property

The uncertainty, expressed as a percent of the magnitude of the measurand

uncertainty_ratio: float property

The uncertainty, expressed as a fraction of the magnitude of the measurand

measured.approximately(quantity: Union[Quantity, Level], within: float = 1e-07) -> Measurement

An approximation used mostly for making test assertions. This special type of measurement uses a relative value for uncertainty and overrides its repr to be more useful for test assertions.

Parameters:

quantity (Quantity): the quantity to approximate

within (float): An uncertainty of the measurement relative to the quantity

Examples:

>>> from measured import approximately
>>> from measured.si import Meter
>>> assert 5.2 * Meter == approximately(5 * Meter, 0.3)
>>> assert 5.2 * Meter < approximately(6 * Meter, 0.5)
Source code in src/measured/__init__.py
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
def approximately(
    quantity: Union[Quantity, Level], within: float = 1e-7
) -> Measurement:
    """
    An approximation used mostly for making test assertions.  This special type of
    measurement uses a relative value for uncertainty and overrides its repr to be
    more useful for test assertions.

    Parameters:

        quantity (Quantity): the quantity to approximate

        within (float): An uncertainty of the measurement relative to the quantity

    Examples:

        >>> from measured import approximately
        >>> from measured.si import Meter
        >>> assert 5.2 * Meter == approximately(5 * Meter, 0.3)
        >>> assert 5.2 * Meter < approximately(6 * Meter, 0.5)
    """
    if isinstance(quantity, Level):
        quantity = quantity.quantify()

    uncertainty = _mul(quantity.magnitude or 1.0, within)
    return Measurement(quantity, uncertainty)