Skip to content

Commit

Permalink
Update compatibility to include latest Pint (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroenlein authored Aug 15, 2024
1 parent bb12bd1 commit f3cd227
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gemd/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.8"
__version__ = "2.1.9"
13 changes: 9 additions & 4 deletions gemd/units/citrine_en.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Citrine customized units input file for Pint, based explicitly on the Pint defaults as
# included release 0.23 (https://github.com/hgrecco/pint/releases/tag/0.23). The Pint Authors hold
# copyright and are documented in https://github.com/hgrecco/pint/blob/0.23/AUTHORS.
# included release 0.24.3 (https://github.com/hgrecco/pint/releases/tag/0.24.3). The Pint Authors hold
# copyright and are documented in https://github.com/hgrecco/pint/blob/0.24.3/AUTHORS.
#
# The original copyright statement for the constants file reads:
#
Expand Down Expand Up @@ -116,7 +116,7 @@ pico- = 1e-12 = p-
nano- = 1e-9 = n-
# The micro (U+00B5) and Greek mu (U+03BC) are both valid prefixes,
# and they often use the same glyph.
micro- = 1e-6 = µ- = μ- = u-
micro- = 1e-6 = µ- = μ- = u- = mu- = mc-
milli- = 1e-3 = m-
centi- = 1e-2 = c-
deci- = 1e-1 = d-
Expand Down Expand Up @@ -269,7 +269,7 @@ hectare = 100 * are = ha = Hectare

# Volume
[volume] = [length] ** 3
liter = decimeter ** 3 = l = L = litre = Liter = Litre
liter = decimeter ** 3 = l = L = ℓ = litre = Liter = Litre
cubic_centimeter = centimeter ** 3 = cc = Cubic_Centimeter
lambda = microliter = λ = Lambda
stere = meter ** 3 = _ = Stere
Expand Down Expand Up @@ -543,12 +543,17 @@ buckingham = debye * angstrom = Buckingham
bohr_magneton = e * hbar / (2 * m_e) = µ_B = mu_B
nuclear_magneton = e * hbar / (2 * m_p) = µ_N = mu_N

# Refractive index
[refractive_index] = []
refractive_index_unit = [] = RIU

# Logaritmic Unit Definition
# Unit = scale; logbase; logfactor
# x_dB = [logfactor] * log( x_lin / [scale] ) / log( [logbase] )

# Logaritmic Units of dimensionless quantity: [ https://en.wikipedia.org/wiki/Level_(logarithmic_quantity) ]

decibelwatt = watt; logbase: 10; logfactor: 10 = dBW
decibelmilliwatt = 1e-3 watt; logbase: 10; logfactor: 10 = dBm
decibelmicrowatt = 1e-6 watt; logbase: 10; logfactor: 10 = dBu

Expand Down
20 changes: 18 additions & 2 deletions gemd/units/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,24 @@ def _format_clean(unit, registry, **options):
Responsibility for this piece of clean-up has been shifted to a custom class.
"""
from pint.formatting import _FORMATTERS
return _FORMATTERS["D"](unit, registry, **options)
try: # Informal route changed in 0.22
from pint.formatting import _FORMATTERS
formatter = _FORMATTERS["D"]
except ImportError: # pragma: no cover
from pint import Unit
formatter_obj = registry.formatter._formatters["D"]

def _surrogate_formatter(unit, registry, **options):
try:
parsed = Unit(unit)
return formatter_obj.format_unit(parsed)
except ValueError:
parsed = Unit(unit)
return formatter_obj.format_quantity(unit)

formatter = _surrogate_formatter

return formatter(unit, registry, **options)


@functools.lru_cache(maxsize=1024)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'tests.units': ['test_units.txt']
},
install_requires=[
"pint>=0.21,<0.24",
"pint>=0.21,<0.25,!=0.22,!=0.23", # pint 0.22,0.23 have a bad interaction w/ numpy >= 2
"deprecation>=2.1.0,<3",
"typing_extensions>=4.8,<5",
"importlib-resources>=5.3,<7"
Expand All @@ -52,7 +52,7 @@
"pandas>=2.0.3,<3"
],
"tests.entity.bounds": [
"numpy>=1.24.4,<2",
"numpy>=1.24.4,<3",
"pandas>=2.0.3,<3"
]
},
Expand Down
2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ flake8==7.0.0
flake8-docstrings==1.7.0
pytest==8.0.0
pytest-cov==4.1.0
pandas>=2.0.3,<2.2.0
pandas>=2.0.3,<2.2.0 # includes a compatible version of numpy
derp==0.1.1
10 changes: 9 additions & 1 deletion tests/units/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,12 @@ def test_deprecation():
megapascals = parse_units("MPa", return_unit=True)
with pytest.warns(DeprecatedWarning):
stringified = f"{megapascals:clean}"
assert megapascals == parse_units(stringified, return_unit=True)
assert megapascals == parse_units(stringified, return_unit=False)

from pint import Quantity
with pytest.warns(DeprecatedWarning):
assert f"{Quantity('5 MPa'):clean}" == f"5 {stringified}"

from pint import Unit
with pytest.warns(DeprecatedWarning):
assert f"{Unit('MPa'):clean}" == stringified

0 comments on commit f3cd227

Please sign in to comment.