Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add units formatting #4

Merged
merged 3 commits into from
Dec 30, 2024
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
15 changes: 2 additions & 13 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ jobs:
uses: bigladder/github-actions/setup-python-poetry@main
with:
python-version: ${{ matrix.python-version }}
- name: Lint with Pylint
- name: Run tasks # doit -v 1 for now because of issue with Windows output stream character maps
run: |
poetry run pylint ${{env.REPOSITORY_NAME}}
poetry run pylint test/*.py
continue-on-error: true
- name: Static type checking
run: |
poetry run mypy ${{env.REPOSITORY_NAME}}
poetry run mypy test/*.py
continue-on-error: true
- name: Test
run: poetry run pytest
- name: List units
run: poetry run koozie -l
poetry run doit -v 1
44 changes: 44 additions & 0 deletions dodo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

DOIT_CONFIG = {"continue": True}

project_name = "koozie"


def task_check_code():
"""Run linter(s)"""
checkers = ["pylint", "mypy", "black --check"]
source_checking_paths = [project_name, "test"]

for checker in checkers:
for path in source_checking_paths:
command = f"{checker} {path}"
if checker == "pylint":
# Get around weird issue on Windows
command += "/*.py"
yield {
"name": f"{checker} {path}",
"actions": [command],
"verbosity": 1, # print only errors
}


def run_pytest():
"""Run pytest"""
return not bool(pytest.main(["-v", "-s", "test"]))


def task_test():
"""Performs tests"""
return {
"actions": [(run_pytest, [])],
"verbosity": 2, # print everything
}


def task_run_cli():
"""Run the CLI"""
return {
"actions": ["koozie -l"],
"verbosity": 1, # print only errors
}
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
93 changes: 82 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ python = "^3.10"
pint = "^0.24"
click = "^8.1.3"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
pytest = "^7.1.3"
doit = "*"
pylint = "*"
black = "*"
mypy = "*"
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
Loading