Beispiel #1
0
class Energy(AbstractMeasure):
    joule = MetricUnit("1", ["J", "Joule"], ["J"], ["joule"])
    calorie = MetricUnit(
        "4184.0", ["c", "cal", "Cal", "Calorie", "C"], ["cal"], ["calorie"]
    )
    electronvolt = MetricUnit(
        "1.602177e-19",
        ["eV", "electron-volt", "electron volt"],
        ["eV"],
        ["electronvolt"],
    )
    tonne_tnt = Unit("4184000000")
class ElectricPower(AbstractMeasure):
    """
    Electric power is defined as :class:`Voltage` multiplied by :class:`Current`.

    This is why you can divide :class:`Current` to get the :class:`Voltage`
    by :class:`Voltage` to get the :class:`Current`
    or by :class:`Current` to get the :class:`Voltage`:

        >>> from measurement import measures
        >>> measures.ElectricPower('24 W') / measures.Voltage('12 V')
        Current(ampere="2")
        >>> measures.ElectricPower('24 W') / measures.Current('4 A')
        Voltage(volt="6")


    Analog to this, you can also multiply both :class:`Current`
    and :class:`Voltage` to get :class:`Current` to get the :class:`Voltage`:

        >>> from measurement import measures
        >>> measures.Current('2 A') * measures.Voltage('12 V')
        ElectricPower(watt="24")

    """

    watt = MetricUnit("1", ["W", "VA", "Watt", "Voltampere"], ["W", "VA"],
                      ["watt", "voltampere"])

    def __truediv__(self, other):
        if isinstance(other, Current):
            return Voltage(volt=self.si_value / other.si_value)

        if isinstance(other, Voltage):
            return Current(ampere=self.si_value / other.si_value)

        return super().__truediv__(other)
class Voltage(AbstractMeasure):
    volt = MetricUnit("1", ["V", "Volt"], ["V"], ["volt"])

    def __mul__(self, other):
        if isinstance(other, Current):
            return ElectricPower(self.si_value * other.si_value, "W")
        return super().__mul__(other)
class Current(AbstractMeasure):
    ampere = MetricUnit("1", ["A", "amp", "Ampere"], ["A"], ["ampere", "amp"])

    def __mul__(self, other):
        if isinstance(other, Voltage):
            return ElectricPower(self.si_value * other.si_value, "W")
        return super().__mul__(other)
class Pressure(AbstractMeasure):
    pascal = MetricUnit("1", ["pa"], ["pa"], ["pascal"])
    bar = Unit("100000")
    atmosphere = Unit("101325", ["atm"])
    technical_atmosphere = Unit("98066.5", ["at"])
    torr = Unit("133.322")
    psi = Unit("6894.757293168", ["pounds per square inch"])
class Mass(AbstractMeasure):
    gram = MetricUnit("1", ["g", "Gram"], ["g"], ["gram"])
    tonne = Unit("1000000", ["t", "metric ton", "metric tonne"])
    ounce = Unit("28.34952", ["oz"])
    pound = Unit("453.59237", ["lb"])
    stone = Unit("6350.293")
    short_ton = Unit("907185.0", ["ton"])
    long_ton = Unit("1016000.0")
Beispiel #7
0
class Frequency(AbstractMeasure):
    hertz = MetricUnit("1", ["Hz", "Hertz"], ["Hz"], ["hertz"])
    rpm = Unit(
        decimal.Decimal("1.0") / decimal.Decimal("60"), ["RPM", "bpm", "BPM"])

    def __mul__(self, other):
        if isinstance(other, Time):
            return self.si_value * other.si_value
        return super().__mul__(other)
    def test_get_symbols(self):
        metre = MetricUnit("1", ["m", "meter"], ["m"], ["metre", "meter"])
        metre.name = "metre"
        symbols = list(metre.get_symbols())

        assert ("metre", Unit("1")) in symbols
        assert ("m", Unit("1")) in symbols
        assert ("meter", Unit("1")) in symbols

        assert ("km", Unit("1E+3")) in symbols
        assert ("μm", Unit("1E-6")) in symbols

        assert ("Kilometre", Unit("1E+3")) in symbols
        assert ("kilometre", Unit("1E+3")) in symbols

        assert ("Kilometer", Unit("1E+3")) in symbols
        assert ("kilometer", Unit("1E+3")) in symbols

        assert ("nanometer", Unit("1E-9")) in symbols
Beispiel #9
0
class Time(AbstractMeasure):
    """
    Time measurements (generally for multidimensional measures).

    Please do not use this for handling durations of time unrelated to
    measure classes -- python's built-in datetime module has much better
    functionality for handling intervals of time than this class provides.
    """

    second = MetricUnit("1", ["s", "sec", "seconds"], ["s"], ["sec"])
    minute = Unit("60", ["min", "minutes"])
    hour = Unit("3600", ["hr", "h", "hours"])
    day = Unit("86400", ["d", "days"])
    julian_year = MetricUnit(
        "31557600",
        ["year", "a", "aj", "years", "annum", "Julian year"],
        ["a"],
        ["annum"],
    )
class Inductance(AbstractMeasure):
    henry = MetricUnit("1", ["H", "Henry"], ["H"], ["henry"])
class Resistance(AbstractMeasure):
    ohm = MetricUnit("1", ["Ohm", "Ω"], ["Ω"], ["ohm"])
class Capacitance(AbstractMeasure):
    farad = MetricUnit("1", ["F", "Farad"], ["F"], ["farad"])
Beispiel #13
0
class Distance(AbstractMeasure):
    """
    Distance is a factor for both :class:`Area` and :class:`Volume`.

    If you multiply a :class:`Distance` with another :class:`Distance`,
    you will get a :class:`Area`:

        >>> from measurement import measures
        >>> measures.Distance('2 m') * measures.Distance('3 m')
        Area(metre²="6")

    If you multiply a :class:`Distance` with a :class:`Area` or two
    :class:`Distances<Distance>`, you will get a :class:`Volume`:

        >>> from measurement import measures
        >>> measures.Distance('2 m') * measures.Area('6 m²')
        Volume(metre³="12")

    You can also build the second and third power of a :class:`Distance`,
    to get a :class:`Area` or :class:`Volume`.

        >>> from measurement import measures
        >>> measures.Distance('2 m') ** 2
        Area(metre²="4")
        >>> measures.Distance('2 m') ** 3
        Volume(metre³="8")

    """

    metre = MetricUnit("1", ["m", "meter", "Meter", "Metre"], ["m"], ["metre", "meter"])
    parsec = MetricUnit("3.0857E+16", ["Parsec", "pc"], ["pc"], ["parsec"])
    astronomical_unit = MetricUnit(
        "1.495978707E+11", ["au", "ua", "AU"], ["au", "ua", "AU"]
    )
    foot = Unit("0.3048", ["ft", "feet", "Foot (International)"])
    inch = Unit("0.0254", ["in", "inches"])
    mile = Unit("1609.344", ["mi"])
    chain = Unit("20.1168")
    chain_benoit = Unit("20.116782", ["Chain (Benoit)"])
    chain_sears = Unit("20.1167645", ["Chain (Sears)"])
    british_chain_benoit = Unit("20.1167824944", ["British chain (Benoit 1895 B)"])
    british_chain_sears = Unit("20.1167651216", ["British chain (Sears 1922)"])
    british_chain_sears_truncated = Unit(
        "20.116756", ["British chain (Sears 1922 truncated)"]
    )
    british_foot = Unit(
        "0.304799471539", ["british_ft", "British foot", "British foot (Sears 1922)"]
    )
    british_yard = Unit(
        "0.914398414616", ["british_yd", "British yard", "British yard (Sears 1922)"]
    )
    clarke_ft = Unit("0.3047972654", ["Clarke's Foot"])
    clarke_link = Unit("0.201166195164", ["Clarke's link"])
    fathom = Unit("1.8288")
    german_meter = Unit("1.0000135965", ["german_m", "German legal metre"])
    gold_coast_foot = Unit("0.304799710181508", ["gold_coast_ft", "Gold Coast foot"])
    indian_yard = Unit("0.914398530744", ["indian_yd", "Indian yard", "Yard (Indian)"])
    link = Unit("0.201168", ["Link"])
    link_benoit = Unit("0.20116782", ["Link (Benoit)"])
    link_sears = Unit("0.20116765", ["Link (Sears)"])
    nautical_mile = Unit("1852", ["Nautical Mile", "NM", "nmi"])
    nautical_mile_uk = Unit("1853.184", ["nm_uk", "Nautical Mile (UK)"])
    rod = Unit("5.0292")
    sears_yard = Unit("0.91439841", ["sears_yd", "Yard (Sears"])
    survey_foot = Unit("0.304800609601", ["survey_ft", "US survey foot", "U.S. Foot"])
    yard = Unit("0.9144", ["yd"])
    point = Unit(decimal.Decimal("25.4e-3") / 72, ["pt"])

    def __mul__(self, other):
        if isinstance(other, Distance):
            return Area(sq_metre=self.si_value * other.si_value)

        if isinstance(other, Area):
            return Volume(cubic_metre=self.si_value * other.si_value)

        return super().__mul__(other)

    def __pow__(self, power, modulo=None):
        if power == 2:
            return self * self
        if power == 3:
            return self * self * self
        return NotImplemented
Beispiel #14
0
class Volume(AbstractMeasure, metaclass=VolumeBase):
    """
    A volume is defined as :class:`Area` multipled by :class:`Distance`.

    This is why you can multiply three :class:`Distances<Distance>` to get
    a :class:`Volume`, multiply a :class:`Area` by a :class:`Distance`
    or divide a :class:`Volume` by both :class:`Distance` and :class:`Area`:

        >>> from measurement import measures
        >>> measures.Distance('2 m') * measures.Distance('3 m') * measures.Distance('4 m')
        Volume(metre³="24")
        >>> measures.Distance('2 m') * measures.Area('6 m²')
        Volume(metre³="12")
        >>> measures.Volume('12 m³') / measures.Area('6 m²')
        Distance(metre="2")
        >>> measures.Volume('12 m³') / measures.Distance('6 m')
        Area(metre²="2")

    """

    __factors__ = (Distance, Distance, Distance)

    litre = MetricUnit(
        "1e-3", ["liter", "L", "l", "ℓ"], ["L", "l", "ℓ"], ["litre", "liter"]
    )

    us_gallon = Unit(
        "3.785411784e-3", ["US gallon", "US gal", "US fluid gallon", "gallon (US)"]
    )
    us_fluid_ounce = Unit("29.57353e-6", ["US oz", "US fl oz"])
    us_fluid_ounce_food = Unit("30e-6", ["US fluid ounce (food nutrition labelling)"])
    us_liquid_quart = Unit("0.946352946e-3", ["US liquid quart"])
    us_liquid_pint = Unit("473.176473e-6")
    us_gill = Unit("118.29411825e-6")
    us_tablespoon = Unit("14.78676478125e-6", ["US tablespoon", "Us tbsp", "Us Tbsp"])
    us_tsp = Unit("4.92892159375e-6", ["US tsp"])
    us_fluid_darm = Unit("3.6966911953125e-6")

    us_dry_gallon = Unit(
        "4.40488377086e-3", ["US dry gallon", "corn gallon", "grain gallon"]
    )
    us_dry_quart = Unit("1.101220942715e-3")
    us_dry_pint = Unit("550.6104713575e-6")
    us_bushel = Unit("35.23907016688e-3", ["US bsh", "US bu"])

    cubic_inch = Unit("16.387064e-6", ["cu in"])
    cubic_foot = Unit("0.02832", ["cu ft"])

    imperial_gallon = Unit(
        "4.54609e-3", ["Imperial gallon", "Imperial gal", "gallon (Imperial)"]
    )
    imperial_fluid_ounce = Unit("28.41306e-6", ["Imperial fluid ounce", "imp fl oz"])
    imperial_quart = Unit("1.1365225e-6", ["Imperial quart"])
    imperial_pint = Unit("568.26125e-6", ["Imperial pint"])
    imperial_gill = Unit("142.0653125e-6", ["Imperial gill"])
    imperial_bushel = Unit("36.36872e-3", ["imp bsh", "imp bu"])
    imperial_fluid_darm = Unit("3.5516328125e-6", ["luid drachm"])

    au_tablespoon = Unit("20e-6", ["Australian tablespoon", "Australian tbsp"])
    au_teaspoon = Unit(
        "5e-6",
        [
            "US tsp (food nutrition labelling)",
            "metic teaspoon",
            "metric tsp",
            "Australian tsp",
        ],
    )

    oil_barrel = Unit("158.987294928e-3", ["oil bbl", "bbl"])

    acre_in = Unit(
        "102.790153128960",
        ["ac in", "ac-in", "acre inches", "acre inch", "acre-inches", "acre-inch"],
    )
    acre_ft = Unit(
        "1233.481837547520",
        ["ac ft", "ac-ft", "acre feet", "acre foot", "acre-feet", "acre-foot"],
    )

    @classmethod
    def _attr_to_unit(cls, name):
        if name[:6] in ["cubic_", "cubic "]:
            name = f"{name[6:]}³"
        return super()._attr_to_unit(name)

    def __truediv__(self, other):
        if isinstance(other, Distance):
            return Area(sq_metre=self.si_value / other.si_value)

        if isinstance(other, Area):
            return Distance(metre=self.si_value / other.si_value)

        return super().__truediv__(other)
 def test_get_symbols__unique_names(self):
     metre = MetricUnit("1", ["m", "meter"], ["m"], ["metre", "meter"])
     metre.name = "metre"
     symbols = list(metre.get_symbols())
     assert len([k for k, v in symbols]) == len({k for k, v in symbols})
class Radioactivity(AbstractMeasure):
    """Radioactivity measurements."""

    becquerel = MetricUnit("1", ["Bq"], ["Bq"])
    curie = MetricUnit("37000000000", ["Ci"], ["Ci"])
    rutherford = MetricUnit("1000000", ["Rd"], ["Rd"])