Module peprock.models.metric_prefix

Metric prefix model.

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

Examples

>>> MetricPrefix.mega.convert(5, to=MetricPrefix.kilo)
5000
>>> MetricPrefix.centi.convert(0.7, to=MetricPrefix.milli)
7.0

Classes

class MetricPrefix (*args, **kwds)
Expand source code
class MetricPrefix(enum.IntEnum):
    """MetricPrefix IntEnum with symbol and conversion support."""

    quetta = 30
    ronna = 27
    yotta = 24
    zetta = 21
    exa = 18
    peta = 15
    tera = 12
    giga = 9
    mega = 6
    kilo = 3
    hecto = 2
    deca = 1
    NONE = 0
    deci = -1
    centi = -2
    milli = -3
    micro = -6
    nano = -9
    pico = -12
    femto = -15
    atto = -18
    zepto = -21
    yocto = -24
    ronto = -27
    quecto = -30

    @classmethod
    def from_symbol(cls: type[MetricPrefix], symbol: str, /) -> MetricPrefix:
        """Return MetricPrefix by symbol."""
        return cls._by_symbol()[symbol]

    @functools.cached_property
    def symbol(self: MetricPrefix) -> str:
        """Metric prefix symbol, e.g. G for giga."""
        return self._symbols()[self]

    def __str__(self: MetricPrefix) -> str:
        """Return symbol."""
        return self.symbol

    @typing.overload
    def to(
        self: MetricPrefix,
        __other: MetricPrefix | int,
        /,
        *,
        number_type: type[int] = int,
    ) -> int | float: ...

    @typing.overload
    def to(
        self: MetricPrefix,
        __other: MetricPrefix | int,
        /,
        *,
        number_type: type[ComplexT],
    ) -> ComplexT: ...

    def to(
        self: MetricPrefix,
        __other: MetricPrefix | int,
        /,
        *,
        number_type: type[int | ComplexT] = int,
    ) -> int | ComplexT:
        """Calculate conversion factor between self and other."""
        return number_type(_BASE) ** (self - __other)

    @typing.overload
    def convert(
        self: MetricPrefix,
        __value: int,
        /,
        to: MetricPrefix = NONE,  # type: ignore[assignment]
    ) -> int | float: ...

    @typing.overload
    def convert(
        self: MetricPrefix,
        __value: ComplexT,
        /,
        to: MetricPrefix = NONE,  # type: ignore[assignment]
    ) -> ComplexT: ...

    def convert(
        self,
        __value,
        /,
        to=NONE,
    ):
        """Convert value from metric prefix self to to."""
        if self is to:
            return __value

        return __value * self.to(to, number_type=type(__value))

    @staticmethod
    @functools.cache
    def _symbols() -> types.MappingProxyType[MetricPrefix, str]:
        return types.MappingProxyType(
            {
                MetricPrefix.quetta: "Q",
                MetricPrefix.ronna: "R",
                MetricPrefix.yotta: "Y",
                MetricPrefix.zetta: "Z",
                MetricPrefix.exa: "E",
                MetricPrefix.peta: "P",
                MetricPrefix.tera: "T",
                MetricPrefix.giga: "G",
                MetricPrefix.mega: "M",
                MetricPrefix.kilo: "k",
                MetricPrefix.hecto: "h",
                MetricPrefix.deca: "da",
                MetricPrefix.NONE: "",
                MetricPrefix.deci: "d",
                MetricPrefix.centi: "c",
                MetricPrefix.milli: "m",
                MetricPrefix.micro: "μ",
                MetricPrefix.nano: "n",
                MetricPrefix.pico: "p",
                MetricPrefix.femto: "f",
                MetricPrefix.atto: "a",
                MetricPrefix.zepto: "z",
                MetricPrefix.yocto: "y",
                MetricPrefix.ronto: "r",
                MetricPrefix.quecto: "q",
            },
        )

    @classmethod
    @functools.cache
    def _by_symbol(
        cls: type[MetricPrefix],
    ) -> types.MappingProxyType[str, MetricPrefix]:
        return types.MappingProxyType(
            {symbol: metric_prefix for metric_prefix, symbol in cls._symbols().items()},
        )

MetricPrefix IntEnum with symbol and conversion support.

Ancestors

  • enum.IntEnum
  • builtins.int
  • enum.ReprEnum
  • enum.Enum

Class variables

var NONE

The type of the None singleton.

var atto

The type of the None singleton.

var centi

The type of the None singleton.

var deca

The type of the None singleton.

var deci

The type of the None singleton.

var exa

The type of the None singleton.

var femto

The type of the None singleton.

var giga

The type of the None singleton.

var hecto

The type of the None singleton.

var kilo

The type of the None singleton.

var mega

The type of the None singleton.

var micro

The type of the None singleton.

var milli

The type of the None singleton.

var nano

The type of the None singleton.

var peta

The type of the None singleton.

var pico

The type of the None singleton.

var quecto

The type of the None singleton.

var quetta

The type of the None singleton.

var ronna

The type of the None singleton.

var ronto

The type of the None singleton.

var tera

The type of the None singleton.

var yocto

The type of the None singleton.

var yotta

The type of the None singleton.

var zepto

The type of the None singleton.

var zetta

The type of the None singleton.

Static methods

def from_symbol(symbol: str, /) ‑> MetricPrefix

Return MetricPrefix by symbol.

Instance variables

var symbol : str
Expand source code
@functools.cached_property
def symbol(self: MetricPrefix) -> str:
    """Metric prefix symbol, e.g. G for giga."""
    return self._symbols()[self]

Metric prefix symbol, e.g. G for giga.

Methods

def convert(self, _MetricPrefix__value, /, to=0)
Expand source code
def convert(
    self,
    __value,
    /,
    to=NONE,
):
    """Convert value from metric prefix self to to."""
    if self is to:
        return __value

    return __value * self.to(to, number_type=type(__value))

Convert value from metric prefix self to to.

def to(self: MetricPrefix,
_MetricPrefix__other: MetricPrefix | int,
/,
*,
number_type: type[int | ComplexT] = builtins.int) ‑> int | ComplexT
Expand source code
def to(
    self: MetricPrefix,
    __other: MetricPrefix | int,
    /,
    *,
    number_type: type[int | ComplexT] = int,
) -> int | ComplexT:
    """Calculate conversion factor between self and other."""
    return number_type(_BASE) ** (self - __other)

Calculate conversion factor between self and other.