Skip to content

Commit

Permalink
pre-commit fixes
Browse files Browse the repository at this point in the history
Forgot to run pre-commit for initial commit.

Signed-off-by: Michael Tiemann <[email protected]>
  • Loading branch information
MichaelTiemannOSC committed Oct 22, 2023
1 parent 9320a32 commit cdf7e65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 15 additions & 4 deletions pint/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,17 @@ def format_compact(unit: UnitsContainer, registry: UnitRegistry, **options) -> s
)


dim_order = [ '[substance]', '[mass]', '[current]', '[luminosity]', '[length]', '[time]', '[temperature]' ]
dim_order = [
"[substance]",
"[mass]",
"[current]",
"[luminosity]",
"[length]",
"[time]",
"[temperature]",
]


def dim_sort(units: Iterable[list[str]], registry: UnitRegistry):
"""Sort a list of units by dimensional order.
Expand All @@ -318,7 +328,7 @@ def dim_sort(units: Iterable[list[str]], registry: UnitRegistry):
If unit cannot be found in the registry.
"""
ret_dict = dict()
many = len(units) > 1
len(units) > 1
for name in units:
cname = registry.get_name(name)
if not cname:
Expand All @@ -338,6 +348,7 @@ def dim_sort(units: Iterable[list[str]], registry: UnitRegistry):
ret = sum([ret_dict[dim] for dim in dim_order if dim in ret_dict], [])
return ret


def formatter(
items: Iterable[tuple[str, Number]],
as_ratio: bool = True,
Expand Down Expand Up @@ -449,9 +460,9 @@ def formatter(
neg_terms.append(power_fmt.format(key, fun(value)))

if sort_dims:
if len(pos_terms)>1:
if len(pos_terms) > 1:
pos_terms = dim_sort(pos_terms, registry)
if len(neg_terms)>1:
if len(neg_terms) > 1:
neg_terms = dim_sort(neg_terms, registry)

if not as_ratio:
Expand Down
12 changes: 9 additions & 3 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,13 +1157,19 @@ def test_issues_1841():

# sets compact display mode
ur = UnitRegistry()
ur.default_format = '~P'
ur.default_format = "~P"

# creates quantity
q = ur.Quantity("1 kW * 1 h")

assert pint.formatting.format_unit(q.u._units, spec='', registry=ur, sort_dims=True) == 'kilowatt * hour'
assert pint.formatting.format_unit(q.u._units, spec='', registry=ur, sort_dims=False) == 'hour * kilowatt'
assert (
pint.formatting.format_unit(q.u._units, spec="", registry=ur, sort_dims=True)
== "kilowatt * hour"
)
assert (
pint.formatting.format_unit(q.u._units, spec="", registry=ur, sort_dims=False)
== "hour * kilowatt"
)

# this prints "1 h·kW", not "1 kW·h" unless sort_dims is True
# print(q)

0 comments on commit cdf7e65

Please sign in to comment.