Skip to content

Commit 7fb804d

Browse files
convertible_unit docstring. remove registry preprocessor since it appears to have no effect
1 parent 1aeb71d commit 7fb804d

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/geophires_x/Units.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
# copyright, 2023, Malcolm I Ross
22
from enum import IntEnum, Enum, auto
3+
from typing import Any
34

45
import pint
56
import os
67

7-
88
_UREG = None
9+
10+
911
def get_unit_registry():
1012
global _UREG
1113
if _UREG is None:
1214
_UREG = pint.get_application_registry()
1315
_UREG.load_definitions(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'GEOPHIRES3_newunits.txt'))
14-
_UREG.preprocessors.append(lambda s: s.replace('%%', 'percent'))
1516

1617
return _UREG
1718

1819

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

23-
return u
29+
return unit
30+
2431

2532
class Units(IntEnum):
2633
"""All possible systems of measure"""
@@ -67,8 +74,8 @@ class Units(IntEnum):
6774
POWERPERUNITAREA = auto()
6875
HEATPERUNITVOLUME = auto()
6976
POWERPERUNITVOLUME = auto()
70-
DECAY_RATE=auto()
71-
INFLATION_RATE=auto()
77+
DECAY_RATE = auto()
78+
INFLATION_RATE = auto()
7279
DYNAMIC_VISCOSITY = auto()
7380

7481

@@ -166,6 +173,7 @@ class EnergyFrequencyUnit(str, Enum):
166173
MWhPERYEAR = "MWh/year"
167174
GWhPERYEAR = "GWh/year"
168175

176+
169177
class CurrencyUnit(str, Enum):
170178
"""Currency Units"""
171179
MDOLLARS = "MUSD"
@@ -335,41 +343,41 @@ class MassUnit(str, Enum):
335343
OZ = "ounce"
336344

337345

338-
class PopDensityUnit(str,Enum):
346+
class PopDensityUnit(str, Enum):
339347
"""Population Density Units"""
340348
perkm2 = "Population per square km"
341349

342350

343-
class HeatPerUnitAreaUnit(str,Enum):
351+
class HeatPerUnitAreaUnit(str, Enum):
344352
"""Population Density Units"""
345353
KJPERSQKM = "kJ/km**2"
346354

347355

348-
class PowerPerUnitAreaUnit(str,Enum):
356+
class PowerPerUnitAreaUnit(str, Enum):
349357
"""Population Density Units"""
350358
MWPERSQKM = "MW/km**2"
351359

352360

353-
class HeatPerUnitVolumeUnit(str,Enum):
361+
class HeatPerUnitVolumeUnit(str, Enum):
354362
"""Population Density Units"""
355363
KJPERCUBICKM = "kJ/km**3"
356364

357365

358-
class PowerPerUnitVolumeUnit(str,Enum):
366+
class PowerPerUnitVolumeUnit(str, Enum):
359367
"""Population Density Units"""
360368
MWPERCUBICKM = "MW/km**3"
361369

362370

363-
class Decay_RateUnit(str,Enum):
371+
class Decay_RateUnit(str, Enum):
364372
"""Decay rate Units"""
365373
PERCENTPERYEAR = "%/yr"
366374

367375

368-
class Inflation_RateUnit(str,Enum):
376+
class Inflation_RateUnit(str, Enum):
369377
"""Decay rate Units"""
370378
KPASCALPERYEAR = "kPa/yr"
371379

372380

373-
class Dynamic_ViscosityUnit(str,Enum):
381+
class Dynamic_ViscosityUnit(str, Enum):
374382
"""Dynamic Viscosity Units"""
375383
PASCALSEC = "PaSec"

0 commit comments

Comments
 (0)