Skip to content

Commit

Permalink
Merge pull request #3 from bigladder/fix-numpy-compatibility
Browse files Browse the repository at this point in the history
Fix numpy compatibility and update python version compatibility
  • Loading branch information
nealkruis authored Dec 21, 2024
2 parents cd25a7e + da4d805 commit 417a270
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3.9"]
python-version: ["3.11"]
defaults:
run:
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12", "3.13"]
defaults:
run:
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ indent-after-paren=4
indent-string=' '

# Maximum number of characters on a single line.
max-line-length=100
max-line-length=120

# Maximum number of lines in a module.
max-module-lines=1000
Expand Down
23 changes: 11 additions & 12 deletions koozie/koozie.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from typing import List, Union # Needed for python < 3.10

import pint
import pint.definitions
import pint.facets

# Set up UnitRegistry with abbreviated scientific format
unit_registry = pint.UnitRegistry()
unit_registry.default_format = "~P" # short pretty
unit_registry.formatter.default_format = "~P" # short pretty

# Add new aliases
unit_registry.define("@alias inch_H2O_39F = in_H2O")
Expand All @@ -32,35 +34,31 @@


# Private functions (used in CLI)
def fr_q(value: SupportsFloat, from_units: str) -> pint.Quantity:
def fr_q(value: SupportsFloat, from_units: str) -> pint.facets.plain.PlainQuantity:
"""Convert a value from given units to a quantity in base SI units"""
return unit_registry.Quantity(value, from_units).to_base_units()


def to_q(value: SupportsFloat, to_units: str) -> pint.Quantity:
def to_q(value: SupportsFloat, to_units: str) -> pint.facets.plain.PlainQuantity:
"""Convert a value from base SI units to a quantity in any other units"""
base_units = unit_registry.Quantity(value, to_units).to_base_units().units
return unit_registry.Quantity(value, base_units).to(to_units)


def convert_q(value: SupportsFloat, from_units: str, to_units: str) -> pint.Quantity:
def convert_q(value: SupportsFloat, from_units: str, to_units: str) -> pint.facets.plain.PlainQuantity:
"""Convert a value from any units to a quantity in another units of the same dimension"""
return unit_registry.Quantity(value, from_units).to(to_units)


# Public functions
def fr_u(
value: Union[SupportsFloat, Iterable[SupportsFloat]], from_units: str
) -> Union[float, List[float]]:
def fr_u(value: Union[SupportsFloat, Iterable[SupportsFloat]], from_units: str) -> Union[float, List[float]]:
"""Convert a value from given units to base SI units"""
if isinstance(value, SupportsFloat):
return fr_q(value, from_units).magnitude
return [fr_q(v, from_units).magnitude for v in value]


def to_u(
value: Union[SupportsFloat, Iterable[SupportsFloat]], to_units: str
) -> Union[float, List[float]]:
def to_u(value: Union[SupportsFloat, Iterable[SupportsFloat]], to_units: str) -> Union[float, List[float]]:
"""Convert a value from base SI units to any other units"""
if isinstance(value, SupportsFloat):
return to_q(value, to_units).magnitude
Expand Down Expand Up @@ -99,11 +97,12 @@ def get_unit_list() -> OrderedDict:
unit_list[dimensionality]["units"][unit].append(u)

# Get dimension aliases
for dim in unit_registry._dimensions: # pylint: disable=protected-access
for dim, definition in unit_registry._dimensions.items(): # pylint: disable=protected-access
if dim in unit_list:
continue

reference = unit_registry._dimensions[dim].reference # pylint: disable=protected-access
assert isinstance(definition, pint.facets.plain.definitions.DerivedDimensionDefinition)
reference = definition.reference
dimensionality = f"{unit_registry.get_dimensionality(reference)}"
if dimensionality not in unit_list:
unit_list[dimensionality] = {"aliases": [], "units": {}}
Expand Down
Loading

0 comments on commit 417a270

Please sign in to comment.