Module peprock.models.unit

Unit of measurement model.

See https://en.wikipedia.org/wiki/Unit_of_measurement

Examples

>>> Unit.ohm.symbol
'Ω'
>>> Unit("W")
<Unit.watt: 'W'>
Expand source code
"""Unit of measurement model.

See https://en.wikipedia.org/wiki/Unit_of_measurement

Examples
--------
>>> Unit.ohm.symbol
'Ω'

>>> Unit("W")
<Unit.watt: 'W'>

"""

from __future__ import annotations

import enum
import functools


class Unit(enum.Enum):
    """Unit Enum with symbol."""

    # metric units, see https://en.wikipedia.org/wiki/List_of_metric_units
    one = "1"  # unit of a quantity of dimension one
    second = "s"  # unit of time
    metre = "m"  # unit of length
    gram = "g"  # unit of mass (actually kilogram in SI)
    ampere = "A"  # unit of electric current
    kelvin = "K"  # unit of thermodynamic temperature
    mole = "mol"  # unit of amount of substance
    candela = "cd"  # unit of luminous intensity
    hertz = "Hz"  # equal to one reciprocal second
    radian = "rad"  # equal to one
    steradian = "sr"  # equal to one
    newton = "N"  # equal to one kilogram-metre per second squared
    pascal = "Pa"  # equal to one newton per square metre
    joule = "J"  # equal to one newton-metre
    watt = "W"  # equal to one joule per second
    coulomb = "C"  # equal to one ampere second
    volt = "V"  # equal to one joule per coulomb
    weber = "Wb"  # equal to one volt-second
    tesla = "T"  # equal to one weber per square metre
    farad = "F"  # equal to one coulomb per volt
    ohm = "Ω"  # equal to one volt per ampere
    siemens = "S"  # equal to one ampere per volt
    henry = "H"  # equal to one volt-second per ampere
    # degree Celsius (°C) is equal to one kelvin
    lumen = "lm"  # equal to one candela-steradian
    lux = "lx"  # equal to one lumen per square metre
    becquerel = "Bq"  # equal to one reciprocal second
    gray = "Gy"  # equal to one joule per kilogram
    sievert = "Sv"  # equal to one joule per kilogram
    katal = "kat"  # equal to one mole per second

    @functools.cached_property
    def symbol(self: Unit) -> str:
        """Get the unit symbol."""
        return self.value


__all__ = [
    "Unit",
]

Classes

class Unit (value, names=None, *, module=None, qualname=None, type=None, start=1)

Unit Enum with symbol.

Expand source code
class Unit(enum.Enum):
    """Unit Enum with symbol."""

    # metric units, see https://en.wikipedia.org/wiki/List_of_metric_units
    one = "1"  # unit of a quantity of dimension one
    second = "s"  # unit of time
    metre = "m"  # unit of length
    gram = "g"  # unit of mass (actually kilogram in SI)
    ampere = "A"  # unit of electric current
    kelvin = "K"  # unit of thermodynamic temperature
    mole = "mol"  # unit of amount of substance
    candela = "cd"  # unit of luminous intensity
    hertz = "Hz"  # equal to one reciprocal second
    radian = "rad"  # equal to one
    steradian = "sr"  # equal to one
    newton = "N"  # equal to one kilogram-metre per second squared
    pascal = "Pa"  # equal to one newton per square metre
    joule = "J"  # equal to one newton-metre
    watt = "W"  # equal to one joule per second
    coulomb = "C"  # equal to one ampere second
    volt = "V"  # equal to one joule per coulomb
    weber = "Wb"  # equal to one volt-second
    tesla = "T"  # equal to one weber per square metre
    farad = "F"  # equal to one coulomb per volt
    ohm = "Ω"  # equal to one volt per ampere
    siemens = "S"  # equal to one ampere per volt
    henry = "H"  # equal to one volt-second per ampere
    # degree Celsius (°C) is equal to one kelvin
    lumen = "lm"  # equal to one candela-steradian
    lux = "lx"  # equal to one lumen per square metre
    becquerel = "Bq"  # equal to one reciprocal second
    gray = "Gy"  # equal to one joule per kilogram
    sievert = "Sv"  # equal to one joule per kilogram
    katal = "kat"  # equal to one mole per second

    @functools.cached_property
    def symbol(self: Unit) -> str:
        """Get the unit symbol."""
        return self.value

Ancestors

  • enum.Enum

Class variables

var ampere
var becquerel
var candela
var coulomb
var farad
var gram
var gray
var henry
var hertz
var joule
var katal
var kelvin
var lumen
var lux
var metre
var mole
var newton
var ohm
var one
var pascal
var radian
var second
var siemens
var sievert
var steradian
var tesla
var volt
var watt
var weber

Instance variables

var symbol

Get the unit symbol.

Expand source code
def __get__(self, instance, owner=None):
    if instance is None:
        return self
    if self.attrname is None:
        raise TypeError(
            "Cannot use cached_property instance without calling __set_name__ on it.")
    try:
        cache = instance.__dict__
    except AttributeError:  # not all objects have __dict__ (e.g. class defines slots)
        msg = (
            f"No '__dict__' attribute on {type(instance).__name__!r} "
            f"instance to cache {self.attrname!r} property."
        )
        raise TypeError(msg) from None
    val = cache.get(self.attrname, _NOT_FOUND)
    if val is _NOT_FOUND:
        with self.lock:
            # check if another thread filled cache while we awaited lock
            val = cache.get(self.attrname, _NOT_FOUND)
            if val is _NOT_FOUND:
                val = self.func(instance)
                try:
                    cache[self.attrname] = val
                except TypeError:
                    msg = (
                        f"The '__dict__' attribute on {type(instance).__name__!r} instance "
                        f"does not support item assignment for caching {self.attrname!r} property."
                    )
                    raise TypeError(msg) from None
    return val