2
2
from decimal import Decimal
3
3
4
4
METRIC_NUMBER = re .compile (
5
- r"(\d*\.?\d+)([pnumKkMGT]?)" ) # cSpell:ignore pnum
5
+ r"(\d*\.?\d+)\s* ([pnumKkMGT]?)" ) # cSpell:ignore pnum
6
6
METRIC_RANGE = re .compile (
7
- r"(\d*\.?\d+[pnumKkMGT]?)-(\d*\.?\d+[pnumKkMGT]?)" )
7
+ r"(\d*\.?\d+\s* [pnumKkMGT]?)-(\d*\.?\d+\s* [pnumKkMGT]?)" )
8
8
ENG_NUMBER = re .compile (r"^(\d*\.?\d+)[Ee]?([+-]?\d*)$" )
9
9
10
10
@@ -102,7 +102,8 @@ def format_metric_unit(
102
102
six : bool = False ,
103
103
unicode : bool = True ,
104
104
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.
106
107
107
108
* If there is a suffix on num, moves it to after the unit.
108
109
* If there is a range of numbers, formats each number in the range
@@ -117,7 +118,7 @@ def format_metric_unit(
117
118
raise ValueError ("range not allowed" )
118
119
# format the range by calling recursively
119
120
num0 , num1 = match .group (1 ), match .group (2 )
120
- suffix = num [match .span ()[1 ]:]
121
+ suffix = num [match .span ()[1 ]:]. lstrip (). removeprefix ( unit )
121
122
digits0 , exp0 = normalize_metric (num0 , six , unicode )
122
123
digits1 , exp1 = normalize_metric (num1 , six , unicode )
123
124
if exp0 != exp1 and digits0 != "0" :
@@ -128,7 +129,7 @@ def format_metric_unit(
128
129
match = METRIC_NUMBER .match (num )
129
130
if not match :
130
131
return num
131
- suffix = num [match .span (0 )[1 ]:]
132
+ suffix = num [match .span (0 )[1 ]:]. lstrip (). removeprefix ( unit )
132
133
digits , exp = normalize_metric (match .group (), six , unicode )
133
134
return f"{ digits } { exp } { unit } { suffix } " .rstrip ()
134
135
@@ -138,9 +139,12 @@ def test(*args):
138
139
print (">>> format_metric_unit" , args , sep = "" )
139
140
print (repr (format_metric_unit (* args )))
140
141
test ("2.5-3500" , "V" )
141
- test ("50n " , "F " , True )
142
+ test ("0.33m " , "H " , True )
142
143
test ("50M-100000000000000000000p" , "Hz" )
143
144
test (".1" , "Ω" )
144
145
test ("2200u" , "F" , True )
146
+ test ("2200uF" , "F" , True )
147
+ test ("2200u F" , "F" , True )
148
+ test ("2200 uF" , "F" , True )
145
149
test ("0-100k" , "V" )
146
150
test ("Gain" , "Ω" )
0 commit comments