11# _*_ coding:utf-8 _*_
22
3- # Stolen from https://github.com/SchrodingersGat/KiBoM/blob/master/KiBOM/units.py
3+ # Stolen from
4+ # https://github.com/SchrodingersGat/KiBoM/blob/master/KiBOM/units.py
45
56"""
67
1718current_locale = locale .setlocale (locale .LC_NUMERIC )
1819try :
1920 locale .setlocale (locale .LC_NUMERIC , '' )
20- except Exception as ignore :
21+ except Exception :
2122 # sometimes setlocale with empty string doesn't work on OSX
2223 pass
2324decimal_separator = locale .localeconv ()['decimal_point' ]
2425locale .setlocale (locale .LC_NUMERIC , current_locale )
2526
26- PREFIX_MICRO = [u"μ" , " u" , "micro" ]
27+ PREFIX_MICRO = [u"μ" , u"µ" , " u" , "micro" ] # first is \u03BC second is \u00B5
2728PREFIX_MILLI = ["milli" , "m" ]
2829PREFIX_NANO = ["nano" , "n" ]
2930PREFIX_PICO = ["pico" , "p" ]
3334
3435# All prefices
3536PREFIX_ALL = PREFIX_PICO + PREFIX_NANO + PREFIX_MICRO + \
36- PREFIX_MILLI + PREFIX_KILO + PREFIX_MEGA + PREFIX_GIGA
37+ PREFIX_MILLI + PREFIX_KILO + PREFIX_MEGA + PREFIX_GIGA
3738
3839# Common methods of expressing component units
39- UNIT_R = ["r" , "ohms" , "ohm" , u"Ω" ]
40+ UNIT_R = ["r" , "ohms" , "ohm" , u"Ω" , u"ω" ]
4041UNIT_C = ["farad" , "f" ]
4142UNIT_L = ["henry" , "h" ]
4243
4344UNIT_ALL = UNIT_R + UNIT_C + UNIT_L
4445
45- """
46- Return a simplified version of a units string, for comparison purposes
47- """
46+ VALUE_REGEX = re .compile (
47+ "^([0-9\\ .]+)(" + "|" .join (PREFIX_ALL ) + ")*(" + "|" .join (
48+ UNIT_ALL ) + ")*(\\ d*)$" )
49+
50+ REFERENCE_REGEX = re .compile ("^(r|rv|c|l)(\\ d+)$" )
4851
4952
5053def getUnit (unit ):
54+ """
55+ Return a simplified version of a units string, for comparison purposes
56+ """
5157 if not unit :
5258 return None
5359
@@ -63,12 +69,10 @@ def getUnit(unit):
6369 return None
6470
6571
66- """
67- Return the (numerical) value of a given prefix
68- """
69-
70-
7172def getPrefix (prefix ):
73+ """
74+ Return the (numerical) value of a given prefix
75+ """
7276 if not prefix :
7377 return 1
7478
@@ -92,32 +96,21 @@ def getPrefix(prefix):
9296 return 1
9397
9498
95- def groupString (group ): # return a reg-ex string for a list of values
96- return "|" .join (group )
97-
98-
99- def matchString ():
100- return "^([0-9\.]+)(" + groupString (PREFIX_ALL ) + ")*(" + groupString (
101- UNIT_ALL ) + ")*(\d*)$"
102-
103-
104- """
105- Return a normalized value and units for a given component value string
106- e.g. compMatch("10R2") returns (10, R)
107- e.g. compMatch("3.3mOhm") returns (0.0033, R)
108- """
109-
110-
11199def compMatch (component ):
100+ """
101+ Return a normalized value and units for a given component value string
102+ e.g. compMatch("10R2") returns (1000, R)
103+ e.g. compMatch("3.3mOhm") returns (0.0033, R)
104+ """
112105 component = component .strip ().lower ()
113106 if decimal_separator == ',' :
114107 # replace separator with dot
115108 component = component .replace ("," , "." )
116109 else :
117110 # remove thousands separator
118111 component = component .replace ("," , "" )
119- match = matchString ()
120- result = re . search ( match , component )
112+
113+ result = VALUE_REGEX . match ( component )
121114
122115 if not result :
123116 return None
@@ -137,20 +130,21 @@ def compMatch(component):
137130 value = float (int (value ))
138131 postValue = float (int (post )) / (10 ** len (post ))
139132 value = value * 1.0 + postValue
140- except :
133+ except ValueError :
141134 return None
142135
143136 try :
144137 val = float (value )
145- except :
138+ except ValueError :
146139 return None
147140
148141 val = "{0:.15f}" .format (val * 1.0 * getPrefix (prefix ))
149142
150143 return (val , getUnit (units ))
151144
152145
153- def componentValue (valString ):
146+ def componentValue (valString , reference ):
147+ # type: (str, str) -> tuple
154148 result = compMatch (valString )
155149
156150 if not result :
@@ -159,10 +153,21 @@ def componentValue(valString):
159153 if not len (result ) == 2 : # result length is incorrect
160154 return valString , None # return the same string back with `None` unit
161155
162- return result # (val,unit)
156+ if result [1 ] is None :
157+ # try to infer unit from reference
158+ match = REFERENCE_REGEX .match (reference .lower ())
159+ if match and len (match .groups ()) == 2 :
160+ prefix , _ = match .groups ()
161+ unit = None
162+ if prefix in ['r' , 'rv' ]:
163+ unit = 'R'
164+ if prefix == 'c' :
165+ unit = 'F'
166+ if prefix == 'l' :
167+ unit = 'H'
168+ result = (result [0 ], unit )
163169
164-
165- # compare two values
170+ return result # (val,unit)
166171
167172
168173def compareValues (c1 , c2 ):
@@ -185,3 +190,9 @@ def compareValues(c1, c2):
185190 return True # no units for component 2
186191
187192 return False
193+
194+
195+ print (compMatch ("1µF" ))
196+ print (compMatch ("1u" ))
197+
198+ print (componentValue ("1µ" , "C2" ))
0 commit comments