Skip to content

Commit

Permalink
Add unit formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
nealkruis committed Dec 27, 2024
1 parent 5749d7a commit f196e66
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion koozie/__init__.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion koozie/__main__.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions koozie/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""koozie command line interface"""

import sys
import click
import koozie
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions koozie/koozie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
17 changes: 16 additions & 1 deletion test/test_koozie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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():
Expand Down

0 comments on commit f196e66

Please sign in to comment.