Skip to content

Commit

Permalink
convertible_unit docstring. remove registry preprocessor since it app…
Browse files Browse the repository at this point in the history
…ears to have no effect
  • Loading branch information
softwareengineerprogrammer committed Oct 11, 2024
1 parent 1aeb71d commit 7fb804d
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/geophires_x/Units.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
# copyright, 2023, Malcolm I Ross
from enum import IntEnum, Enum, auto
from typing import Any

import pint
import os


_UREG = None


def get_unit_registry():
global _UREG
if _UREG is None:
_UREG = pint.get_application_registry()
_UREG.load_definitions(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'GEOPHIRES3_newunits.txt'))
_UREG.preprocessors.append(lambda s: s.replace('%%', 'percent'))

return _UREG


def convertible_unit(u:str) -> str:
if u == Units.PERCENT or u == PercentUnit.PERCENT or u == Units.PERCENT.value:
def convertible_unit(unit: Any) -> Any:
"""
pint can't handle '%' as a unit in python 3.8, so use this method when constructing quantities
:type unit: str|Enum
"""
if unit == Units.PERCENT or unit == PercentUnit.PERCENT or unit == Units.PERCENT.value:
return 'percent'

return u
return unit


class Units(IntEnum):
"""All possible systems of measure"""
Expand Down Expand Up @@ -67,8 +74,8 @@ class Units(IntEnum):
POWERPERUNITAREA = auto()
HEATPERUNITVOLUME = auto()
POWERPERUNITVOLUME = auto()
DECAY_RATE=auto()
INFLATION_RATE=auto()
DECAY_RATE = auto()
INFLATION_RATE = auto()
DYNAMIC_VISCOSITY = auto()


Expand Down Expand Up @@ -166,6 +173,7 @@ class EnergyFrequencyUnit(str, Enum):
MWhPERYEAR = "MWh/year"
GWhPERYEAR = "GWh/year"


class CurrencyUnit(str, Enum):
"""Currency Units"""
MDOLLARS = "MUSD"
Expand Down Expand Up @@ -335,41 +343,41 @@ class MassUnit(str, Enum):
OZ = "ounce"


class PopDensityUnit(str,Enum):
class PopDensityUnit(str, Enum):
"""Population Density Units"""
perkm2 = "Population per square km"


class HeatPerUnitAreaUnit(str,Enum):
class HeatPerUnitAreaUnit(str, Enum):
"""Population Density Units"""
KJPERSQKM = "kJ/km**2"


class PowerPerUnitAreaUnit(str,Enum):
class PowerPerUnitAreaUnit(str, Enum):
"""Population Density Units"""
MWPERSQKM = "MW/km**2"


class HeatPerUnitVolumeUnit(str,Enum):
class HeatPerUnitVolumeUnit(str, Enum):
"""Population Density Units"""
KJPERCUBICKM = "kJ/km**3"


class PowerPerUnitVolumeUnit(str,Enum):
class PowerPerUnitVolumeUnit(str, Enum):
"""Population Density Units"""
MWPERCUBICKM = "MW/km**3"


class Decay_RateUnit(str,Enum):
class Decay_RateUnit(str, Enum):
"""Decay rate Units"""
PERCENTPERYEAR = "%/yr"


class Inflation_RateUnit(str,Enum):
class Inflation_RateUnit(str, Enum):
"""Decay rate Units"""
KPASCALPERYEAR = "kPa/yr"


class Dynamic_ViscosityUnit(str,Enum):
class Dynamic_ViscosityUnit(str, Enum):
"""Dynamic Viscosity Units"""
PASCALSEC = "PaSec"

0 comments on commit 7fb804d

Please sign in to comment.