Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gemd/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.3.2"
__version__ = "2.4.0"
2 changes: 2 additions & 0 deletions gemd/units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
parse_units,
convert_units,
get_base_units,
get_compatible_units,
change_definitions_file,
UndefinedUnitError,
IncompatibleUnitsError,
Expand All @@ -13,6 +14,7 @@
"parse_units",
"convert_units",
"get_base_units",
"get_compatible_units",
"change_definitions_file",
"UndefinedUnitError",
"IncompatibleUnitsError",
Expand Down
17 changes: 17 additions & 0 deletions gemd/units/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,23 @@ def get_base_units(units: Union[str, UnitRegistry.Unit]) -> Tuple[UnitRegistry.U
return base_unit, float(ratio), offset


def get_compatible_units(units: Union[str, UnitRegistry.Unit]) -> List[str]:
"""Get all compatible units that match the dimensionality of the provided units.

Parameters
----------
units: str, Unit
The representation of the object we wish to compare

Returns
-------
List[str]
A list of the compatible units.

"""
return [f"{u}" for u in _REGISTRY.get_compatible_units(units)]


def change_definitions_file(filename: str = None):
"""Change which file is used for units definition.

Expand Down
14 changes: 14 additions & 0 deletions tests/units/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
change_definitions_file,
convert_units,
get_base_units,
get_compatible_units,
parse_units,
)

Expand Down Expand Up @@ -191,6 +192,19 @@ def test_get_base_units():
assert get_base_units("g / 25 mm") == (_REGISTRY("kg / m"), 0.04, 0)


def test_get_compatible_units():
"""Test that compatible units make sense."""
from gemd.units.impl import _REGISTRY
assert "kelvin" in get_compatible_units("degC")
assert "degree_Celsius" in get_compatible_units("degC")
assert "gram" in get_compatible_units("kg")
assert "meter" not in get_compatible_units("kg")
assert "meter" in get_compatible_units("km")
assert "meter" in get_compatible_units(_REGISTRY("km"))
assert "meter" in get_compatible_units("[length]")
assert "gram" not in get_compatible_units("km")


@contextmanager
def _change_units(filename):
try:
Expand Down
Loading