Skip to content

Commit eb44d7d

Browse files
allow the unit to be included in the value
1 parent ab28a90 commit eb44d7d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

schemascii/metric.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from decimal import Decimal
33

44
METRIC_NUMBER = re.compile(
5-
r"(\d*\.?\d+)([pnumKkMGT]?)") # cSpell:ignore pnum
5+
r"(\d*\.?\d+)\s*([pnumKkMGT]?)") # cSpell:ignore pnum
66
METRIC_RANGE = re.compile(
7-
r"(\d*\.?\d+[pnumKkMGT]?)-(\d*\.?\d+[pnumKkMGT]?)")
7+
r"(\d*\.?\d+\s*[pnumKkMGT]?)-(\d*\.?\d+\s*[pnumKkMGT]?)")
88
ENG_NUMBER = re.compile(r"^(\d*\.?\d+)[Ee]?([+-]?\d*)$")
99

1010

@@ -102,7 +102,8 @@ def format_metric_unit(
102102
six: bool = False,
103103
unicode: bool = True,
104104
allow_range: bool = True) -> str:
105-
"""Normalizes the Metric multiplier on the number, then adds the unit.
105+
"""Normalizes the Metric multiplier on the number, then adds the unit
106+
if the unit was not used.
106107
107108
* If there is a suffix on num, moves it to after the unit.
108109
* If there is a range of numbers, formats each number in the range
@@ -117,7 +118,7 @@ def format_metric_unit(
117118
raise ValueError("range not allowed")
118119
# format the range by calling recursively
119120
num0, num1 = match.group(1), match.group(2)
120-
suffix = num[match.span()[1]:]
121+
suffix = num[match.span()[1]:].lstrip().removeprefix(unit)
121122
digits0, exp0 = normalize_metric(num0, six, unicode)
122123
digits1, exp1 = normalize_metric(num1, six, unicode)
123124
if exp0 != exp1 and digits0 != "0":
@@ -128,7 +129,7 @@ def format_metric_unit(
128129
match = METRIC_NUMBER.match(num)
129130
if not match:
130131
return num
131-
suffix = num[match.span(0)[1]:]
132+
suffix = num[match.span(0)[1]:].lstrip().removeprefix(unit)
132133
digits, exp = normalize_metric(match.group(), six, unicode)
133134
return f"{digits} {exp}{unit} {suffix}".rstrip()
134135

@@ -138,9 +139,12 @@ def test(*args):
138139
print(">>> format_metric_unit", args, sep="")
139140
print(repr(format_metric_unit(*args)))
140141
test("2.5-3500", "V")
141-
test("50n", "F", True)
142+
test("0.33m", "H", True)
142143
test("50M-100000000000000000000p", "Hz")
143144
test(".1", "Ω")
144145
test("2200u", "F", True)
146+
test("2200uF", "F", True)
147+
test("2200u F", "F", True)
148+
test("2200 uF", "F", True)
145149
test("0-100k", "V")
146150
test("Gain", "Ω")

0 commit comments

Comments
 (0)