From f196e66e79de8a8016a877470e1bfc57af9b44c4 Mon Sep 17 00:00:00 2001 From: Neal Kruis Date: Fri, 27 Dec 2024 14:58:41 -0700 Subject: [PATCH] Add unit formatting. --- koozie/__init__.py | 3 ++- koozie/__main__.py | 3 ++- koozie/cli.py | 5 ++--- koozie/koozie.py | 5 +++++ test/test_koozie.py | 17 ++++++++++++++++- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/koozie/__init__.py b/koozie/__init__.py index 710830c..54b6bfb 100644 --- a/koozie/__init__.py +++ b/koozie/__init__.py @@ -1,2 +1,3 @@ """koozie public interface""" -from .koozie import fr_u, to_u, convert, get_dimensionality + +from .koozie import fr_u, to_u, convert, get_dimensionality, format_units diff --git a/koozie/__main__.py b/koozie/__main__.py index 7ea3096..b3434e9 100644 --- a/koozie/__main__.py +++ b/koozie/__main__.py @@ -1,5 +1,6 @@ """Main calling point for the command line interface""" + from koozie.cli import koozie_cli if __name__ == "__main__": - koozie_cli() # pylint: disable=no-value-for-parameter + koozie_cli() # pylint: disable=no-value-for-parameter diff --git a/koozie/cli.py b/koozie/cli.py index 6f8953a..e813fb0 100644 --- a/koozie/cli.py +++ b/koozie/cli.py @@ -1,4 +1,5 @@ """koozie command line interface""" + import sys import click import koozie @@ -32,9 +33,7 @@ def list_callback(context: click.Context, _: click.Parameter, value: str) -> Non context.exit() -@click.command( - context_settings={"help_option_names": ["-h", "--help"], "ignore_unknown_options": True} -) +@click.command(context_settings={"help_option_names": ["-h", "--help"], "ignore_unknown_options": True}) @click.version_option(None, "-v", "--version") @click.option( "-l", diff --git a/koozie/koozie.py b/koozie/koozie.py index 25303c8..eb78bb1 100644 --- a/koozie/koozie.py +++ b/koozie/koozie.py @@ -79,6 +79,11 @@ def get_dimensionality(units: str) -> pint.util.UnitsContainer: return unit_registry.Unit(units).dimensionality +def format_units(units: str) -> str: + """Format units for display.""" + return f"{unit_registry.Unit(units):~P}" + + def get_unit_list() -> OrderedDict: """Get list of valid units.""" unit_list: dict = {} diff --git a/test/test_koozie.py b/test/test_koozie.py index 070c9f1..b59bf4c 100644 --- a/test/test_koozie.py +++ b/test/test_koozie.py @@ -4,7 +4,7 @@ from pytest import approx from click.testing import CliRunner -from koozie import fr_u, to_u, convert, get_dimensionality +from koozie import fr_u, to_u, convert, get_dimensionality, format_units from koozie.cli import koozie_cli @@ -28,6 +28,21 @@ def test_dimensionality(): assert get_dimensionality("F") != get_dimensionality("C") assert get_dimensionality("%") == get_dimensionality("") assert get_dimensionality("h") == get_dimensionality("s") + assert get_dimensionality("ton_ref") == get_dimensionality("W") + + +def test_unit_formatting(): + """Test unit formatting""" + assert format_units("degF") == "°F" + assert format_units("m**3/s") == "m³/s" + assert format_units("m**1.5/s") == "m¹⋅⁵/s" + assert format_units("m**2*K/W") == "K·m²/W" + assert format_units("degree") == "deg" + assert format_units("cm**3") == "cm³" + assert format_units("inch_H2O_39F") == "inch_H2O_39F" + assert format_units("cfm") == "cfm" + # assert format_units("thermal_resistance_SI") == "m²·K/W" + # assert format_units("ton_ref") == "ton_ref" def test_iterable():